class Rubeetup::Request

Represents API requests. Provides for their own validation and execution

Attributes

http_verb[R]

@return [Symbol] http_verb the http_verb for the request

method_path[R]

@return [String] method_path the path of the Meetup API resource

multipart[R]

@return [Lambda] multipart if present it contains the multipart POST logic

name[R]

@return [Symbol] name the name of the request as input by the user

options[R]

@return [Hash{Symbol=>String}] options holds the request’s options

sender[R]

@return [Rubeetup::Sender] sender the request’s chosen sender

Public Class Methods

new(args = {}) click to toggle source

@param [Hash{Symbol=>String}] args holds the request’s data @option args [Symbol] :name the full request’s name @option args [Hash{Symbol=>String}] :options holds the request’s options @option args [Symbol] :http_verb the request’s http_verb

# File lib/rubeetup/request.rb, line 47
def initialize(args = {})
  @name = args[:name]
  @options = args[:options]
  validate_request
  @http_verb = args[:http_verb]
  @method_path = request_path.call(@options)
  @multipart = request_multipart
  @sender = Rubeetup::RequestSender.new
end

Public Instance Methods

execute() click to toggle source

Completes this request @return [Array<Rubeetup::ResponseWrapper>] the request’s response

# File lib/rubeetup/request.rb, line 61
def execute
  sender.get_response(self)
end
to_s() click to toggle source

For debugging purposes

# File lib/rubeetup/request.rb, line 68
    def to_s
      <<-DOC.gsub(/^ {8}/, '')
        \nREQUEST
        name => #{name}
        verb => #{http_verb}
        path => #{method_path}
        options => #{options.inspect}\n
      DOC
    end

Private Instance Methods

error_class() click to toggle source
# File lib/rubeetup/request.rb, line 135
def error_class
  Rubeetup::RequestError
end
existence_message() click to toggle source
# File lib/rubeetup/request.rb, line 107
    def existence_message
      <<-DOC.gsub(/^ {8}/, '')
        The provided request => '#{name}' is an invalid request.
        This request does not exist in the catalog of supported requests.
        Please consult rubeetup/requests_lib/meetup_catalog.rb, or the provided
        documentation for the complete list of requests.
      DOC
    end
has_all_required_options?() click to toggle source

Uses sets to make sure that there is at least a set of required parameters which is a subset of the arguments passed to the request @return [Boolean] whether all the required arguments have been passed

# File lib/rubeetup/request.rb, line 98
def has_all_required_options?
  required_keys_sets = required_options.map do |elem|
    elem.respond_to?(:to_set) ? elem.to_set : Set[elem]
  end
  return true if required_keys_sets.empty?
  option_keys_set = options.keys.to_set
  required_keys_sets.any? { |set| set.subset? option_keys_set }
end
options_message() click to toggle source
# File lib/rubeetup/request.rb, line 116
    def options_message
      <<-DOC.gsub(/^ {8}/, '')
        This request cannot be completed as is.
        The provided arguments => '#{options.inspect}' miss one or more
        required parameters.
        The request '#{name}' requires the following parameters:
        #{required_options_message}
        Please consult rubeetup/requests_lib/meetup_catalog.rb, or the provided
        documentation for the complete list of requests, and their respective
        required parameters.
      DOC
    end
required_options_message() click to toggle source
# File lib/rubeetup/request.rb, line 129
def required_options_message
  str = ''
  required_options.each {|opt| str << opt.inspect + " OR "}
  str[0...-4]
end
validate_options() click to toggle source
# File lib/rubeetup/request.rb, line 89
def validate_options
  fail error_class, options_message unless has_all_required_options?
end
validate_request() click to toggle source
# File lib/rubeetup/request.rb, line 80
def validate_request
  verify_existence
  validate_options
end
verify_existence() click to toggle source
# File lib/rubeetup/request.rb, line 85
def verify_existence
  fail error_class, existence_message unless is_in_catalog?
end