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)



135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/google_custom_search_api.rb', line 135

def method_missing(*args)
  name = args[0].to_s
  return self[name] if has_key? name

  camelname = name.split('_').map do |w|
    "#{w[0,1].upcase}#{w[1..-1]}"
  end.join("")

  if has_key? camelname
    self[camelname]
  else
    super *args
  end
end