Class: GamesRadar::Game

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

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

#descriptionObject (readonly)

The game description



19
20
21
# File 'lib/games_radar/game.rb', line 19

def description
  @description
end

#genreObject (readonly)

The game genre. Is a GamesRadar::Genre instance.



16
17
18
# File 'lib/games_radar/game.rb', line 16

def genre
  @genre
end

#idObject (readonly)

The game id



7
8
9
# File 'lib/games_radar/game.rb', line 7

def id
  @id
end

#nameObject (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

#platformObject (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 from a to z or 0-9.

  • :genre: set the game genre. Can be all or any value from GamesRadar::Genre::GENRES.

  • :page: set the current page.

  • :size: set the page size. Can be 5 or multiples of 10 up to 50.

  • :platform: set the console platform. Can be all or any code returned by GamesRadar::Platform#code

  • :sort: specify how the result will be sorted. The available options are newest, oldest, updated, asc and desc.

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(options = {})
  options = {
    :page => 1,
    :size => GamesRadar::Config.page_size,
    :sort => :newest
  }.merge(options)

  request "/games", options 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 => options[: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
  options = {
    :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
  options[:genre] = GamesRadar::Genre.new(xml.at("genre > id").text) if xml.at("genre > id")
  options[:description] = Nokogiri(xml.at("description").text).text if xml.at("description")

  new(options)
end

Instance Method Details

#screenshots(options = {}) ⇒ Object

Retrieve the screenshot list. Return a GamesRadar::Result instance.

Options

  • :region: filter screenshots by region. Can be us or uk.

  • :page: set the current page.

  • :size: set the page size. Can be 5 or multiples of 10 up to 50.



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(options = {})
  options = {
    :region => :us,
    :page => 1,
    :size => GamesRadar::Config.page_size
  }.merge(options)

  request "/game/screenshots/:id", options.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 => options[:size],
      :count     => total_rows
    )
  end
end

#titleObject

Return the US game as main title.



109
110
111
# File 'lib/games_radar/game.rb', line 109

def title
  name[:us]
end