Class: GoogleCustomSearchApi::ResponseData

Inherits:
Hash
  • Object
show all
Defined in:
lib/google_custom_search_api.rb

Overview

Convenience wrapper for the response Hash. Converts keys to Strings. Crawls through all member data and converts any other Hashes it finds. Provides access to values through method calls, which will convert underscored to camel case.

Usage:

rd = ResponseData.new("AlphaBeta" => 1, "Results" => {"Gamma" => 2, "delta" => [3, 4]})
puts rd.alpha_beta
=> 1
puts rd.alpha_beta.results.gamma
=> 2
puts rd.alpha_beta.results.delta
=> [3, 4]

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(*args) ⇒ Object (private)



89
90
91
92
93
94
95
96
97
98
# File 'lib/google_custom_search_api.rb', line 89

def method_missing(*args)
  name = args[0].to_s
  return self[name] if has_key? name
  camelname = name.split('_').map {|w| "#{w[0,1].upcase}#{w[1..-1]}" }.join("")
  if has_key? camelname
    self[camelname]
  else
    super *args
  end
end