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 = 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")
|
||||||
|
|
|
@ -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
|
||||||
|
|
|
@ -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
|
||||||
|
|
|
@ -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
|
||||||
|
|
|
@ -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
|
||||||
|
|
|
@ -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)
|
||||||
|
|
|
@ -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,15 +26,27 @@ 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
|
||||||
|
|
||||||
|
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
|
||||||
|
|
Loading…
Reference in New Issue