class Songdrop::Client

Attributes

auth_token[RW]

Public Class Methods

new(options={}) click to toggle source
# File lib/songdrop/client.rb, line 5
def initialize(options={})
  @token = options[:token]
  @endpoint = options[:endpoint] || 'https://songdrop.com/v1'
  @auth_token = options[:auth_token]
  @ip_address = options[:ip_address] # if the client is proxying for the user
  @user_agent = options[:user_agent] # if the client is proxying for the user
end

Public Instance Methods

delete(path, params={}, &block) click to toggle source
# File lib/songdrop/client.rb, line 37
def delete(path, params={}, &block)
  puts "[Songdrop::Client] DELETE #{path} with #{params.inspect} block? #{block_given?}"
  params.merge!(:client => @token, :token => @auth_token, :ip => @ip_address, :ua => @user_agent)
  HTTP.delete(full_url(path), params) do |response, headers, error|
    handle_response(response, headers, error, &block)
  end
end
get(path, params={}, &block) click to toggle source
# File lib/songdrop/client.rb, line 13
def get(path, params={}, &block)
  puts "[Songdrop::Client] GET #{path} with #{params.inspect} block? #{block_given?}"
  params.merge!(:client => @token, :token => @auth_token, :ip => @ip_address, :ua => @user_agent)
  HTTP.get(full_url(path), params) do |response, headers, error|
    handle_response(response, headers, error, &block)
  end
end
login(params, &block) click to toggle source
# File lib/songdrop/client/methods.rb, line 4
def login(params, &block)
  post('/session', params) do |user|
    @auth_token = user.auth_token
    block.call user if block
    user
  end
end
post(path, params={}, &block) click to toggle source
# File lib/songdrop/client.rb, line 29
def post(path, params={}, &block)
  puts "[Songdrop::Client] POST #{path} with #{params.inspect} block? #{block_given?}"
  params.merge!(:client => @token, :token => @auth_token, :ip => @ip_address, :ua => @user_agent)
  HTTP.post(full_url(path), params) do |response, headers, error|
    handle_response(response, headers, error, &block)
  end
end
put(path, params={}, &block) click to toggle source
# File lib/songdrop/client.rb, line 21
def put(path, params={}, &block)
  puts "[Songdrop::Client] PUT #{path} with #{params.inspect} block? #{block_given?}"
  params.merge!(:client => @token, :token => @auth_token, :ip => @ip_address, :ua => @user_agent)
  HTTP.put(full_url(path), params) do |response, headers, error|
    handle_response(response, headers, error, &block)
  end
end
signup(params, &block) click to toggle source
# File lib/songdrop/client/methods.rb, line 12
def signup(params, &block)
  post('/users', params) do |user|
    @auth_token = user.auth_token
    block.call user if block
    user
  end
end

Private Instance Methods

full_url(path) click to toggle source
# File lib/songdrop/client.rb, line 46
def full_url(path)
  "#{@endpoint}#{path}"
end
handle_response(response, headers, error, &block) click to toggle source
# File lib/songdrop/client.rb, line 50
def handle_response(response, headers, error, &block)
  target = response || error
  res = nil
  JSON.parse(target) do |obj|
    res = Parser.parse(obj, headers)
    block.call res if block
    res
  end
  res
end