class Eventbrite::Api::Model::Base

Public Class Methods

new(client, model_name=nil) click to toggle source
# File lib/eventbrite/api/model/base.rb, line 6
def initialize(client, model_name=nil)
  @client          = client
  @model_name      = model_name || 'Base'

  @pagination = nil
  @endpoint = nil
end

Public Instance Methods

all(opts) click to toggle source
# File lib/eventbrite/api/model/base.rb, line 35
def all(opts)
  all_pages = get(opts)
  while @pagination && @pagination['page_number'] < @pagination['page_count']
    all_pages = all_pages.deeper_merge(next_page(opts))
  end
  all_pages
end
create(object, opts={}) click to toggle source
# File lib/eventbrite/api/model/base.rb, line 27
def create(object, opts={})
  @client.connection.post(url(opts), {:headers => @client.headers, :body => object.to_json})
end
find(id) click to toggle source
# File lib/eventbrite/api/model/base.rb, line 23
def find(id)
  self.get({}, id)
end
get(opts={}, endpoint=nil) click to toggle source
# File lib/eventbrite/api/model/base.rb, line 18
def get(opts={}, endpoint=nil)
  @endpoint = endpoint
  perform_request(url(opts, endpoint))
end
model_route() click to toggle source
# File lib/eventbrite/api/model/base.rb, line 14
def model_route
  @model_name.to_s.downcase
end
next_page(opts={}) click to toggle source
# File lib/eventbrite/api/model/base.rb, line 43
def next_page(opts={})
  opts['params'] = {'page'=>@pagination['page_number']+1}
  get(opts, @endpoint)
end
previous_page(opts={}) click to toggle source
# File lib/eventbrite/api/model/base.rb, line 48
def previous_page(opts={})
  opts['params'] = {'page'=>@pagination['page_number']-1}
  get(opts, @endpoint)
end
update(id, object, opts={}) click to toggle source
# File lib/eventbrite/api/model/base.rb, line 31
def update(id, object, opts={})
  @client.connection.post(url(opts, id), {:headers => @client.headers, :body => object.to_json})
end

Protected Instance Methods

perform_request(target) click to toggle source
# File lib/eventbrite/api/model/base.rb, line 75
def perform_request(target)
  response = @client.connection.get(target, {:headers => @client.headers})
  hash = JSON.parse(response.body)
  @pagination = hash['pagination']
  hash
end
resource_url(opts={}) click to toggle source
# File lib/eventbrite/api/model/base.rb, line 69
def resource_url(opts={})
  target = "https://www.eventbriteapi.com/v3/#{model_route}"
  opts.each { |k,v| target.gsub!(":#{k}", v) if v.is_a?(String) } unless opts.empty?
  target
end
url(opts={}, endpoint=nil) click to toggle source
# File lib/eventbrite/api/model/base.rb, line 54
def url(opts={}, endpoint=nil)
  if model_route == ''
    target = "https://www.eventbriteapi.com/v3"
  elsif endpoint
    target = "#{resource_url(opts)}/#{endpoint}"
  else
    target = resource_url(opts)
  end
  if opts['params']
    params = opts['params'].map { |k,v| "#{k}=#{v}" }.join('&')
    target = "#{target}?#{params}"
  end
  target
end