Class: Gamesdb::Client::Real

Inherits:
Object
  • Object
show all
Defined in:
lib/gamesdb/client.rb,
lib/gamesdb/requests/get_game.rb,
lib/gamesdb/requests/list_games.rb,
lib/gamesdb/requests/get_platform.rb,
lib/gamesdb/requests/list_platforms.rb,
lib/gamesdb/requests/get_platform_games.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Real

Returns a new instance of Real.



20
21
22
23
24
# File 'lib/gamesdb/client.rb', line 20

def initialize(options={})
  url = options[:url] || 'http://thegamesdb.net/api'

  @connection = RestClient::Resource.new(url) #Excon.new(url)
end

Instance Attribute Details

#connectionObject (readonly)

Returns the value of attribute connection.



18
19
20
# File 'lib/gamesdb/client.rb', line 18

def connection
  @connection
end

Instance Method Details

#get_game(id) ⇒ Object



3
4
5
6
7
8
# File 'lib/gamesdb/requests/get_game.rb', line 3

def get_game(id)
  request(
    :path => "/GetGame.php",
    :query => {"id" => id}
  )
end

#get_platform(id) ⇒ Object



3
4
5
6
7
8
# File 'lib/gamesdb/requests/get_platform.rb', line 3

def get_platform(id)
  request(
    :path => "/GetPlatform.php",
    :query => {"id" => id}
  )
end

#get_platform_games(id) ⇒ Object



3
4
5
6
7
8
# File 'lib/gamesdb/requests/get_platform_games.rb', line 3

def get_platform_games(id)
  request(
    :path => "/GetPlatformGames.php",
    :query => {"platform" => id}
  )
end

#list_games(search) ⇒ Object



3
4
5
6
7
8
# File 'lib/gamesdb/requests/list_games.rb', line 3

def list_games(search)
  request(
    :path => "/GetGamesList.php",
    :query => {"name" => search},
  )
end

#list_platformsObject



3
4
5
6
7
# File 'lib/gamesdb/requests/list_platforms.rb', line 3

def list_platforms
  request(
    :path => "/GetPlatformsList.php"
  )
end

#request(options) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/gamesdb/client.rb', line 26

def request(options)
  path    = options[:path]
  method  = options[:method]        || :get
  method  = method.to_sym
  headers = options[:headers]       || {}
  body    = options[:body]          || nil
  expects = options[:expects]       || 200
  query   = options[:query]         || {}

  connect_hash = {
    :method  => method,
    :headers => headers,
    :path    => path,
    :expects => expects,
    :body    => body,
    #:query   => query
  }

  path = "#{path}?#{query.map{|k,v| "#{URI.escape(k.to_s)}=#{URI.escape(v.to_s)}"}.join("&")}" unless query.empty?
  response = case method
             when :get
               connection[path].get(headers)
             when :post
               connection[path].post(body, headers)
             when :delete
               connection[path].delete(headers)
             end

  document = Gamesdb::SaxDocument.new
  parser = Nokogiri::XML::SAX::PushParser.new(document)
  parser << response
  parser.finish
  document.body
end