Created
May 22, 2012 13:11
-
-
Save romanbsd/2768946 to your computer and use it in GitHub Desktop.
Stathat API using Faraday
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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