class Rubeetup::Requester

@author Mike Vascelli <michele.vascelli@gmail.com>

Requester instances perform requests for the user

Attributes

auth[R]

@return [Hash{Symbol=>String}] this Requester’s auth data

request_builder[R]

@return [Symbol] the chosen request builder

Public Class Methods

new(args) click to toggle source

@param [Hash{Symbol=>String}] args holds auth data to send with each request @option args [String] :key the api key

# File lib/rubeetup/requester.rb, line 24
def initialize(args)
  self.auth = args
  @request_builder = Rubeetup::RequestBuilder
end

Public Instance Methods

auth=(args) click to toggle source

Sets auth data, and validates it. @param [Hash{Symbol=>String}] args holds auth data to send with each request @option args [String] :key the api key @raise [Rubeetup::InvalidAuthenticationError] if the passed auth data is not a Hash,

or if it does not include key: 'val'
# File lib/rubeetup/requester.rb, line 36
def auth=(args)
  @auth = args
  validate_auth
end
method_missing(name, *args) click to toggle source

Performs the actual request, by dynamically creating a Request instance, and by then executing it. @param [Symbol] name request name @param [Array<Hash{Symbol=>String}, …>] args holds the request options @return [Array<Rubeetup::ResponseWrapper>] the request response

# File lib/rubeetup/requester.rb, line 49
def method_missing(name, *args)
  merge_auth(args)
  request = request_builder.compose_request(name, args)
  request.execute
end

Private Instance Methods

merge_auth(args) click to toggle source

Operates on the memory referenced by args @note should both auth, and args have the same keys, then the keys in auth

will overwrite those in options. This guarantees that if the user
includes api keys while sending a request, those will be ignored.
# File lib/rubeetup/requester.rb, line 69
def merge_auth(args)
  options = args[0]
  args[0] = options.respond_to?(:merge) ? options.merge(auth) : auth
end
validate_auth() click to toggle source
# File lib/rubeetup/requester.rb, line 57
def validate_auth
  fail Rubeetup::InvalidAuthenticationError, 'Must respond to #merge' unless
    auth.respond_to? :merge
  fail Rubeetup::InvalidAuthenticationError, "Requires ---> {key: /[^\s]+/}" unless
    (val = auth[:key]) && present?(val)
end