Class: Openlive::Base
- Inherits:
-
Object
- Object
- Openlive::Base
- Extended by:
- Forwardable
- Defined in:
- lib/openlive/base.rb
Instance Attribute Summary collapse
-
#api_data ⇒ Hash
A hash of data returned from the API.
-
#response ⇒ Object
Convenience method for accessing the API response data.
Class Method Summary collapse
-
.connection ⇒ Faraday::Connection
Faraday connection.
-
.handle_response(response, error_class: Openlive::Error, message: nil) {|Response| ... } ⇒ Object
Raise an exception or execute the following block, used for generic error handling for all routes.
-
.oauth ⇒ Openlive::OAuth
OAuth handler.
Instance Method Summary collapse
-
#connection ⇒ Faraday::Connection
Instance convenience method for the connection.
-
#initialize(data, response: nil) ⇒ Hash
constructor
Initialize an instance (used by subclasses) with API data.
-
#method_missing(name, *args, &block) ⇒ Object
Pass method calls through to the API data.
-
#oauth ⇒ Openlive::OAuth
Instance convenience method for oauth instance.
-
#refresh ⇒ self
Refetch data from the API and update existing attributes.
Constructor Details
#initialize(data, response: nil) ⇒ Hash
Initialize an instance (used by subclasses) with API data
15 16 17 18 |
# File 'lib/openlive/base.rb', line 15 def initialize(data, response: nil) self.api_data = data self.response = response end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, &block) ⇒ Object
Pass method calls through to the API data
45 46 47 48 49 |
# File 'lib/openlive/base.rb', line 45 def method_missing(name, *args, &block) if api_data.is_a?(Hash) api_data[name.to_s] end end |
Instance Attribute Details
#api_data ⇒ Hash
Returns a hash of data returned from the API.
6 7 8 |
# File 'lib/openlive/base.rb', line 6 def api_data @api_data end |
#response ⇒ Object
Convenience method for accessing the API response data
9 10 11 |
# File 'lib/openlive/base.rb', line 9 def response @response end |
Class Method Details
.connection ⇒ Faraday::Connection
Faraday connection
55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/openlive/base.rb', line 55 def connection @connection ||= ( conn = Faraday.new(url: Openlive.configuration.base_uri) do |faraday| faraday.request :url_encoded faraday.response :logger faraday.adapter Faraday.default_adapter end conn.(:Bearer, oauth.token.token) conn.url_prefix = Openlive.configuration.base_uri conn ) end |
.handle_response(response, error_class: Openlive::Error, message: nil) {|Response| ... } ⇒ Object
Raise an exception or execute the following block, used for generic error handling for all routes
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/openlive/base.rb', line 84 def handle_response(response, error_class: Openlive::Error, message: nil, &block) = case when !.nil? when error_class == Openlive::APIError "endpoint returned a #{response.status} status: #{response.body}" end case when response.success? block.call(response) when response.status == 404 nil else raise error_class, end rescue Exception => ex raise error_class, ex. end |
.oauth ⇒ Openlive::OAuth
OAuth handler
72 73 74 |
# File 'lib/openlive/base.rb', line 72 def oauth @oauth ||= OAuth.new end |
Instance Method Details
#connection ⇒ Faraday::Connection
Instance convenience method for the connection
23 24 25 |
# File 'lib/openlive/base.rb', line 23 def connection self.class.connection end |
#oauth ⇒ Openlive::OAuth
Instance convenience method for oauth instance
30 31 32 |
# File 'lib/openlive/base.rb', line 30 def oauth self.class.oauth end |
#refresh ⇒ self
Refetch data from the API and update existing attributes
37 38 39 40 41 42 |
# File 'lib/openlive/base.rb', line 37 def refresh new_data = self.class.find(id) self.api_data = new_data.api_data self.response = new_data.response self end |