Class: GamesRadar::Game
- Inherits:
-
Object
- Object
- GamesRadar::Game
- Extended by:
- Request
- Includes:
- Request
- Defined in:
- lib/games_radar/game.rb
Constant Summary
Constants included from Request
Request::BASE_URL, Request::GENRE, Request::PARAMS, Request::SORT
Instance Attribute Summary collapse
-
#description ⇒ Object
readonly
The game description.
-
#genre ⇒ Object
readonly
The game genre.
-
#id ⇒ Object
readonly
The game id.
-
#name ⇒ Object
readonly
The game name.
-
#platform ⇒ Object
readonly
The console platform.
Class Method Summary collapse
-
.all(options = {}) ⇒ Object
Retrieve the game list.
-
.find(id) ⇒ Object
Return a game with the specified id.
-
.initialize_with_node(xml) ⇒ Object
Initialize a GamesRadar::Game object with a game node.
Instance Method Summary collapse
-
#initialize(attrs = {}) ⇒ Game
constructor
Initialize a new game object and set the hash to its attributes.
-
#screenshots(options = {}) ⇒ Object
Retrieve the screenshot list.
-
#title ⇒ Object
Return the US game as main title.
Methods included from Request
handle_exceptions!, prepare_named_params!, prepare_params!, request, to_query_string, uri_for
Constructor Details
#initialize(attrs = {}) ⇒ Game
Initialize a new game object and set the hash to its attributes.
game = GamesRadar::Game.new(
:id => "2005120717014294613647",
:name => "Grand Theft Auto: San Andreas"
)
102 103 104 105 106 |
# File 'lib/games_radar/game.rb', line 102 def initialize(attrs = {}) attrs.each do |name, value| instance_variable_set("@#{name}", value) end end |
Instance Attribute Details
#description ⇒ Object (readonly)
The game description
19 20 21 |
# File 'lib/games_radar/game.rb', line 19 def description @description end |
#genre ⇒ Object (readonly)
The game genre. Is a GamesRadar::Genre instance.
16 17 18 |
# File 'lib/games_radar/game.rb', line 16 def genre @genre end |
#id ⇒ Object (readonly)
The game id
7 8 9 |
# File 'lib/games_radar/game.rb', line 7 def id @id end |
#name ⇒ Object (readonly)
The game name. The hash contains both US and UK names.
10 11 12 |
# File 'lib/games_radar/game.rb', line 10 def name @name end |
#platform ⇒ Object (readonly)
The console platform. Is a GamesRadar::Platform instance.
13 14 15 |
# File 'lib/games_radar/game.rb', line 13 def platform @platform end |
Class Method Details
.all(options = {}) ⇒ Object
Retrieve the game list. Return a GamesRadar::Result instance.
Options
-
:filter
: filter the results by title. You can specify a letter froma
toz
or0-9
. -
:genre
: set the game genre. Can beall
or any value from GamesRadar::Genre::GENRES. -
:page
: set the current page. -
:size
: set the page size. Can be5
or multiples of10
up to50
. -
:platform
: set the console platform. Can beall
or any code returned by GamesRadar::Platform#code -
:sort
: specify how the result will be sorted. The available options arenewest
,oldest
,updated
,asc
anddesc
.
Examples
result = GamesRadar::Game.all
result = GamesRadar::Game.all(:filter => 'a')
result = GamesRadar::Game.all(:filter => '0-9')
result = GamesRadar::Game.all(:genre => :sports)
result = GamesRadar::Game.all(:page => 1)
result = GamesRadar::Game.all(:platform => :ps3)
result = GamesRadar::Game.all(:sort => :asc)
result.each do |game|
puts game.title
end
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/games_radar/game.rb', line 45 def self.all( = {}) = { :page => 1, :size => GamesRadar::Config.page_size, :sort => :newest }.merge() request "/games", do |xml| total_rows = xml.at("total_rows").text.to_i items = xml.search("game").collect {|node| initialize_with_node node } GamesRadar::Result.new( :items => items, :page_size => [:size], :count => total_rows ) end end |
.find(id) ⇒ Object
Return a game with the specified id.
game = GamesRadar::Game.find("2005120717014294613647")
89 90 91 92 93 |
# File 'lib/games_radar/game.rb', line 89 def self.find(id) request "/game/:id", :id => id do |xml| Game.initialize_with_node(xml.search("game")) end end |
.initialize_with_node(xml) ⇒ Object
Initialize a GamesRadar::Game object with a game node.
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/games_radar/game.rb', line 65 def self.initialize_with_node(xml) # :nodoc: raise GamesRadar::GameNotFoundError unless xml.at("id") # First set up mandatory attributes = { :id => xml.at("id").text, :name => { :us => xml.at("name > us").text, :uk => xml.at("name > uk").text }, :platform => GamesRadar::Platform.new(xml.at("platform > id").text) } # Then set up optional attributes [:genre] = GamesRadar::Genre.new(xml.at("genre > id").text) if xml.at("genre > id") [:description] = Nokogiri(xml.at("description").text).text if xml.at("description") new() end |
Instance Method Details
#screenshots(options = {}) ⇒ Object
Retrieve the screenshot list. Return a GamesRadar::Result instance.
Options
-
:region
: filter screenshots by region. Can beus
oruk
. -
:page
: set the current page. -
:size
: set the page size. Can be5
or multiples of10
up to50
.
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/games_radar/game.rb', line 120 def screenshots( = {}) = { :region => :us, :page => 1, :size => GamesRadar::Config.page_size }.merge() request "/game/screenshots/:id", .merge(:id => id) do |xml| total_rows = xml.at("total_rows").text.to_i items = xml.search("screenshot").collect {|node| GamesRadar::Screenshot.initialize_with_node node } GamesRadar::Result.new( :items => items, :page_size => [:size], :count => total_rows ) end end |
#title ⇒ Object
Return the US game as main title.
109 110 111 |
# File 'lib/games_radar/game.rb', line 109 def title name[:us] end |