Class: ApiSports::Resource

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ Resource

Returns a new instance of Resource.



7
8
9
# File 'lib/api_sports/resource.rb', line 7

def initialize(client)
  @client = client
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



5
6
7
# File 'lib/api_sports/resource.rb', line 5

def client
  @client
end

Instance Method Details

#get_request(url, params: {}, headers: {}) ⇒ Object



11
12
13
# File 'lib/api_sports/resource.rb', line 11

def get_request(url, params: {}, headers: {})
  handle_response client.connection.get(url, params, headers)
end

#get_single_resource(url, params: {}, headers: {}) {|response| ... } ⇒ Object

Yields:

  • (response)


15
16
17
18
# File 'lib/api_sports/resource.rb', line 15

def get_single_resource(url, params: {}, headers: {}, &dig_block)
  response = get_request(url, params: params, headers: headers)
  yield(response)
end

#handle_response(response) ⇒ Object



20
21
22
23
24
25
26
27
28
# File 'lib/api_sports/resource.rb', line 20

def handle_response(response)
  errors = response.body["errors"]

  if errors.empty?
    response
  else
    raise Error, errors
  end
end