Class: RMeetup::Fetcher::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/rmeetup/fetcher/base.rb

Overview

RMeetup::Fetcher::Base

Base fetcher class that other fetchers will inherit from.

Direct Known Subclasses

Cities, Events, Groups, Members, OpenEvents, OpenVenues, Photos, Rsvps, Venues

Instance Method Summary collapse

Constructor Details

#initializeBase

Returns a new instance of Base.



21
22
23
# File 'lib/rmeetup/fetcher/base.rb', line 21

def initialize
  @type = nil
end

Instance Method Details

#base_pathObject



50
51
52
# File 'lib/rmeetup/fetcher/base.rb', line 50

def base_path
  "/2/#{@type}"
end

#fetch(options = {}) ⇒ RMeetup::Collection

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

Parameters:

  • options (Hash) (defaults to: {})

    Options for deciding what and how to fetch

Returns:

Raises:

  • (NotConfiguredError)


29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# 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) ⇒ Object



46
47
48
# File 'lib/rmeetup/fetcher/base.rb', line 46

def path_and_query(options)
  base_path + query(options)
end

#query(options) ⇒ Object

Create a query string from an options hash



55
56
57
# File 'lib/rmeetup/fetcher/base.rb', line 55

def query(options)
  '?' + URI.encode_www_form(options)
end

#requester(q) ⇒ Net:HTTP, Net:HTTPS

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

Parameters:

  • q (Hash)

    Constructed HTTP(S) query

Returns:

  • (Net:HTTP)

    Constructs an HTTP request if client is given an API key

  • (Net:HTTPS)

    Constructs an HTTPS request if client is given an access token



64
65
66
67
68
69
70
71
72
# 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