Sha256: 9f38b367ecdfb0585fe11d81c2fd774566dbf06f913608b3d5a911a9b12b5e10
Contents?: true
Size: 1.4 KB
Versions: 1
Compression:
Stored size: 1.4 KB
Contents
require 'http/rest_client' require 'ostruct' module Fluxx # Base endpoint resources class class Resource < OpenStruct extend HTTP::RestClient::DSL extend HTTP::RestClient::CRUD PAGINATION_FIELDS = [ 'total_pages', 'total_entries', 'current_page', 'per_page' ] endpoint ENV['FLUXX_INSTANCE_URL'] content_type 'application/json' # Defines the client endpoint, thread-safe # # @param value [String] the endpoint URI. # @return [String] def self.endpoint(value = nil) return super(value) unless value.nil? Thread.current.thread_variable_get(:FLUXX_INSTANCE_URL) || super end # Updated request handler with the auto-refreshing token authentication # # @param payload [Hash] request payload. # @return [HTTP::Response] instance. def self.request(*args) auth("Bearer #{Token.fresh.access_token}") unless self == Token super(*args) end # Resource constructor wrapper # # @param payload [Hash] response payload to build a resource. # @return [Object] instance or a list of instances. 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 end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
fluxx-0.1.5 | lib/fluxx/resource.rb |