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 20
def initialize
  @type = nil
end

Public Instance Methods

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.

# File lib/rmeetup/fetcher/base.rb, line 29
def fetch(options = {})
  url = build_url(options)
  
  json = get_response(url)
  data = JSON.parse(json)
  
  # Check to see if the api returned an error
  raise ApiError.new(data['details'],url) 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

Protected Instance Methods

base_url() click to toggle source
# File lib/rmeetup/fetcher/base.rb, line 59
def base_url
  "http://api.meetup.com/#{@type}.json/"
end
build_url(options) click to toggle source
# File lib/rmeetup/fetcher/base.rb, line 53
def build_url(options)
  options = encode_options(options)
  
  base_url + params_for(options)
end
encode_options(options) click to toggle source

Encode a hash of options to be used as request parameters

# File lib/rmeetup/fetcher/base.rb, line 73
def encode_options(options)
  options.each do |key,value|
    options[key] = URI.encode(value.to_s)
  end
end
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 49
def format_result(result)
  result
end
get_response(url) click to toggle source
# File lib/rmeetup/fetcher/base.rb, line 79
def get_response(url)
  Net::HTTP.get_response(URI.parse(url)).body || raise(NoResponseError.new)
end
params_for(options) click to toggle source

Create a query string from an options hash

# File lib/rmeetup/fetcher/base.rb, line 64
def params_for(options)
  params = []
  options.each do |key, value|
    params << "#{key}=#{value}"
  end
  "?#{params.join("&")}"
end