Class: IGDB::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/igdb_client.rb

Constant Summary collapse

URL =
"https://api.igdb.com/v4/"
HEADER =
{"Accept" => "application/json"}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client_id, token, endpoint = 'games') ⇒ Client

Returns a new instance of Client.



12
13
14
15
16
# File 'lib/igdb_client.rb', line 12

def initialize(client_id, token, endpoint = 'games')
   @client_id = client_id
   @token = token
   @endpoint = endpoint
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *args, &block) ⇒ Object



41
42
43
# File 'lib/igdb_client.rb', line 41

def method_missing(m, *args, &block)
   self.class.new(@client_id, @token, m.to_s)
end

Instance Attribute Details

#client_idObject

Returns the value of attribute client_id.



10
11
12
# File 'lib/igdb_client.rb', line 10

def client_id
  @client_id
end

#endpointObject

Returns the value of attribute endpoint.



10
11
12
# File 'lib/igdb_client.rb', line 10

def endpoint
  @endpoint
end

#tokenObject

Returns the value of attribute token.



10
11
12
# File 'lib/igdb_client.rb', line 10

def token
  @token
end

Instance Method Details

#get(params = {fields: '*'}) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/igdb_client.rb', line 18

def get(params = {fields: '*'})
   uri = URI.parse(URL+@endpoint)
   data = params.map do |k,v|
     "#{k.to_s} #{v};"
   end.join('')
   response = Net::HTTP.post(uri,
                             data,
                             HEADER.merge({'Client-ID' => self.client_id,
                                           'Authorization' => "Bearer " + self.token}))
   response.value
   JSON.parse(response.body, object_class: OpenStruct)
end

#id(id, params = {fields: '*'}) ⇒ Object



36
37
38
39
# File 'lib/igdb_client.rb', line 36

def id(id, params = {fields: '*'})
   params[:where] = "id = #{id}"
   get params
end

#search(title, params = {fields: '*'}) ⇒ Object



31
32
33
34
# File 'lib/igdb_client.rb', line 31

def search(title, params = {fields: '*'})
   params[:search] = '"' + title + '"'
   get params
end