class RMeetup::Poster::Base

RMeetup::Poster::Base

Base poster class that other posters will inherit from.

Public Class Methods

new() click to toggle source
# File lib/rmeetup/poster/base.rb, line 28
def initialize
  @type = nil
  @response_type = ResponseType::BASIC_RESPONSE
end

Public Instance Methods

post(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/poster/base.rb, line 38
def post(options = {})
  raise NotConfiguredError, /posts only possible with a concrete type/ if @type.nil?

  url = build_url(options)
  res = post_response(url, options)
  response_body = res.body
  data = JSON.parse(response_body)

  unless res.is_a?(Net::HTTPSuccess)
    # Check to see if the api returned an error
    if data.has_key?('problem')
      raise ApiError.new(data['details'], url)
    else
      raise NoResponseError.new
    end
  end

  case @response_type
    when ResponseType::OBJECT
      format_result(data)
    else
      res
  end
end

Protected Instance Methods

base_url() click to toggle source
# File lib/rmeetup/poster/base.rb, line 76
def base_url
  "https://api.meetup.com/2/#{@type}/"
end
build_url(options) click to toggle source
# File lib/rmeetup/poster/base.rb, line 72
def build_url(options)
  base_url
end
encode_options(options) click to toggle source

Encode a hash of options to be used as request parameters

# File lib/rmeetup/poster/base.rb, line 90
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/poster/base.rb, line 68
def format_result(result)
  result
end
params_for(options) click to toggle source

Create a query string from an options hash

# File lib/rmeetup/poster/base.rb, line 81
def params_for(options)
  params = []
  options.each do |key, value|
    params << "#{key}=#{value}"
  end
  "?#{params.join("&")}"
end
post_response(url, options) click to toggle source
# File lib/rmeetup/poster/base.rb, line 96
def post_response(url, options)
  sslurl = URI.parse(url)

  https = Net::HTTP.new(sslurl.host, sslurl.port)
  https.use_ssl = true
  req = Net::HTTP::Post.new(sslurl.path)
  req.set_form_data(options)
  res = https.request(req)
end