lib/postrank-api/api.rb in postrank-api-0.2.0 vs lib/postrank-api/api.rb in postrank-api-0.4.0

- old
+ new

@@ -1,16 +1,19 @@ require 'em-synchrony' require 'em-synchrony/em-http' +require 'postrank-uri' require 'digest/md5' require 'chronic' require 'yajl' module PostRank class API - V1_API_BASE = 'http://api.postrank.com/v1' - V2_API_BASE = 'http://api.postrank.com/v2' + API_BASE = 'http://api.postrank.com' + V1_API_BASE = API_BASE + '/v1' + V2_API_BASE = API_BASE + '/v2' + V3_API_BASE = API_BASE + '/v3' def initialize(appkey) @appkey = appkey end @@ -39,38 +42,25 @@ :start => opts[:start] || 0, :id => feed } } - http = get("#{V2_API_BASE}/feed/", req) + http = get("#{V3_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 = {}) opts[:start_time] ||= 'yesterday' opts[:end_time] ||= 'today' req = { :query => { :appkey => @appkey, - :min_time => Chronic.parse(opts[:start_time]).to_i, - :max_time => Chronic.parse(opts[:start_time]).to_i + :start_time => Chronic.parse(opts[:start_time]).to_i, + :end_time => Chronic.parse(opts[:end_time]).to_i }, - :body => build_body( posts, 'post_hash') + :body => build_body(posts, 'post_hash') } http = post("#{V2_API_BASE}/entry/metrics/historic", req) parse(http.response) end @@ -85,22 +75,21 @@ } http = post("#{V1_API_BASE}/postrank", req) parse(http.response) end - def top_posts(feed, opts = {}) req = { :query => { :appkey => @appkey, :q => opts[:q] || '', :num => opts[:num] || 10, :id => feed } } - http = get("#{V2_API_BASE}/feed/topposts/", req) + http = get("#{V3_API_BASE}/feed/topposts/", req) parse(http.response) end def feed_engagement(feeds, opts = {}) opts[:start_time] ||= '1 month ago' @@ -154,46 +143,64 @@ :appkey => @appkey, }, :body => build_body(urls, 'url') } - http = post("#{V2_API_BASE}/entry/metrics", req) + http = post("#{V3_API_BASE}/entry/metrics", req) parse(http.response).inject({}) do |hash, v| hash[reverse[v[0]]] = v[1] hash end end + def top_posts(feed, opts = {}) + req = { + :query => { + :appkey => @appkey, + :q => opts[:q] || '', + :num => opts[:num] || 10, + :id => feed + } + } + + http = get("#{V3_API_BASE}/feed/topposts/", req) + parse(http.response) + end + private def parse(data) begin - Yajl::Parser.parse(data) - rescue Exception => e + data = Yajl::Parser.parse(data) + + if msg = data['error'] + raise Exception.new(msg) + end + + data + rescue Yajl::ParseError => e puts "Failed to parse request:" puts e.message puts e.backtrace[0, 5].join("\n") end end def build_body(urls, key) - [urls].flatten.map { |e| "#{key}[]=#{e}" }.join("&") + [urls].flatten.map do |e| + if key == "url" && e !~ /\w{32}/ + e = PostRank::URI.clean(e) + end + + "#{key}[]=#{e}" + end.join("&") end def post(url, req) dispatch(:post, url, req) end 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)