Skip to content

Instantly share code, notes, and snippets.

@romanbsd
Created May 22, 2012 13:11
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save romanbsd/2768946 to your computer and use it in GitHub Desktop.
Save romanbsd/2768946 to your computer and use it in GitHub Desktop.
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