class Sched::Client

Attributes

api_key[RW]
conference[RW]

Public Class Methods

new(conference, api_key) click to toggle source
# File lib/sched/client.rb, line 4
def initialize(conference, api_key)
  @conference = conference
  @api_key = api_key
end

Public Instance Methods

api_url() click to toggle source
# File lib/sched/client.rb, line 19
def api_url
  "https://#{@conference}.sched.com/api"
end
event(session_key) click to toggle source
# File lib/sched/client.rb, line 9
def event(session_key)
  event = Sched::Event.new(session_key, self)
  event = events.find { |e| e.session_key == session_key } if event.exists?
  event
end
events() click to toggle source
# File lib/sched/client.rb, line 15
def events
  @events ||= parse_sessions(CSV.parse(request("session/list", nil, :get)))
end
request(path, data = {}, method = :post) click to toggle source
# File lib/sched/client.rb, line 23
def request(path, data = {}, method = :post)
  data ||= {}
  data[:api_key] = @api_key
  if method == :post
    post_request(path, data)
  elsif method == :get
    get_request(path, data)
  end
end

Private Instance Methods

byte_order_mark() click to toggle source
# File lib/sched/client.rb, line 35
def byte_order_mark
  "\xEF\xBB\xBF".force_encoding("UTF-8")
end
curl_client(path) click to toggle source
# File lib/sched/client.rb, line 39
def curl_client(path)
  c = Curl::Easy.new("#{api_url}/#{path}")
  c.headers["User-Agent"] = "sched-gem/#{Sched::VERSION}"
  c
end
encoded(str) click to toggle source
# File lib/sched/client.rb, line 45
def encoded(str)
  str.force_encoding("UTF-8").gsub(byte_order_mark, "")
end
full_url(path) click to toggle source
# File lib/sched/client.rb, line 49
def full_url(path)
  "#{api_url}/#{path}"
end
get_request(path, data) click to toggle source
# File lib/sched/client.rb, line 53
def get_request(path, data)
  get_attributes = data.map { |key, value| "#{key}=#{value}" }.join("&")
  c = curl_client("#{path}?#{get_attributes}")
  c.perform
  encoded(c.body_str)
end
parse_attributes(attributes) click to toggle source
# File lib/sched/client.rb, line 60
def parse_attributes(attributes)
  return [] unless attributes
  attributes.map do |a|
    a.gsub(/^event_/, "session_").to_sym
  end
end
parse_sessions(results) click to toggle source
# File lib/sched/client.rb, line 67
def parse_sessions(results)
  attributes = parse_attributes(results.shift)
  results.map do |row|
    row_hash = {}
    attributes.each_with_index do |a, i|
      row_hash[a] = row[i]
    end
    Sched::Event.new(row_hash[:session_key], self).configure(row_hash)
  end
end
post_request(path, data) click to toggle source
# File lib/sched/client.rb, line 78
def post_request(path, data)
  post_fields = data.map do |key, value|
    Curl::PostField.content(key.to_s, value)
  end
  c = curl_client(path)
  c.http_post(post_fields)
  encoded(c.body_str)
end