Class: Sports::Butler::Api

Inherits:
ApiBase
  • Object
show all
Defined in:
lib/sports/butler/api.rb

Constant Summary

Constants inherited from ApiBase

Sports::Butler::ApiBase::MSG_INVALID_CONFIG

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ApiBase

error_message, invalid_config_result

Constructor Details

#initialize(sport, api_name) ⇒ Api

Returns a new instance of Api.



12
13
14
15
16
17
18
19
20
21
# File 'lib/sports/butler/api.rb', line 12

def initialize(sport, api_name)
  @sport    = sport
  @api_name = api_name

  @response = nil
  @success  = false
  @errors   = []

  @response_code = nil
end

Instance Attribute Details

#api_nameObject

Returns the value of attribute api_name.



9
10
11
# File 'lib/sports/butler/api.rb', line 9

def api_name
  @api_name
end

#errorsObject

Returns the value of attribute errors.



9
10
11
# File 'lib/sports/butler/api.rb', line 9

def errors
  @errors
end

#headersObject

Returns the value of attribute headers.



9
10
11
# File 'lib/sports/butler/api.rb', line 9

def headers
  @headers
end

#parsed_responseObject

Returns the value of attribute parsed_response.



9
10
11
# File 'lib/sports/butler/api.rb', line 9

def parsed_response
  @parsed_response
end

#queryObject

Returns the value of attribute query.



9
10
11
# File 'lib/sports/butler/api.rb', line 9

def query
  @query
end

#responseObject

Returns the value of attribute response.



9
10
11
# File 'lib/sports/butler/api.rb', line 9

def response
  @response
end

#response_codeObject

Returns the value of attribute response_code.



9
10
11
# File 'lib/sports/butler/api.rb', line 9

def response_code
  @response_code
end

#sportObject

Returns the value of attribute sport.



9
10
11
# File 'lib/sports/butler/api.rb', line 9

def sport
  @sport
end

#successObject

Returns the value of attribute success.



9
10
11
# File 'lib/sports/butler/api.rb', line 9

def success
  @success
end

#uriObject

Returns the value of attribute uri.



9
10
11
# File 'lib/sports/butler/api.rb', line 9

def uri
  @uri
end

#urlObject

Returns the value of attribute url.



9
10
11
# File 'lib/sports/butler/api.rb', line 9

def url
  @url
end

Class Method Details

.get(url:, sport: nil, api_name: nil, filters: {}, headers: {}) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/sports/butler/api.rb', line 70

def get(url:, sport: nil, api_name: nil, filters: {}, headers: {})

  if sport && api_name
    if Configuration.invalid_config?(sport, api_name)
      return Sports::Butler::ApiBase.invalid_config_result
    end

    headers  = Configuration.http_party_headers(sport, api_name)
    url      = Configuration.http_party_url_suffix(url, sport, api_name)
  end

  query   = filters || {}

  HTTParty.get "#{url}",
               headers: headers,
               query: query,
               format: :json
end

Instance Method Details

#error_message(error) ⇒ Object



55
56
57
# File 'lib/sports/butler/api.rb', line 55

def error_message(error)
  { message: error }.with_indifferent_access
end

#get(path:, filters: {}) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/sports/butler/api.rb', line 23

def get(path:, filters: {})
  if Configuration.invalid_config?(sport, api_name)
    @response = Sports::Butler::ApiBase.invalid_config_result
    @response_code = 400
    return false
  end

  @response           = process_http_party(path, filters)
  @response_code      = response.code
  @success            = true if response_code == 200
  @parsed_response    = process_response(response)
  @uri                = response.request.last_uri&.to_s

  true
end

#http_party_getObject



59
60
61
62
63
64
65
66
# File 'lib/sports/butler/api.rb', line 59

def http_party_get
  HTTParty.get(
    "#{url}",
    headers: headers,
    query: query,
    format: :json
  )
end

#process_http_party(path, filters) ⇒ Object



39
40
41
42
43
44
45
# File 'lib/sports/butler/api.rb', line 39

def process_http_party(path, filters)
  @headers  = Configuration.http_party_headers(sport, api_name)
  @url      = Configuration.http_party_url(path, sport, api_name)
  @query    = filters || {}

  http_party_get
end

#process_response(response) ⇒ Object



47
48
49
50
51
52
53
# File 'lib/sports/butler/api.rb', line 47

def process_response(response)
  if response.parsed_response.is_a?(Hash) && response.dig('message')
    error_message(response['message'])
  else
    response.parsed_response
  end
end