class Openlive::Base

Attributes

api_data[RW]

@return [Hash] a hash of data returned from the API

response[RW]

Convenience method for accessing the API response data

Public Class Methods

connection() click to toggle source

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
handle_response(response, error_class: Openlive::Error, message: nil, &block) click to toggle source

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
new(data, response: nil) click to toggle source

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() click to toggle source

OAuth handler

@return [Openlive::OAuth]

# File lib/openlive/base.rb, line 72
def oauth
  @oauth ||= OAuth.new
end

Public Instance Methods

connection() click to toggle source

Instance convenience method for the connection

@return [Faraday::Connection]

# File lib/openlive/base.rb, line 23
def connection
  self.class.connection
end
method_missing(name, *args, &block) click to toggle source

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
oauth() click to toggle source

Instance convenience method for oauth instance

@return [Openlive::OAuth]

# File lib/openlive/base.rb, line 30
def oauth
  self.class.oauth
end
refresh() click to toggle source

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