Class: Fluxx::Resource

Inherits:
OpenStruct
  • Object
show all
Extended by:
HTTP::RestClient::CRUD, HTTP::RestClient::DSL
Defined in:
lib/fluxx/resource.rb

Overview

Base endpoint resources class

Direct Known Subclasses

Token

Constant Summary collapse

PAGINATION_FIELDS =
[
  'total_pages',
  'total_entries',
  'current_page',
  'per_page'
]

Class Method Summary collapse

Class Method Details

.endpoint(value = nil) ⇒ String

Defines the client endpoint, thread-safe

Parameters:

  • value (String) (defaults to: nil)

    the endpoint URI.

Returns:

  • (String)


24
25
26
27
28
# File 'lib/fluxx/resource.rb', line 24

def self.endpoint(value = nil)
  return super(value) unless value.nil?

  Thread.current.thread_variable_get(:FLUXX_INSTANCE_URL) || super
end

.objectify(payload) ⇒ Object

Resource constructor wrapper

Parameters:

  • payload (Hash)

    response payload to build a resource.

Returns:

  • (Object)

    instance or a list of instances.



43
44
45
46
47
48
49
# File 'lib/fluxx/resource.rb', line 43

def self.objectify(payload)
  filtered = payload.reject { |key, _| PAGINATION_FIELDS.include?(key) }
  data = filtered.values.first if filtered.is_a?(Hash) && filtered.size == 1
  data = data.values.first if data.is_a?(Hash) && data.size == 1

  super(data || payload)
end

.request(*args) ⇒ HTTP::Response

Updated request handler with the auto-refreshing token authentication

Parameters:

  • payload (Hash)

    request payload.

Returns:

  • (HTTP::Response)

    instance.



34
35
36
37
# File 'lib/fluxx/resource.rb', line 34

def self.request(*args)
  auth("Bearer #{Token.fresh.access_token}") unless self == Token
  super(*args)
end