Accessing httparty API results in ruby - ruby

I'm trying to access the API results from HTTParty, but can't figure out how...I've stored the results in a variable called zillow that looks like the following:
=> {"searchresults"=>
{"request"=>{"address"=>"49 Alpine Way", "citystatezip"=>"28805"},
"message"=>{"text"=>"Request successfully processed", "code"=>"0"},
"response"=>
{"results"=>
{"result"=>
{"zpid"=>"5628657",
"links"=>
{"homedetails"=>"http://www.zillow.com/homedetails/49-Alpine-Way-Asheville-NC-28805/5628657_zpid/",
"graphsanddata"=>"http://www.zillow.com/homedetails/49-Alpine-Way-Asheville-NC-28805/5628657_zpid/#charts-and-data",
"mapthishome"=>"http://www.zillow.com/homes/5628657_zpid/",
"comparables"=>"http://www.zillow.com/homes/comps/5628657_zpid/"},
"address"=>
{"street"=>"49 Alpine Way",
"zipcode"=>"28805",
"city"=>"Asheville",
I'm trying to access "zpid" but keep getting nil as a response. I've tried the following:
[107] pry(main)> zillow["searchresults"]["zpid"]
=> nil
[108] pry(main)> zillow["zpid"]
=> nil
[109] pry(main)> zillow["searchresults"]["results"]["zpid"]
NoMethodError: undefined method `[]' for nil:NilClass
from (pry):100:in `<main>'
[110] pry(main)> zillow.find["zpid"]
NoMethodError: undefined method `[]' for #<Enumerator:0x0000000323b3f8>
from (pry):101:in `<main>'
[111] pry(main)> zillow.get["zpid"]
NoMethodError: undefined method `get' for #<HTTParty::Response:0x00000003adb0f8>
from /home/pjw/.rvm/gems/ruby-2.3.0/gems/httparty-0.14.0/lib/httparty/response.rb:85:in `method_missing'
[112] pry(main)> zillow["searchresults"]
=> {"request"=>{"address"=>"49 Alpine Way", "citystatezip"=>"28805"},
"message"=>{"text"=>"Request successfully processed", "code"=>"0"},
What am I missing?

You missed a chain of keys to get zpid
zillow["searchresults"]["response"]["results"]["result"]["zpid"]
If you on ruby 2.3.0 then you can use Hash#dig method

Related

Hanami: undefined method `size' for nil:NilClass

I keep learning hanami and ran into the following problem: when I initialize and check the parameters, I encounter the following error
Boot Error
Something went wrong while loading /Users/anewaccount/Projects/Mediateka/config.ru
NoMethodError: undefined method `size' for nil:NilClass
/Users/anewaccount/.rvm/gems/ruby-2.7.2/gems/dry-validation-0.11.0/lib/dry/validation/schema/deprecated.rb:11:in `input_processor'
/Users/anewaccount/.rvm/gems/ruby-2.7.2/gems/dry-validation-0.11.0/lib/dry/validation/schema/class_interface.rb:165:in `default_options'
/Users/anewaccount/.rvm/gems/ruby-2.7.2/gems/dry-validation-0.11.0/lib/dry/validation/schema/class_interface.rb:35:in `new'
/Users/anewaccount/.rvm/gems/ruby-2.7.2/gems/hanami-validations-1.3.7/lib/hanami/validations.rb:109:in `validations'
/Users/anewaccount/.rvm/gems/ruby-2.7.2/gems/hanami-controller-1.3.3/lib/hanami/action/params.rb:152:in `params'
/Users/anewaccount/.rvm/gems/ruby-2.7.2/gems/hanami-controller-1.3.3/lib/hanami/action/validatable.rb:100:in `block in params'
/Users/anewaccount/.rvm/gems/ruby-2.7.2/gems/hanami-controller-1.3.3/lib/hanami/action/validatable.rb:100:in `class_eval'
/Users/anewaccount/.rvm/gems/ruby-2.7.2/gems/hanami-controller-1.3.3/lib/hanami/action/validatable.rb:100:in `params'
/Users/anewaccount/Projects/Mediateka/apps/web/controllers/auth/sign_up.rb:11:in `<class:SignUp>'
/Users/anewaccount/Projects/Mediateka/apps/web/controllers/auth/sign_up.rb:9:in `<module:Auth>'
/Users/anewaccount/Projects/Mediateka/apps/web/controllers/auth/sign_up.rb:8:in `<module:Controllers>'
/Users/anewaccount/Projects/Mediateka/apps/web/controllers/auth/sign_up.rb:7:in `<module:Web>'
/Users/anewaccount/Projects/Mediateka/apps/web/controllers/auth/sign_up.rb:6:in `<top (required)>'
/Users/anewaccount/.rvm/gems/ruby-2.7.2/gems/hanami-utils-1.3.8/lib/hanami/utils.rb:56:in `require_relative'
/Users/anewaccount/.rvm/gems/ruby-2.7.2/gems/hanami-utils-1.3.8/lib/hanami/utils.rb:56:in `block in require!'
/Users/anewaccount/.rvm/gems/ruby-2.7.2/gems/hanami-utils-1.3.8/lib/hanami/utils.rb:94:in `each'
/Users/anewaccount/.rvm/gems/ruby-2.7.2/gems/hanami-utils-1.3.8/lib/hanami/utils.rb:94:in `for_each_file_in'
/Users/anewaccount/.rvm/gems/ruby-2.7.2/gems/hanami-utils-1.3.8/lib/hanami/utils.rb:56:in `require!'
my action:
# frozen_string_literal: true
require 'pry-byebug'
require_relative 'auth_base_action'
module Web
module Controllers
module Auth
class SignUp < AuthBaseAction
params do
required(:user).schema do
required(:first_name).filled(:str?)
required(:last_name).filled(:str?)
required(:email).filled(:str?, format?: /#/)
required(:password).filled(:str?).confirmation
end
end
def call params
binding.pry
end
end
end
end
end
I wrote the params block according to the example from the official documentation on the idea that it should work, but I catch NoMethodError: undefined method `size 'for nil: NilClass I also saw in the official documentation that these validations are delegated from Hanami :: Validations, I installed gem' hanami- validation 'according to their official documentation, but I had the same error, then I started looking at other people's code and they were successful using the example from the official documentation. Please tell me what is the error or please explain what am I doing wrong? I'm already very confused

Ruby - dry-rb - how to update an existing object's attributes?

Using dry-rb structs and types, I'm trying amend an object that has already been created, but can't seem to figure it out.
[3] pry(main)> class User < Dry::Struct
attribute :name, Types::String.optional
attribute :age, Types::Coercible::Integer
end
=> User
[4] pry(main)> user = User.new(name: nil, age: '21')
=> #<User name=nil age=21>
[5] pry(main)> user.name = "ted"
NoMethodError: undefined method `name=' for #<User name=nil age=21>
Did you mean? name
from (pry):10:in `__pry__'
[6] pry(main)> user(name: "Ted")
NoMethodError: undefined method `user' for main:Object
Did you mean? super
from (pry):11:in `__pry__'
[7] pry(main)> user[:name => "Ted"]
Dry::Struct::MissingAttributeError: Missing attribute: {:name=>"Ted"}
from /Users/me/.rvm/gems/ruby-2.6.5/gems/dry-struct-1.3.0/lib/dry/struct.rb:137:in `block in []'
[8] pry(main)> user('name' => 'Ted')
NoMethodError: undefined method `user' for main:Object
Did you mean? super
from (pry):13:in `__pry__'
Is this just not possible, or am I missing something super obvious? Any help is much appreciated

Figure out where a method was defined

If I follow the following part of this article:
Figure out where a method was defined
object = Object.new
puts object.method(:blank?).source_location
=> ["/gems/activesupport-5.0.0.beta1/lib/active_support/core_ext/object/blank.rb", 14]
I should be able to find the definition of the blank? method, however when I try this code within irb with ruby 2.0.0 I get this error message:
➜ ~ irb
irb(main):001:0> object = Object.new
=> #<Object:0x007fc84882f088>
irb(main):002:0> puts object.method(:blank?).source_location
NameError: undefined method `blank?' for class `Object'
from (irb):2:in `method'
from (irb):2
from /usr/bin/irb:12:in `<main>'
Did I miss anything?
Thank you.
.blank? method does not exist for a Object type. I know for sure it exists for a String method if I include the active_support lib
irb(main):001:0> String.new.method(:blank?).source_location
=> ["/home/xeon/.rbenv/versions/2.3.4/lib/ruby/gems/2.3.0/gems/activesupport-4.2.8/lib/active_support/core_ext/object/blank.rb", 116]
If you include activesupport-5.0.0.beta1 then it will work for you. (Looking at the source path of the article you have posted)

Where is Float#to_d?

Sometimes I see code using to_d. The ruby documentation even states there is a Float#to_d method. However, it's not in my version of ruby (ruby 1.9.3p263 (2012-08-23 revision 36792).
1.9.3p263 :001 > "0.0".to_d
NoMethodError: undefined method `to_d' for "0.0":String
from (irb):1
from /home/iblue/.rvm/rubies/ruby-1.9.3-head/bin/irb:16:in `<main>'
1.9.3p263 :002 > 0.0.to_d
NoMethodError: undefined method `to_d' for 0.0:Float
from (irb):2
from /home/iblue/.rvm/rubies/ruby-1.9.3-head/bin/irb:16:in `<main>'
1.9.3p263 :003 > 0.to_d
NoMethodError: undefined method `to_d' for 0:Fixnum
from (irb):3
from /home/iblue/.rvm/rubies/ruby-1.9.3-head/bin/irb:16:in `<main>'
No to_d in Float, String or Fixnum. What's going on?
As stated in the example of the documentation you need
require 'bigdecimal'
require 'bigdecimal/util'

How to reproduce undefined method error for NilClass

I want to find out how to reproduce the following error in Ruby 1.9:
NoMethodError (undefined method `[]' for nil:NilClass):
It's my own interest. The following doesn't work for me:
a = nil
a[:key]
It produce the following error:
You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.[]
Your code works on my machine (submitting an answer since I can't format the comments):
⇨ irb
1.9.3p194 :001 > a = nil
=> nil
1.9.3p194 :002 > a[:key]
NoMethodError: undefined method `[]' for nil:NilClass
from (irb):2
from /Users/bjc/.rvm/rubies/ruby-1.9.3-p194/bin/irb:16:in `<main>'

Resources