I have a method CustomGoogleDirections
class CustomGoogleDirections < GoogleDirections
##base_url = "https://maps.googleapis.com/maps/api/directions/xml"
end
Which i call in my controller to find the location based on a hash:
#locations = #all_locations.each_cons(2).with_index.with_object({}) do |((e1, e2), i), h|
h[i + 1] =
[e1, e2,
CustomGoogleDirections.new(e1, e2, key: Figaro.env.google_translate_secret_key).distance_in_miles, CustomGoogleDirections.new(e1, e2, key: Figaro.env.google_translate_secret_key).drive_time_in_minutes.url]
end
This result in the error:
Errno::ENOENT (No such file or directory # rb_sysopen - https://maps.googleapis.com/maps/api/directions/xml?destination=60%2BBarron%2BRoad%252C%2BDubai%252C%2BDubai&key=######################&origin=60%2BAlFhaged%2BRoad%252C%2BDubai%252C%2Dubai)
It used to work well before I did a rails upgrade.
Does anynone knows what I am missing please?
(When I used the generated xml link, it does load properly with all the long/lat informations)
I found a solution by modifying the gem, changing :
#url = ##base_url + '?' + #options.to_query
#xml = open(#url).read
#doc = Nokogiri::XML(#xml)
#status = #doc.css('status').text
to :
#url = ##base_url + '?' + #options.to_query
#response = Net::HTTP.get_response(URI(#url))
#doc = Nokogiri::XML(#response.body)
#status = #doc.css('status').text
It works locally but not sure about this as the right approach.
Related
Would it be possible to access the ActiveStorageBlob or ActiveStorageAttachment like it would be a native model ?
E.g.
I want to do ActiveStorageBlob.first to access the first record of this model/table.
or. ActiveStorageAttachment.all.as_json to generate json formated print.
The background idea is to find a way how to dump the content of these ActiveStorage related tables as json formated files. Then change simething on these files, and load it back.
----Extending this text after got correct answer-----
Thank you very much Sarah Marie.
And I hope you know how to load the JSON data back into these tables ?
I have tried this :
dump_file_path = File.join(Rails.root, "backup", active_storage_blobs_file)
load_json = JSON.parse(File.read(dump_file_path))
load_json.each do |j|
ActiveStorage::Blob.create(j)
end
But thats not working.
ActiveModel::UnknownAttributeError (unknown attribute
'attachable_sgid' for ActiveStorage::Blob.)
ActiveStorage::Blob.first
ActiveStorage::Attachment.all.as_json
---- For second extended question ----
ActiveStorage::Blob.create_before_direct_upload!(
filename: j[:filename],
content_type: j[:content_type],
byte_size: j[:byte_size],
checksum: j[:checksum]
)
# or
ActiveStorage::Blob.create_before_direct_upload!(**j.symbolize_keys)
Reference: https://github.com/rails/rails/blob/5f3ff60084ab5d5921ca3499814e4697f8350ee7/activestorage/app/controllers/active_storage/direct_uploads_controller.rb#L8-L9
https://github.com/rails/rails/blob/098fd7f9b3d5c6f540911bc0c17207d6b48d5bb3/activestorage/app/models/active_storage/blob.rb#L113-L120
Now I have a complete solution, how to dump and load the ActiveStorage tables as JSON files.
...dump it
active_storage_blobs_file = "active_storage_blob.json"
active_storage_attachments_file = "active_storage_attachment.json"
puts("...dump active_storage_blob")
dump_file_path = File.join(Rails.root, "backup",active_storage_blobs_file)
dump_file = File.open(dump_file_path, "w")
dump_file.write(JSON.pretty_generate(ActiveStorage::Blob.all.as_json))
dump_file.close()
puts("...dump active_storage_attachment")
dump_file_path = File.join(Rails.root, "backup",
active_storage_attachments_file)
dump_file = File.open(dump_file_path, "w")
dump_file.write(JSON.pretty_generate(ActiveStorage::Attachment.all.as_json))
dump_file.close()
...load it back
puts("...load active_storage_blob")
dump_file_path = File.join(Rails.root, "backup", active_storage_blobs_file)
abort("File does not exist (" + dump_file_path + ") > abort <") unless File.exist?(dump_file_path)
load_json = JSON.parse(File.read(dump_file_path))
load_json.each do |j|
j = j.except("attachable_sgid")
result = ActiveStorage::Blob.create(j)
if (not result.errors.empty?)
puts(result.errors.full_messages.to_s)
puts(j.inspect)
exit(1)
end
end
puts("...load active_storage_attachment")
dump_file_path = File.join(Rails.root, "backup", active_storage_attachments_file)
abort("File does not exist (" + dump_file_path + ") > abort <") unless File.exist?(dump_file_path)
load_json = JSON.parse(File.read(dump_file_path))
load_json.each do |j|
result = ActiveStorage::Attachment.create(j)
if (not result.errors.empty?)
puts(result.errors.full_messages.to_s)
puts(j.inspect)
exit(1)
end
end
I'm porting the Gulp Starter Rails helpers to Middleman but am getting the following error:
no implicit conversion of Symbol into String related to Ruby /middleman-gulp-starter/helpers/gulp_asset_helper.rb: in join, line 14
I'm not sure of the differences between Rails and Middleman to understand why this isnt working.
module GulpAssetHelper
def gulp_asset_path(path, type = nil)
rev_manifest = nil
# In development, check for the manifest every time
if !config[:build]
rev_manifest = JSON.parse(File.read(REV_MANIFEST_PATH)) if File.exist?(REV_MANIFEST_PATH)
# In production, use the manifest cached in initializers/gulp.rb
else
rev_manifest = REV_MANIFEST if defined?(REV_MANIFEST)
end
root = GULP_CONFIG['root']['dest'].gsub(/(.*)build\//, '/')
asset_path = type ? File.join(GULP_CONFIG['tasks'][type]['dest'], path) : path # LINE 14
asset_path = rev_manifest[asset_path] if rev_manifest
asset_path = File.join(root, asset_path)
File.absolute_path(asset_path, '/')
end
def gulp_js_path(path)
gulp_asset_path(path, 'js')
GULP_CONFIG
end
def gulp_css_path(path)
gulp_asset_path(path, 'css')
end
def gulp_image_path(path)
gulp_asset_path(path, 'images')
end
def sprite(id, classes = "", viewBox = "0 0 24 24")
"<svg class='sprite -#{id} #{classes}' aria-hidden='true' preserveAspectRatio viewBox='#{viewBox}'><use xlink:href='#{gulp_image_path('sprites.svg')}##{id}' /></use></svg>".html_safe
end
end
The rev and config import file:
GULP_CONFIG = JSON.parse(File.read('gulpfile.js/config.json'))
REV_MANIFEST_PATH = File.join(GULP_CONFIG['root']['dest'], 'rev-manifest.json')
if File.exist?(REV_MANIFEST_PATH)
REV_MANIFEST = JSON.parse(File.read(REV_MANIFEST_PATH))
end
Example rev-manifest.json file:
{
"images/middleman-logo.svg": "images/middleman-logo-2e3d8b5ad1.svg",
"javascripts/all.js": "javascripts/all-92681c51e741e0e1370c.js",
"stylesheets/site.css": "stylesheets/site-9b25f1d1ac.css"
}
I've output the contents of the gulpfile so know that it is being read correctly.
You can find the full repo here: https://github.com/craigmdennis/middleman-gulp-starter/tree/4_asset-helpers
it looks like it fails on this line:
asset_path = type ? File.join(GULP_CONFIG['tasks'][type]['dest'], path) : path
The File.join method expect strings so either the GULP_CONFIG['tasks'][type]['dest'] or path is not a string but a symbol. Try something like this:
asset_path = type ? File.join(GULP_CONFIG['tasks'][type.to_s]['dest'].to_s, path.to_s) : path.to_s
In my app when user add object it execute one API request to third party site. For single user is ok to send the request immediately. But API calls are limited by 3 per second. So, when qtt of users will increase it'll be a trouble. I suggested that ActiveJob + Sidekiq will do the work, but how to get results when job is done?
My code for one user:
controller.rb
def create
wall = get_request(code_constructor({params for constructor },[]))
end
module CommonMods
def code_constructor(meth_name, meth_params, code)
param_str = []
meth_params.each_pair do |name, value|
param_str << '"' + name.to_s + '":'+ value.to_s
end
param_str = param_str.join(',')
code << meth_name + '({' + param_str + '})'
end
def get_request(code)
i = 0
result = []
while i < code.size
part_code = code.slice(i, i + 25)
if part_code.size >1
part_code = part_code.join(',')
else
part_code = part_code[0]
end
url='https://api.site.com/method/'
uri = URI.parse(url)
parameters = {'code' => "return [#{part_code}];"}
response = Net::HTTP.post_form(uri, parameters)
result = result + JSON.parse(response.body)['response']
i += 25
end
result
end
end
How to execute get_request method in queue with code param and proceed with received response in the controller? Or maybe it have different way to make it?
I have a ruby controller
def new
counter = 1
fileW = File.new("query_output.txt", "w")
file = File.new("query_data.txt", "r")
while (line = file.gets)
puts "#{counter}: #{line}"
query = "select name,highway from planet_osm_line where name ilike '" +line+"'"
#output = PlanetOsmLine.connection.execute(query)
#output.each do |output|
fileW.write(output['highway'] + "\n")
end
counter = counter + 1
end
file.close
query = ""
#output = PlanetOsmLine.connection.execute(query)
end
So in this I am reading from a file like
%12th%main%
%100 feet%
%12th%main%
%12th%main%
%12th%main%
%100 feet%
In the ruby console I can see all the queries getting executed but in query_output.txt I only get the output of last query. What am I doing wrong here?
You use filemode w which will re-create the output file every time (so you will write into an empty file). Instead open your file as follows:
fileW = File.new("query_output.txt", "a")
a stands for append. It will open or create the file, and append at the back.
For more info concerning file-modes: http://pubs.opengroup.org/onlinepubs/009695399/functions/fopen.html
Im having problems trying to display in my view information comming from a json file. I have already parse it.
Here is my error:
When assigning attributes, you must pass a hash as an argument.
Extracted source (around line #23):
21 # #new_member.constructors = [driver['Constructors'][0]['name']]
22 # #new_member.points = [driver['points']]
23 #new_member.from_json(json)
#members << #new_member
end
# #new_member.constructors = [driver['Constructors'][0]['name']]
# #new_member.points = [driver['points']]
#new_member.from_json(json)
#members << #new_member
end
This is my controller:
require 'open-uri'
require 'json'
url = "http://ergast.com/api/f1/2014/driverStandings.json"
data = JSON.parse(open(url).read)
standings = data['MRData']['StandingsTable']['StandingsLists'][0]['DriverStandings']
#members = Array.new
standings.each do |driver|
json = standings.to_json
#new_member = Member.new
# #new_member.position = [driver['position']]
# #new_member.givenName = [driver['Driver']['givenName']]
# #new_member.familyName = [driver['Driver']['familyName']]
# #new_member.constructors = [driver['Constructors'][0]['name']]
# #new_member.points = [driver['points']]
#new_member.from_json(json)
#members << #new_member
If I uncommented the lines in the controller and delete these lines
#new_member.from_json(json)
json = standings.to_json
I get the following in the view
Name: ["Lewis"]
Name: ["Nico"]
That view is really close but is not what i need, I need the data without [" "],
so the view that I need is something like:
1 Lewis Hamilton Mercedes 384
Thanks in advance.
Alternative 1
Change the line:
#new_member.givenName = [driver['Driver']['givenName']]
into:
#new_member.givenName = driver['Driver']['givenName']
removing the external [ and ], so that the field #new_member.givenName that you're populating becomes a string rather than an array containing one string.
Alternative 2
Is it possible that, when looping through the standings hash, the driver temp variable is a Hash?
In this case it may be sufficient to do:
#members = Array.new
standings.each do |driver|
#members << Member.new(driver)
end
Bonus: you probably don't need to assign an instance variable #new_member, that gets overwritten at each iteration of the loop