-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkred.rb
More file actions
43 lines (38 loc) · 940 Bytes
/
kred.rb
File metadata and controls
43 lines (38 loc) · 940 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
require 'net/http'
require 'json'
=begin
Usage:
api = Kred::KredAPI.new(id, key)
json_obj = api.kred_score(
source: 'twitter',
term: 'zombies',
)
puts json_obj['data']
=end
module Kred
class KredAPI
def initialize(id, key)
@id, @key, @base_url = id, key, 'http://api.kred.com'
end
def kred(params={})
fetch "kred", params
end
def kred_score(params={})
fetch "kredscore", params
end
private
def fetch(page, params={})
uri = URI("#@base_url/#{page}?app_id=#@id&app_key=#@key&#{URI.encode_www_form params}")
json = nil
begin
sleep 0.5 unless json.nil?
json = JSON.parse(''.tap do |j|
Net::HTTP.start(uri.host, uri.port) do |http|
http.get(uri.request_uri) { |response| j << response }
end
end)
end until json['status'] == 'complete'
json
end
end
end