1
0
Fork 0

Safe attributes, api limits support

This commit is contained in:
Gregory Eremin 2012-08-26 18:07:37 +04:00
parent 8e26a8d3ec
commit 7fcddbabd5
7 changed files with 33 additions and 6 deletions

View File

@ -38,7 +38,11 @@ foo = Robbie::Artist.find_by_name("foo fighters")
foo = Robbie::Artist.find("MN0000184043") foo = Robbie::Artist.find("MN0000184043")
foo.albums.last foo.albums.last
# <Robbie::Album:0x007fb9cc16b790 @id="MW0002115022", @title="Wasting Light"> # <Robbie::Album:0x007fb9cc16b790
# @id="MW0002115022",
# @title="Wasting Light",
# @year="2011"
# >
# ...or directly by id # ...or directly by id
Robbie::Album.find("MW0002115022") Robbie::Album.find("MW0002115022")

View File

@ -36,5 +36,9 @@ module Robbie
def cache_enabled? def cache_enabled?
@@cache_enabled @@cache_enabled
end end
def free_limits?
true
end
end end
end end

View File

@ -13,7 +13,7 @@ module Robbie
end end
def tracks def tracks
@tracks ||= Parsers::Album.find(id).tracks @tracks ||= Parsers::Album.find(id).tracks || []
end end
end end
end end

View File

@ -17,7 +17,7 @@ module Robbie
end end
def albums def albums
@albums ||= Parsers::Artist.find(id).albums @albums ||= Parsers::Artist.find(id).albums || []
end end
end end
end end

View File

@ -7,5 +7,9 @@ module Robbie
Parsers::Track.find(id) Parsers::Track.find(id)
end end
end end
def artists
@artists || []
end
end end
end end

View File

@ -36,8 +36,10 @@ module Robbie
params = {} params = {}
params[:id] = data["ids"]["albumId"] if data["ids"] params[:id] = data["ids"]["albumId"] if data["ids"]
params[:title] = data["title"] params[:title] = data["title"]
if data["album"] && data["originalReleaseDate"].is_a?(String) if data["originalReleaseDate"].is_a?(String)
params[:year] = data["originalReleaseDate"].split("-").first params[:year] = data["originalReleaseDate"].split("-").first
elsif data["year"].is_a?(String)
params[:year] = data["year"].split("-").first
end end
Robbie::Album.new(params) Robbie::Album.new(params)

View File

@ -4,6 +4,7 @@ module Robbie
include HTTParty include HTTParty
base_uri "api.rovicorp.com" base_uri "api.rovicorp.com"
format :json format :json
@@calls = []
class << self class << self
def sig def sig
@ -25,16 +26,28 @@ module Robbie
if File.exist?(cache_file) if File.exist?(cache_file)
MultiJson.load(File.open(cache_file).read) MultiJson.load(File.open(cache_file).read)
else else
data = get("#{path}?#{params_str}") data = load("#{path}?#{params_str}")
File.open(cache_file, "w") do |file| File.open(cache_file, "w") do |file|
file.write(MultiJson.dump(data)) unless data.nil? or data.empty? file.write(MultiJson.dump(data)) unless data.nil? or data.empty?
end end
data data
end end
else else
get("#{path}?#{params_str}") load("#{path}?#{params_str}")
end end
end end
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
end end
end end
end end