Safe attributes, api limits support
This commit is contained in:
parent
8e26a8d3ec
commit
7fcddbabd5
|
@ -38,7 +38,11 @@ foo = Robbie::Artist.find_by_name("foo fighters")
|
|||
foo = Robbie::Artist.find("MN0000184043")
|
||||
|
||||
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
|
||||
Robbie::Album.find("MW0002115022")
|
||||
|
|
|
@ -36,5 +36,9 @@ module Robbie
|
|||
def cache_enabled?
|
||||
@@cache_enabled
|
||||
end
|
||||
|
||||
def free_limits?
|
||||
true
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -13,7 +13,7 @@ module Robbie
|
|||
end
|
||||
|
||||
def tracks
|
||||
@tracks ||= Parsers::Album.find(id).tracks
|
||||
@tracks ||= Parsers::Album.find(id).tracks || []
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -17,7 +17,7 @@ module Robbie
|
|||
end
|
||||
|
||||
def albums
|
||||
@albums ||= Parsers::Artist.find(id).albums
|
||||
@albums ||= Parsers::Artist.find(id).albums || []
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -7,5 +7,9 @@ module Robbie
|
|||
Parsers::Track.find(id)
|
||||
end
|
||||
end
|
||||
|
||||
def artists
|
||||
@artists || []
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -36,8 +36,10 @@ module Robbie
|
|||
params = {}
|
||||
params[:id] = data["ids"]["albumId"] if data["ids"]
|
||||
params[:title] = data["title"]
|
||||
if data["album"] && data["originalReleaseDate"].is_a?(String)
|
||||
if data["originalReleaseDate"].is_a?(String)
|
||||
params[:year] = data["originalReleaseDate"].split("-").first
|
||||
elsif data["year"].is_a?(String)
|
||||
params[:year] = data["year"].split("-").first
|
||||
end
|
||||
|
||||
Robbie::Album.new(params)
|
||||
|
|
|
@ -4,6 +4,7 @@ module Robbie
|
|||
include HTTParty
|
||||
base_uri "api.rovicorp.com"
|
||||
format :json
|
||||
@@calls = []
|
||||
|
||||
class << self
|
||||
def sig
|
||||
|
@ -25,15 +26,27 @@ module Robbie
|
|||
if File.exist?(cache_file)
|
||||
MultiJson.load(File.open(cache_file).read)
|
||||
else
|
||||
data = get("#{path}?#{params_str}")
|
||||
data = load("#{path}?#{params_str}")
|
||||
File.open(cache_file, "w") do |file|
|
||||
file.write(MultiJson.dump(data)) unless data.nil? or data.empty?
|
||||
end
|
||||
data
|
||||
end
|
||||
else
|
||||
get("#{path}?#{params_str}")
|
||||
end
|
||||
load("#{path}?#{params_str}")
|
||||
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
|
||||
|
|
Loading…
Reference in New Issue