Class: Coverart::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/coverart/client.rb

Constant Summary collapse

ENDPOINT =
"http://coverartarchive.org"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ Client

Returns a new instance of Client.



12
13
14
15
16
17
# File 'lib/coverart/client.rb', line 12

def initialize(&block)
  @http ||= Faraday.new(url: ENDPOINT) do |connection|
    connection.adapter Faraday.default_adapter
    yield connection if block
  end
end

Instance Attribute Details

#httpObject (readonly)

Returns the value of attribute http.



10
11
12
# File 'lib/coverart/client.rb', line 10

def http
  @http
end

Instance Method Details

#back(mbid) ⇒ Object



40
41
42
# File 'lib/coverart/client.rb', line 40

def back mbid
  get("/release/#{mbid}/back")
end

#front(mbid) ⇒ Object



36
37
38
# File 'lib/coverart/client.rb', line 36

def front mbid
  get("/release/#{mbid}/front")
end

#get(url) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/coverart/client.rb', line 19

def get(url)
  response = http.get(url)

  case response.status
  when 404 then nil
  when 200 then JSON.parse response.body
  when 300..310
    if response.headers[:location].match?(/\.(jpg|jpeg|png|gif)/)
      response.headers[:location]
    else
      get response.headers[:location]
    end
  else
    raise "Unexpected response (#{response.status}) at #{url}"
  end
end

#group(mbid) ⇒ Object



44
45
46
# File 'lib/coverart/client.rb', line 44

def group mbid
  get("/release-group/#{mbid}/front")
end