class Openlive::Base
Attributes
@return [Hash] a hash of data returned from the API
Convenience method for accessing the API response data
Public Class Methods
Faraday connection
@return [Faraday::Connection]
# 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.authorization(:Bearer, oauth.token.token) conn.url_prefix = Openlive.configuration.base_uri conn ) end
Raise an exception or execute the following block, used for generic error handling for all routes
@param response [Response] @param error_class [OpenliveError] @param message [String] an optional message for the exception if raised @yield [Response] Block called for success condition @raise [OpenliveError] Will raise an error on unsuccessful response
# File lib/openlive/base.rb, line 84 def handle_response(response, error_class: Openlive::Error, message: nil, &block) message = case when !message.nil? message 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, message end rescue Exception => ex raise error_class, ex.message end
Initialize an instance (used by subclasses) with API data
@param data [Hash] @return [Hash] the API data
# File lib/openlive/base.rb, line 15 def initialize(data, response: nil) self.api_data = data self.response = response end
OAuth
handler
@return [Openlive::OAuth]
# File lib/openlive/base.rb, line 72 def oauth @oauth ||= OAuth.new end
Public Instance Methods
Instance convenience method for the connection
@return [Faraday::Connection]
# File lib/openlive/base.rb, line 23 def connection self.class.connection end
Pass method calls through to the API data
# 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 convenience method for oauth instance
@return [Openlive::OAuth]
# File lib/openlive/base.rb, line 30 def oauth self.class.oauth end
Refetch data from the API and update existing attributes
@return [self]
# 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