module TheCastleClient

Constants

VERSION

Attributes

host[RW]
key[RW]
port[RW]
scheme[RW]
secret[RW]
version[RW]

Public Class Methods

account_aggregates(id) click to toggle source
# File lib/the_castle_client.rb, line 27
def account_aggregates(id); query(id, 'account', 'aggregates'); end
account_data(id, opts={}) click to toggle source

these are just niceties around the query method; deprecate?

# File lib/the_castle_client.rb, line 26
def account_data(id, opts={}); query(id, 'account', nil, opts); end
most_listened_pieces(most_on=Date.today, limit=25) click to toggle source
# File lib/the_castle_client.rb, line 10
def most_listened_pieces(most_on=Date.today, limit=25)
  most_on = Date.parse(most_on) if most_on.is_a?(String)
  options = {'most_on'=>most_on.to_s, 'limit'=>limit}
  url = "/api/#{version}/pieces/most_listened#{to_query(options)}"
  get(url, {'Accept'=>'application/json', 'Content-Type'=>'application/json'})
end
piece_aggregates(id) click to toggle source
# File lib/the_castle_client.rb, line 29
def piece_aggregates(id); query(id, 'piece', 'aggregates'); end
piece_data(id, opts={}) click to toggle source
# File lib/the_castle_client.rb, line 28
def piece_data(id, opts={}); query(id, 'piece', nil, opts); end
query(id, model='account', data_type=nil, options={}) click to toggle source
# File lib/the_castle_client.rb, line 31
def query(id, model='account', data_type=nil, options={})
  raise "Invalid data type #{data_type}" unless ['aggregates', 'referrers', 'embedders', '', nil].include?(data_type)
  url = ['/api', version, model.pluralize, id, data_type].compact.join("/") + to_query(options)
  get(url, {'Accept'=>'application/json', 'Content-Type'=>'application/json'})
end

Protected Class Methods

access_token() click to toggle source
# File lib/the_castle_client.rb, line 52
def access_token
  @access_token ||= OAuth::AccessToken.new(consumer)
end
consumer() click to toggle source
# File lib/the_castle_client.rb, line 45
def consumer
  @consumer ||= OAuth::Consumer.new(key,
                                    secret,
                                    :site               => "#{scheme || 'http'}://#{host}:#{port}",
                                    :http_method        => :get)
end
to_query(hash) click to toggle source
# File lib/the_castle_client.rb, line 56
def to_query(hash)
  params = ''
  stack = []

  hash.each do |k, v|
    if v.is_a?(Hash)
      stack << [k,v]
    else
      params << "#{k}=#{v}&"
    end
  end

  stack.each do |parent, h|
    h.each do |k, v|
      if v.is_a?(Hash)
        stack << ["#{parent}[#{k}]", v]
      else
        params << "#{parent}[#{k}]=#{v}&"
      end
    end
  end

  params.chop! # trailing &
  (params.nil? || params.size <=0) ? '' : "?#{params}"
end