Deep Rails JSON parse [closed] - ruby-on-rails

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
Is it possible to have JSON.parse work 2 layers deep so that a hash within a hash will get parsed as well? Is there a method for or do I have to do something like JSON.parse(JSON.parse(...)['foo'])?

JSON.parse doesn't care about your hash structure:
> str = JSON.dump({foo: {bar: {baz: :qux}}})
=> "{\"foo\":{\"bar\":{\"baz\":\"qux\"}}}"
> p = JSON.parse(str).with_indifferent_access
=> {"foo"=>{"bar"=>{"baz"=>"qux"}}}
> p[:foo][:bar][:baz]
=> "qux"
(Well, it cares if you have a malformed string, but that's something else altogether.)
So, what are you asking?

Related

How to use or in checking presence? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
i need to check presence of a value in an array. It can be two values i need to check with an or case if it includes in the array. I think it will be confusing from my description you will understand after you see the code below.
if user.sheet_ids.include? params[:id].to_i || params[:sheet_id].to_i
can :show, Project
end
It returns false even if sheet_id is present in the array of sheet_ids.
user.sheet_ids is an array and i need to check if any of the value includes in this array. The method i used is not seems to be working.
Is there any simple way of doing this. Thanks.
I would do something like this:
(user.sheet_ids & params.values_at(:id, :sheet_id).map(&:to_i)).any?
user.sheet_ids.include? params[:id].to_i || params[:sheet_id] is executed in this way user.sheet_ids.include?(params[:id].to_i || params[:sheet_id]). The OR statement is executed and the result is what is passed as an argument to user.sheet_ids.include?
To check if either params[:id].to_i or params[:sheet_id] is included in the array users.sheet_ids, you need to do this;
if user.sheet_ids.include?(params[:id].to_i) || user.sheet_ids.include?(params[:sheet_id].to_i)
can :show, Project
end

Resolve Fixnum error [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I have a parameter (params[:authorization]) that comes from the URL, as you can see:
authorization_selected = params[:authorization]
new_parcel = params[:new_parcel].to_i
puts authorization_selected.class (in the console show type String)
puts new_parcel.class (in the console show type Fixnum)
In my controller, have:
#portability = Portability.new
#portability.employee_id = authorization_selected.employee_id
However this returns an error:
undefined method `employee_id' for 3:Fixnum
I need that both was integer. How do it?
You are calling the employee_id method on authorization_selectedwhich is a String and does not provide this method.
Obviously this does not work. You probably want to do
#portability = Portability.new
#portability.employee_id = authorization_selected
assuming that params[:employee] contains the employee_id and Portability is an ActiveModel or an ActiveRecord.
Perhaps you can change your form that the value can be assigned through the initializer?

How to turn a string into URL in rails [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
If I have a form input where someone puts 'facebook.com' or 'google.com' it turns it into a valid URL adding http:// so that rails can use it. What I want is to have a form where you input a URL and it grabs the 3 most used words at that site and shows them in a list on the results page. This list should be accessible later, so I also need to store those words with that URL
def smart_add_url_protocol
url = Url.find_by(params[:url])
unless self.url[/\Ahttp:\/\//] || self.url[/\Ahttps:\/\//]
self.url = "http://#{self.url}"
end
end
something like this.
You can use URI::HTTP#build like so:
URI::HTTP.build(host: 'facebook.com').to_s
#=> "http://facebook.com"
I recommend the Addressable gem which is somewhat smart about guessing URLs from strings:
require "addressable/uri"
Addressable::URI.heuristic_parse(some_string_with_an_url).to_s

Handling Ruby Standard Error? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I'm using the FedEx gem to pull down tracking numbers, the issue I'm running into is that if a tracking number has been generated, but not picked up, the gem returns a Fedex::RateError
According to the documentation Fedex:RateError inherits from StandardError I need to display a message if this error trips on the frontend site
I looked around online and found some ideas, but was hoping someone can point me in the right direction for handling errors, I'm using Rails 4.
Thanks for any suggestions
begin
# Do your normal happy path view stuff here
rescue Fedex:RateError => error
# Do your display of the error to the user here
end
Thanks msergeant, that did it!
Here's my final code- this recovery isn't very well documented and seems like a handful of people we're looking for it.
begin
results = fedex.track(:tracking_number => tracking)
rescue Fedex::RateError
statusImg = "http://t2.gstatic.com/images?q=tbn:ANd9GcRuKAIYZ2mNLsjRulsH05zNwF93jmAdpgZGSgtVN8XiT7_SWw285g"
statusRet = "Nope!!"
#trkResultTxt = statusRet
#trkResultImg = statusImg
else
tracking_info = results.first
#trkStatus = tracking_info.status
end

How to get a ruby class when it's name is in a variable? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 9 years ago.
Improve this question
So I'm passing a string with the name of an activerecord model class into my method and I want to be able to call some methods on that class.
This is wrong:
def perform(body_class, body_id)
body = body_class.constantize
request = body.find(body_id)
end
Update - that actually works fine.
What error are you getting? Make sure the string is in the correct ActiveRecord format.
For example:
"Person".constantize # works
"person".constantize # does not work
To get the second one to work you should also add camelize
"person".camelize.constantize # works

Resources