Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Stathat API using Faraday
require 'faraday'
module StatHat
module API
extend self
CLASSIC_VALUE_URL = "http://api.stathat.com/v".freeze
CLASSIC_COUNT_URL = "http://api.stathat.com/c".freeze
EZ_URL = "http://api.stathat.com/ez".freeze
def post_value(stat_key, user_key, value)
args = {
:key => stat_key,
:ukey => user_key,
:value => value
}
send_to_stathat(CLASSIC_VALUE_URL, args)
end
def post_count(stat_key, user_key, count)
args = {
:key => stat_key,
:ukey => user_key,
:value => count
}
send_to_stathat(CLASSIC_COUNT_URL, args)
end
def ez_post_value(stat_name, ezkey, value)
args = {
:stat => stat_name,
:ezkey => ezkey,
:value => value
}
send_to_stathat(EZ_URL, args)
end
def ez_post_count(stat_name, ezkey, count)
args = {
:stat => stat_name,
:ezkey => ezkey,
:count => count
}
send_to_stathat(EZ_URL, args)
end
def send_to_stathat(url, args)
conn = Faraday.new(url)
resp = conn.get(url, args)
Response.new(resp.body)
end
end
class Response
def initialize(body)
@body = body
end
def valid?
return status == 200
end
def status
parsed['status']
end
def msg
parsed['msg']
end
private
def parsed
@parsed ||= JSON.parse(@body)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment