1
0
Fork 0
robbie/lib/robbie/parsers/base_parser.rb

55 lines
1.6 KiB
Ruby
Raw Normal View History

2012-08-26 04:09:00 +00:00
module Robbie
module Parsers
class BaseParser
include HTTParty
base_uri "api.rovicorp.com"
format :json
2012-08-26 14:07:37 +00:00
@@calls = []
2012-08-26 04:09:00 +00:00
class << self
def sig
Digest::MD5.hexdigest("#{API_KEY}#{API_SECRET}#{Time.now.to_i}")
end
def query(path, params)
unless Robbie.const_defined?(:API_KEY) and Robbie.const_defined?(:API_SECRET)
raise Exception.new("No API credentials given")
end
params_str = params
.merge({ apikey: API_KEY, sig: sig, format: "json" })
.map{ |key, val| "#{key}=#{val}" }.join("&")
if Robbie.cache_enabled?
cache_key = "#{path}?#{params.inspect}".scan(/\w/).join
cache_file = File.expand_path("../../../../tmp/cache/#{cache_key}", __FILE__)
if File.exist?(cache_file)
MultiJson.load(File.open(cache_file).read)
else
2012-08-26 14:07:37 +00:00
data = load("#{path}?#{params_str}")
2012-08-26 04:09:00 +00:00
File.open(cache_file, "w") do |file|
file.write(MultiJson.dump(data)) unless data.nil? or data.empty?
end
data
end
else
2012-08-26 14:07:37 +00:00
load("#{path}?#{params_str}")
2012-08-26 04:09:00 +00:00
end
end
2012-08-26 14:07:37 +00:00
def load(uri)
if Robbie.free_limits?
@@calls = @@calls.length > 5 ? @@calls.slice(-5, 5) : @@calls
if @@calls.length > 5 && Time.now.to_f - @@calls.first <= 1.0
sleep(1.05 - (Time.now.to_f - @@calls.first))
end
@@calls << Time.now.to_f
end
get(uri)
end
2012-08-26 04:09:00 +00:00
end
end
end
end