Class: IGDB::Client
- Inherits:
-
Object
- Object
- IGDB::Client
- Defined in:
- lib/igdb_client.rb
Constant Summary collapse
- URL =
"https://api.igdb.com/v4/"- HEADER =
{"Accept" => "application/json"}
Instance Attribute Summary collapse
-
#client_id ⇒ Object
Returns the value of attribute client_id.
-
#endpoint ⇒ Object
Returns the value of attribute endpoint.
-
#token ⇒ Object
Returns the value of attribute token.
Instance Method Summary collapse
- #get(params = {fields: '*'}) ⇒ Object
- #id(id, params = {fields: '*'}) ⇒ Object
-
#initialize(client_id, token, endpoint = 'games') ⇒ Client
constructor
A new instance of Client.
- #method_missing(m, *args, &block) ⇒ Object
- #search(title, params = {fields: '*'}) ⇒ Object
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_id ⇒ Object
Returns the value of attribute client_id.
10 11 12 |
# File 'lib/igdb_client.rb', line 10 def client_id @client_id end |
#endpoint ⇒ Object
Returns the value of attribute endpoint.
10 11 12 |
# File 'lib/igdb_client.rb', line 10 def endpoint @endpoint end |
#token ⇒ Object
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 |