class RMeetup::Fetcher::Base

RMeetup::Fetcher::Base

Base fetcher class that other fetchers will inherit from.

Public Class Methods

new() click to toggle source
# File lib/rmeetup/fetcher/base.rb, line 21
def initialize
  @type = nil
end

Public Instance Methods

base_path() click to toggle source
# File lib/rmeetup/fetcher/base.rb, line 50
def base_path
  "/2/#{@type}"
end
fetch(options = {}) click to toggle source

Fetch and parse a response based on a set of options. Override this method to ensure neccessary options are passed for the request.

@param options [Hash] Options for deciding what and how to fetch @return [RMeetup::Collection] Collection of appropriate entities

# File lib/rmeetup/fetcher/base.rb, line 29
def fetch(options = {})
  raise NotConfiguredError, /fetches only possible with a concrete fetcher/ if @type.nil?
  path = path_and_query(options)

  # Fetch and parse data from Meetup
  response_body = requester(options).get(path).body || raise(NoResponseError.new)
  data = JSON.parse(response_body)

  # Check to see if the api returned an error
  raise ApiError.new(data['details'],path) if data.has_key?('problem')

  collection = RMeetup::Collection.build(data)

  # Format each result in the collection and return it
  collection.map!{|result| format_result(result)}
end
path_and_query(options) click to toggle source
# File lib/rmeetup/fetcher/base.rb, line 46
def path_and_query(options)
  base_path + query(options)
end
query(options) click to toggle source

Create a query string from an options hash

# File lib/rmeetup/fetcher/base.rb, line 55
def query(options)
  '?' + URI.encode_www_form(options)
end
requester(q) click to toggle source

Decides whether to use HTTP or HTTPS. HTTPS is needed if we’re authoizing via the :access_token

@param q [Hash] Constructed HTTP(S) query @return [Net:HTTP] Constructs an HTTP request if client is given an API key @return [Net:HTTPS] Constructs an HTTPS request if client is given an access token

# File lib/rmeetup/fetcher/base.rb, line 64
def requester(q)
  if q.has_key?(:api_key)
    http
  elsif q.has_key?(:access_token)
    https
  else
    http
  end
end

Protected Instance Methods

format_result(result) click to toggle source

OVERRIDE this method to format a result section as per Result type. Takes a result in a collection and formats it to be put back into the collection.

# File lib/rmeetup/fetcher/base.rb, line 79
def format_result(result)
  result
end
http() click to toggle source
# File lib/rmeetup/fetcher/base.rb, line 83
def http
  Net::HTTP.new Domain, 80
end
https() click to toggle source
# File lib/rmeetup/fetcher/base.rb, line 87
def https
  c = Net::HTTP.new Domain, 443
  c.use_ssl = true
  c
end