Class: PostRank::API

Inherits:
Object
  • Object
show all
Defined in:
lib/postrank-api/api.rb,
lib/postrank-api/version.rb

Constant Summary collapse

API_BASE =
'http://api.postrank.com'
V1_API_BASE =
API_BASE + '/v1'
V2_API_BASE =
API_BASE + '/v2'
V3_API_BASE =
API_BASE + '/v3'
VERSION =
"0.4.0"

Instance Method Summary collapse

Constructor Details

#initialize(appkey) ⇒ API

Returns a new instance of API.



16
17
18
# File 'lib/postrank-api/api.rb', line 16

def initialize(appkey)
  @appkey = appkey
end

Instance Method Details

#domain_activity(domains, opts = {}) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/postrank-api/api.rb', line 115

def domain_activity(domains, opts = {})
  opts[:start_time] ||= '1 month ago'
  opts[:end_time]   ||= 'today'

  req = {
    :query => {
      :appkey     => @appkey,
      :start_time => Chronic.parse(opts[:start_time]).to_i,
      :end_time   => Chronic.parse(opts[:end_time]).to_i,
      :mode       => opts[:mode]
    },
    :body => build_body(domains, 'domain')
  }

  http = post("#{V2_API_BASE}/domain/activity", req)
  parse(http.response)
end

#feed(feed, opts = {}) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/postrank-api/api.rb', line 35

def feed(feed, opts = {})
  req = {
    :query => {
      :appkey => @appkey,
      :level  => opts[:level] || 'all',
      :q      => opts[:q]     || '',
      :num    => opts[:num]   || 10,
      :start  => opts[:start] || 0,
      :id     => feed
    }
  }

  http = get("#{V3_API_BASE}/feed/", req)
  parse(http.response)
end

#feed_engagement(feeds, opts = {}) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/postrank-api/api.rb', line 94

def feed_engagement(feeds, opts = {})
  opts[:start_time] ||= '1 month ago'
  opts[:end_time]   ||= 'today'
  opts[:summary]    = true if not opts.key?(:summary)

  req = {
    :query => {
      :appkey     => @appkey,
      :mode       => opts[:mode] || 'daily',
      :start_time => Chronic.parse(opts[:start_time]).to_i,
      :end_time   => Chronic.parse(opts[:end_time]).to_i
    },
    :body => build_body(feeds, 'feed')
  }

  req[:query][:summary] = opts[:summary] if opts[:summary]

  http = post("#{V2_API_BASE}/feed/engagement", req)
  parse(http.response)
end

#feed_info(feeds, opts = {}) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/postrank-api/api.rb', line 20

def feed_info(feeds, opts = {})
  req = {
    :query => {
      :appkey => @appkey,
      :noindex => opts[:noindex] || false,
    },
    :body => build_body(feeds, 'feed')
  }

  http = post("#{V2_API_BASE}/feed/info", req)
  resp = parse(http.response)

  resp.key?('items') ? resp['items'] : resp
end

#metrics(urls, opts = {}) ⇒ Object



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/postrank-api/api.rb', line 133

def metrics(urls, opts = {})
  reverse = {}
  urls = [urls].flatten.map do |url|
    md5 = (url =~ /\w{32}/) ? url : Digest::MD5.hexdigest(url)
    reverse[md5] = url
    md5
  end

  req = {
    :query => {
      :appkey => @appkey,
    },
    :body => build_body(urls, 'url')
  }

  http = post("#{V3_API_BASE}/entry/metrics", req)
  parse(http.response).inject({}) do |hash, v|
    hash[reverse[v[0]]] = v[1]
    hash
  end
end

#metrics_versioned(posts, opts = {}) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/postrank-api/api.rb', line 51

def metrics_versioned(posts, opts = {})
  opts[:start_time] ||= 'yesterday'
  opts[:end_time]   ||= 'today'

  req = {
    :query => {
      :appkey => @appkey,
      :start_time => Chronic.parse(opts[:start_time]).to_i,
      :end_time => Chronic.parse(opts[:end_time]).to_i
    },
    :body => build_body(posts, 'post_hash')
  }

  http = post("#{V2_API_BASE}/entry/metrics/historic", req)
  parse(http.response)
end

#postrank(urls, feeds = [], opts = {}) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
# File 'lib/postrank-api/api.rb', line 68

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

#top_posts(feed, opts = {}) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/postrank-api/api.rb', line 80

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