Module: GamesRadar::Request
Constant Summary collapse
- BASE_URL =
The base url points to the latest API version.
"http://api.gamesradar.com"
- GENRE =
Genre values that should be replaced for a more textual equivalent.
{ :children => "children's", :other => "other games/compilations", :rpg => "role playing", :sports => "sport games" }
- SORT =
Sort values that should be replaced for the expected API value.
{ :asc => "a-z", :desc => "z-a" }
- PARAMS =
Replace pretty params name for its ugly counter-part.
{ :page => :page_num, :size => :page_size, :filter => :game_name }
Class Attribute Summary collapse
-
.response ⇒ Object
Hold the latest response.
-
.uri ⇒ Object
Hold the latest uri.
Instance Method Summary collapse
-
#handle_exceptions!(xml) ⇒ Object
Raise exception when XML contains known error codes.
-
#prepare_named_params!(path, params) ⇒ Object
Replace a named param for its value on the specified path.
-
#prepare_params!(params) ⇒ Object
Modify the specified params hash by escaping each of its values.
-
#request(path, params = {}) {|xml| ... } ⇒ Object
Make a API request processing the provided parameters and merging the API key in every request.
-
#to_query_string(params) ⇒ Object
Convert to query string.
-
#uri_for(path, params) ⇒ Object
Generate a URI based on the specified path and params.
Class Attribute Details
.response ⇒ Object
Hold the latest response.
29 30 31 |
# File 'lib/games_radar/request.rb', line 29 def response @response end |
.uri ⇒ Object
Hold the latest uri.
32 33 34 |
# File 'lib/games_radar/request.rb', line 32 def uri @uri end |
Instance Method Details
#handle_exceptions!(xml) ⇒ Object
Raise exception when XML contains known error codes
54 55 56 57 58 |
# File 'lib/games_radar/request.rb', line 54 def handle_exceptions!(xml) # :nodoc: case xml.search("error > code").text.to_i when 10001 then raise GamesRadar::InvalidApiKeyError end end |
#prepare_named_params!(path, params) ⇒ Object
Replace a named param for its value on the specified path.
106 107 108 109 110 111 |
# File 'lib/games_radar/request.rb', line 106 def prepare_named_params!(path, params) # :nodoc: path.gsub!(/(:[a-z0-9]+)/) do |param| param = param.gsub(/:/, "").to_sym params.delete(param) end end |
#prepare_params!(params) ⇒ Object
Modify the specified params hash by escaping each of its values. Also merge the API key into this params hash.
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/games_radar/request.rb', line 80 def prepare_params!(params) # :nodoc: raise GamesRadar::InvalidApiKeyError unless GamesRadar::Config.api_key params.merge!(:api_key => GamesRadar::Config.api_key) params.dup.each do |name, value| name = PARAMS[name.to_sym] || name.to_sym value = value.to_s.to_sym case name when :genre then value = GENRE[value] if GENRE[value] when :sort then value = SORT[value] if SORT[value] end params.merge!(name => CGI.escape(value.to_s)) end # Remove options that are replaced [:page, :size, :filter].each do |param| params.delete(param) && params.delete(param.to_s) end end |
#request(path, params = {}) {|xml| ... } ⇒ Object
Make a API request processing the provided parameters and merging the API key in every request.
include GamesRadar::Request
request "/game", :id => "2005120717014294613647" do |xml|
# do whatever you want with the response XML
end
43 44 45 46 47 48 49 50 51 |
# File 'lib/games_radar/request.rb', line 43 def request(path, params = {}, &block) GamesRadar::Request.uri = uri_for(path, params.dup) GamesRadar::Request.response = open(GamesRadar::Request.uri) xml = Nokogiri::XML(GamesRadar::Request.response.read) handle_exceptions! xml yield xml end |
#to_query_string(params) ⇒ Object
Convert to query string. Value is already escaped by the prepare_params!
method.
70 71 72 73 74 75 76 |
# File 'lib/games_radar/request.rb', line 70 def to_query_string(params) # :nodoc: params = params.collect do |name, value| "%s=%s" % [CGI.escape(name.to_s), value] end params.join("&") end |
#uri_for(path, params) ⇒ Object
Generate a URI based on the specified path and params.
61 62 63 64 65 66 |
# File 'lib/games_radar/request.rb', line 61 def uri_for(path, params) # :nodoc: prepare_params!(params) prepare_named_params!(path, params) URI.parse [File.join(BASE_URL, path), to_query_string(params)].join("?") end |