lib/postrank-api/api.rb in postrank-api-0.1.2 vs lib/postrank-api/api.rb in postrank-api-0.1.3
- old
+ new
@@ -5,10 +5,11 @@
require 'chronic'
require 'yajl'
module PostRank
class API
+ V1_API_BASE = 'http://api.postrank.com/v1'
V2_API_BASE = 'http://api.postrank.com/v2'
def initialize(appkey)
@appkey = appkey
end
@@ -42,10 +43,54 @@
http = get("#{V2_API_BASE}/feed/", req)
parse(http.response)
end
+ def recommendations(feeds, opts = {})
+ req = {
+ :query => {
+ :appkey => @appkey,
+ :num => opts[:num] || 10
+ },
+ :body => build_body(feeds, 'feed')
+ }
+
+ http = post("#{V2_API_BASE}/recommend", req)
+ parse(http.response)
+ end
+
+ def metrics_versioned(posts, opts = {})
+
+ start_time = opts[:start] || 'yesterday'
+ end_time = opts[:end] || 'today'
+
+ req = {
+ :query => {
+ :appkey => @appkey,
+ :min_time => Chronic.parse(start_time).to_i,
+ :max_time => Chronic.parse(end_time).to_i
+ },
+ :body => build_body( posts, 'post_hash')
+ }
+
+ http = post("#{V2_API_BASE}/entry/metrics/historic", req)
+ parse(http.response)
+ end
+
+ def postrank(urls, feeds = [], opts = {})
+ req = {
+ :query => {
+ :appkey => @appkey,
+ :format => 'json',
+ },
+ :body => (build_body(urls, 'url'))+"&"+(build_body(feeds, 'feed_hash'))
+ }
+ http = post("#{V1_API_BASE}/postrank", req)
+ parse(http.response)
+ end
+
+
def top_posts(feed, opts = {})
req = {
:query => {
:appkey => @appkey,
:q => opts[:q] || '',
@@ -140,10 +185,18 @@
def get(url, req)
dispatch(:get, url, req)
end
+ def build_body(urls, key)
+ [urls].flatten.map { |e| "#{key}[]=#{e}" }.join("&")
+ end
+
+ def post(url, req)
+ dispatch(:post, url, req)
+ end
+
def dispatch(method, url, req)
if EM.reactor_running?
http = EM::HttpRequest.new(url).send(method, req)
else
EM.synchrony do
@@ -151,7 +204,7 @@
EM.stop
end
end
http
end
- end
end
+end
\ No newline at end of file