Class: Openlive::Base

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/openlive/base.rb

Direct Known Subclasses

Artist, Booking, MasterBuilder, Request, Response, User

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data, response: nil) ⇒ Hash

Initialize an instance (used by subclasses) with API data

Parameters:

  • data (Hash)


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_dataHash

Returns a hash of data returned from the API.

Returns:

  • (Hash)

    a hash of data returned from the API



6
7
8
# File 'lib/openlive/base.rb', line 6

def api_data
  @api_data
end

#responseObject

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

.connectionFaraday::Connection

Faraday connection

Returns:

  • (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.authorization(: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

Parameters:

  • response (Response)
  • error_class (OpenliveError) (defaults to: Openlive::Error)
  • message (String) (defaults to: nil)

    an optional message for the exception if raised

Yields:

  • (Response)

    Block called for success condition

Raises:

  • (OpenliveError)

    Will raise an error on unsuccessful response



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)
  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

.oauthOpenlive::OAuth

OAuth handler

Returns:



72
73
74
# File 'lib/openlive/base.rb', line 72

def oauth
  @oauth ||= OAuth.new
end

Instance Method Details

#connectionFaraday::Connection

Instance convenience method for the connection

Returns:

  • (Faraday::Connection)


23
24
25
# File 'lib/openlive/base.rb', line 23

def connection
  self.class.connection
end

#oauthOpenlive::OAuth

Instance convenience method for oauth instance

Returns:



30
31
32
# File 'lib/openlive/base.rb', line 30

def oauth
  self.class.oauth
end

#refreshself

Refetch data from the API and update existing attributes

Returns:

  • (self)


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