)
I have some cloudinary's photos and I would like to make a seed (in my rails app).
I'm using Carrierwave.
In my seed, I try to put the cloudinary's url image :
Course.create! ({
photo: "fismpnq3zma80dc2ovjt.jpg"
)}
But it's don't work. What can I do ?
If I ask by console :
pry(main)> c = Course.first
....
pry(main)> c.photo
#cache_id=nil,
#file=nil,
#filename=nil,
....
#model=#<Way:0x00007f80e7a3a9c0
photo:nil,
....
#mounted_as=:photo,
#versions=nil>
In order to use the create method, the parameters need to be verified (by signature) before the SDK will save them.
For example:
resource_type = "image"
type = "upload"
version = 1234567890
public_id = "fismpnq3zma80dc2ovjt"
format = "jpg"
signature = Cloudinary::Utils.api_sign_request({:public_id=>public_id,
:version=>version}, Cloudinary.config.api_secret)
photo = "#{resource_type}/#{type}/v#{version}/#{public_id}.#{format}##
{signature}"
Course.create!({ photo: photo )}
Please let me know if it works for you.
--Yakir
Related
I'm using restforce to interat with salesforce in Ruby on Rails.
I have this snippet of code:
client = Restforce.new
acc = client.find("Account", account_ID)
acc.firstName = "test"
acc.save
# if I use update I get the same error
acc.update
But I get the following error
[
{
"message":"Unable to create/update fields: jigsaw_clean__Sync_Status_Indicator__c, LastModifiedDate, PhotoUrl, IsDeleted, jigsaw_clean__Jigsaw_Managed_Backend__c, jigsaw_clean__Sync_Status_Summary__c, AccPwr1__c, BillingAddress, jigsaw_clean__Duplicate__c, LastActivityDate, jigsaw_clean__Automatic_Updates__c, JigsawCompanyId, CreatedById, MasterRecordId, LastViewedDate, CreatedDate, LastReferencedDate, jigsaw_clean__Additional_Information__c, jigsaw_clean__Jigsaw_Last_Sync_Locked__c, Cross_Sell_Score__c, jigsaw_clean__Jigsaw_Managed__c, ShippingAddress, LastModifiedById, IANA_Number_field__c. Please check the security settings of this field and verify that it is read/write for your profile or permission set.",
"errorCode":"INVALID_FIELD_FOR_INSERT_UPDATE",
"fields":[
"jigsaw_clean__Sync_Status_Indicator__c",
"jigsaw_clean__Jigsaw_Managed_Backend__c",
"jigsaw_clean__Sync_Status_Summary__c",
"AccPwr1__c",
"BillingAddress",
"jigsaw_clean__Duplicate__c",
"LastActivityDate",
"jigsaw_clean__Automatic_Updates__c",
"CreatedById",
"MasterRecordId",
"jigsaw_clean__Additional_Information__c",
"jigsaw_clean__Jigsaw_Last_Sync_Locked__c",
"Cross_Sell_Score__c",
"jigsaw_clean__Jigsaw_Managed__c",
]
}
]
and other fields.
I know, I can do something like:
client = Restforce.new
acc = client.find("Account", account_ID)
acc1 = {Id: acc.Id}
acc1["firstName"] = "test"
client.update("Account", acc1)
How do I do this in a more efficient way?
Please check the security settings of this field and verify that it is read/write for your profile or permission set.
Are you sure that you have permission to make updates?
The only way I can find is the instead of using find we use query with fields that we want to update.
client = Restforce.new
acc1 = client.query("SELECT ID, firstName, LastName FROM Account where ID = '#{account_ID}'").first
acc1["firstName"] = "test"
acc1.save
I am new to shopify. Now I want to use shopify API to create a price rule, this is my source code here
I use rails 5.1.4, shopify_app 8.1.0
shop_url = "https://api_key:secret#domain/admin"
ShopifyAPI::Base.site = shop_url
prerequisite_saved_search_ids = [53677883419]
price_rule = ShopifyAPI::PriceRule.new
price_rule.title = "demodemo"
price_rule.target_name = "line_item"
price_rule.target_selection = "all"
price_rule.allocation_method = "across"
price_rule.value_type = "fixed_amount"
price_rule.value = "-10.0"
price_rule.customer_selection = "prerequisite"
price_rule.prerequisite_saved_search_ids = prerequisite_saved_search_ids
price_rule.start_at = Time.now.iso8601
res = price_rule.save
puts res
However it always return me false. If anyone has the idea? Thanks a million!
Please check this Api to create price rule(Shopify Api). I have used this Api in php and its working fine for me.
I have created App and then use Api key and secret key to generate price rule.
Thanks
For the ones coming to this question, one could fetch Price rules as per shopify_app gem for Rails applications as:
First allow your app to access read/write permissions in initializers/shopify.rb file as:
config.scope = "read_products, write_products, read_price_rules, write_price_rules"
After that you can fetch price rules as:
#price_rules = ShopifyAPI::PriceRule.find(:all, params:{id: '4171201931'})
And can also create a price rule as:
#create_price_rule = ShopifyAPI::PriceRule.new(
price_rule: {
title: "FREESHIPPING2",
target_type: "shipping_line",
target_selection: "all",
allocation_method: "each",
value_type: "percentage",
value: "-100.0",
usage_limit: 20,
customer_selection: "all",
prerequisite_subtotal_range: {
greater_than_or_equal_to: "50.0"
},
starts_at: "2017-11-19T17:59:10Z"
}
)
#create_price_rule.save
There are validations involved. Incase you want to check the response, one may inspect it like #create_price_rule.inspect
Or even you can delete a PriceRule as:
#price_rules = ShopifyAPI::PriceRule.find(:all).first
#price_rules.destroy
#last_price_rule = ShopifyAPI::PriceRule.find(4171860875)
I'm new to Rails.
I have a method that downloads a file from google drive and saves it in the local disc. When the file is downloaded, the console returns nil, but the file is in the folder.
I need to use this file in my controller, but if the download method is returning nil I can't pass it around as an object.
download method:
def download
found = google_files.select { |f| f.id == file_id }
file_title = found.first.title
file = session.file_by_title(file_title)
path = File.join(Backup.base_directory, file_title)
file.download_to_file("#{path}")
end
Controller:
def create
# file_id = params.fetch(:file_id)
file_id = "0Byyflt8z3jarbm5DZGNNVXZSWjg"
#backup = DiscourseDownloadFromDrive::DriveDownloader.new(file_id).download
end
console output after executing the download method:
[...]
Writing chunk (1397 bytes)
Writing chunk (1397 bytes)
Writing chunk (1397 bytes)
Writing chunk (619 bytes)
Success - nil
=> nil
[4] pry(main)>
Logger:
Rails.logger.debug(">>> #BACKUP >>>: #{#backup.inspect}")
D, [2017-09-07T20:21:24.835450 #7755] DEBUG -- : >>> #BACKUP >>>: nil
Any hint on how to proceed with this would be very much appreciated!
Your download method always returns nothing but nil. That's because the gem's download_to_file always returns nil.
You shoud change your download method for it to return something, that you can use to get the file. I think this method should return the path to the downloaded file.
def download
found = google_files.select { |f| f.id == file_id }
file_title = found.first.title
file = session.file_by_title(file_title)
path = File.join(Backup.base_directory, file_title)
file.download_to_file("#{path}")
path
end
Now you can use it in the controller:
def create
# file_id = params.fetch(:file_id)
file_id = "0Byyflt8z3jarbm5DZGNNVXZSWjg"
file_path = DiscourseDownloadFromDrive::DriveDownloader.new(file_id).download
#backup = File.open(file_path) # do whatever you want with the file, since now you know how to get it
end
I'm fetching rss data from a news API. However, sometimes the entries have certain fields such as image or summary, and sometimes they do not. How can I check if the object is empty before calling it?
url = "http://rss.cnn.com/rss/cnn_topstories.rss"
feed = Feedjira::Feed.fetch_and_parse url
api_json = JSON.parse(feed.to_json)
data_array = []
feed.entries.each_with_index do |entry,index|
data_array << { title: entry.title, link: entry.url, image_url: entry.image, summary: entry.summary }.as_json
end
In the above code, sometimes entry.image and entry.summary are empty and it returns an error such as:
undefined method `image' for #<Feedjira::Parser::ITunesRSSItem:0x007fb25c452688>
Current Attempt:
One obvious way is to check every object before saving it as a variable. But is this the best approach?
if entry.image.exists?
image = entry.image
else
image = ""
end
if entry.summary.exists?
summary = entry.summary
else
summary = ""
end
Use try(), it will return nil if the associated property is missing
entry.try(:image)
In your code, you can do like this
data_array << { title: entry.try(:title), link: entry.try(:url), image_url: entry.try(:image), summary: entry.try(:summary) }.as_json
Hope this helps!
If you are using Ruby 2.3.0 or higher you can make use of the brand new Safe Navigation Operator:
entry&.image # same thing as entry.try(:image) but shorter
You can even navigate as deep as you want, e.g.:
entry&.image&.urls&.small # Works as a charm even if "urls" is not present
I need to parse some params to my DB and image from URL.
I use paperclip for image.
In Rails console I can add image to new post by this code:
image = Image.new
image.image_from_url "http://yug-avto.ru/files/image/tradein/hyundai/877_VOLKSWAGEN_FAETON_2011_2_1366379491.jpg"
image.watermark = true
image.save!
in my Image model I have
require "open-uri"
.......
def image_from_url(img_url)
self.image = open(img_url)
end
And all work done. But when I use Nokogiri, this code don't work.
rake aborted!
No such file or directory -
http://yug-avto.ru/files/image/tradein/peugeot/1027_Peugeot_308_2011_2_1370850441.jpg
My rake task for Nokogiri parse:
doc.xpath("//item").each do |ad|
img = ad.at("image").text
img1 = Image.new
img1.image = open("#{img}")
img1.watermark = true
img1.save!
end
In rake task for Nokogiri, I have require 'nokogiri' and require 'open-uri'.
How to be?:))))
This is a code snippet from my parser... I guess where you went wrong is using open(url) instead of parse(url).
picture = Picture.new(
realty_id: realty.id,
position: position,
hashcode: realty.hashcode
)
# picture.image = URI.parse(url), edit: added open() as this worked for Savroff
picture.image = open(URI.parse(url))
picture.save!
Additionally it would be a good idea to check if the image really exists
picture_array.each do |url|
# checks if the Link works
res = Net::HTTP.get_response(URI.parse(url))
# if so, it will add the Picture Link to the verified Array
if res.code.to_i >= 200 && res.code.to_i < 400 #good codes will be betweem 200 - 399
verified_array << url
end
end
Thanks TheChamp, you led me to the right thoughts.
First need to parse URL and after that open.
image = Image.new
ad_image_url = URI.parse("#{img}")
image.image = open(ad_image_url)
image.watermark = true
image.save!