games_radar 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/games_radar/game.rb +15 -10
- data/lib/games_radar/request.rb +12 -3
- data/lib/games_radar/version.rb +1 -1
- data/test/request_test.rb +6 -0
- metadata +2 -2
data/lib/games_radar/game.rb
CHANGED
@@ -35,8 +35,8 @@ module GamesRadar
|
|
35
35
|
}.merge(options)
|
36
36
|
|
37
37
|
request "/games", options do |xml|
|
38
|
-
total_rows = xml.at("total_rows").
|
39
|
-
items = xml.search("game").collect {|node|
|
38
|
+
total_rows = xml.at("total_rows").text.to_i
|
39
|
+
items = xml.search("game").collect {|node| initialize_with_node node }
|
40
40
|
|
41
41
|
GamesRadar::Result.new(
|
42
42
|
:items => items,
|
@@ -50,16 +50,21 @@ module GamesRadar
|
|
50
50
|
def self.initialize_with_node(xml) # :nodoc:
|
51
51
|
raise GamesRadar::GameNotFoundError unless xml.at("id")
|
52
52
|
|
53
|
-
|
54
|
-
|
53
|
+
# First set up mandatory attributes
|
54
|
+
options = {
|
55
|
+
:id => xml.at("id").text,
|
55
56
|
:name => {
|
56
|
-
:us => xml.at("name > us").
|
57
|
-
:uk => xml.at("name > uk").
|
57
|
+
:us => xml.at("name > us").text,
|
58
|
+
:uk => xml.at("name > uk").text
|
58
59
|
},
|
59
|
-
:platform => GamesRadar::Platform.new(xml.at("platform > id").
|
60
|
-
|
61
|
-
|
62
|
-
|
60
|
+
:platform => GamesRadar::Platform.new(xml.at("platform > id").text)
|
61
|
+
}
|
62
|
+
|
63
|
+
# Then set up optional attributes
|
64
|
+
options[:genre] = GamesRadar::Genre.new(xml.at("genre > id").text) if xml.at("genre > id")
|
65
|
+
options[:description] = Nokogiri(xml.at("description").text).text if xml.at("description")
|
66
|
+
|
67
|
+
new(options)
|
63
68
|
end
|
64
69
|
|
65
70
|
# Return a game with the specified id.
|
data/lib/games_radar/request.rb
CHANGED
@@ -24,6 +24,14 @@ module GamesRadar
|
|
24
24
|
:filter => :game_name
|
25
25
|
}
|
26
26
|
|
27
|
+
class << self
|
28
|
+
# Hold the latest response.
|
29
|
+
attr_accessor :response
|
30
|
+
|
31
|
+
# Hold the latest uri.
|
32
|
+
attr_accessor :uri
|
33
|
+
end
|
34
|
+
|
27
35
|
# Make a API request processing the provided parameters and
|
28
36
|
# merging the API key in every request.
|
29
37
|
#
|
@@ -33,8 +41,9 @@ module GamesRadar
|
|
33
41
|
# # do whatever you want with the response XML
|
34
42
|
# end
|
35
43
|
def request(path, params = {}, &block)
|
36
|
-
uri = uri_for(path, params.dup)
|
37
|
-
|
44
|
+
GamesRadar::Request.uri = uri_for(path, params.dup)
|
45
|
+
GamesRadar::Request.response = open(GamesRadar::Request.uri)
|
46
|
+
xml = Nokogiri::XML(GamesRadar::Request.response.read)
|
38
47
|
|
39
48
|
handle_exceptions! xml
|
40
49
|
|
@@ -43,7 +52,7 @@ module GamesRadar
|
|
43
52
|
|
44
53
|
# Raise exception when XML contains known error codes
|
45
54
|
def handle_exceptions!(xml) # :nodoc:
|
46
|
-
case xml.search("error > code").
|
55
|
+
case xml.search("error > code").text.to_i
|
47
56
|
when 10001 then raise GamesRadar::InvalidApiKeyError
|
48
57
|
end
|
49
58
|
end
|
data/lib/games_radar/version.rb
CHANGED
data/test/request_test.rb
CHANGED
@@ -29,4 +29,10 @@ class GamesRadar::GenreTest < Test::Unit::TestCase
|
|
29
29
|
@request.prepare_params!(options)
|
30
30
|
assert_equal "sport+games", options[:genre]
|
31
31
|
end
|
32
|
+
|
33
|
+
def test_response_object
|
34
|
+
register_uri "/game/20060713144458560042?api_key=SxKuVse22qqUcZXq", "game.xml"
|
35
|
+
@request.request("/game/20060713144458560042") do |xml|
|
36
|
+
end
|
37
|
+
end
|
32
38
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: games_radar
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Nando Vieira
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2010-02-
|
12
|
+
date: 2010-02-17 00:00:00 -02:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|