Class: Bandcamp::Request

Inherits:
Object
  • Object
show all
Defined in:
lib/bandcamp/request.rb

Overview

A helper class for assembling a uri to query the API

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key) ⇒ Request

Returns a new instance of Request.



13
14
15
16
# File 'lib/bandcamp/request.rb', line 13

def initialize api_key
  @uri = URI('http://api.bandcamp.com')
  @api_key = api_key
end

Instance Attribute Details

#api_keyObject (readonly)

Returns the value of attribute api_key.



11
12
13
# File 'lib/bandcamp/request.rb', line 11

def api_key
  @api_key
end

Instance Method Details

#add_key_param(params) ⇒ Object



107
108
109
# File 'lib/bandcamp/request.rb', line 107

def add_key_param params
  {key: @api_key}.merge params
end

#album(albumid) ⇒ Object



37
38
39
40
41
42
# File 'lib/bandcamp/request.rb', line 37

def album albumid
  type :album
  self.query={ album_id: albumid }
  response = dispatch
  response.empty? ? nil : response
end

#band(bandid) ⇒ Object



44
45
46
47
48
49
# File 'lib/bandcamp/request.rb', line 44

def band bandid
  type :band
  self.query={ band_id: bandid }
  response = dispatch
  response["error"] ? nil : response
end

#discography(bandid) ⇒ Object



58
59
60
61
62
63
# File 'lib/bandcamp/request.rb', line 58

def discography bandid
  type :discography
  self.query = {band_id: bandid}
  response = dispatch
  response["discography"].empty? ? nil : response["discography"]
end

#dispatchObject



111
112
113
# File 'lib/bandcamp/request.rb', line 111

def dispatch
  MultiJson.decode(get(@uri))
end

#generate_params(params) ⇒ Object



103
104
105
# File 'lib/bandcamp/request.rb', line 103

def generate_params params
  add_key_param(params).map{|key,val| "#{URI.encode(key.to_s)}=#{URI.encode(val.to_s)}"}.join('&')
end

#hostObject



18
19
20
# File 'lib/bandcamp/request.rb', line 18

def host
  @uri.host
end

#pathObject



22
23
24
# File 'lib/bandcamp/request.rb', line 22

def path
  @uri.path
end

#path=(path) ⇒ Object



26
27
28
# File 'lib/bandcamp/request.rb', line 26

def path= path
  @uri.path = path
end

#queryObject



99
100
101
# File 'lib/bandcamp/request.rb', line 99

def query
  @uri.query
end

#query=(params) ⇒ Object



95
96
97
# File 'lib/bandcamp/request.rb', line 95

def query= params
  @uri.query = generate_params(params)
end

#search(name) ⇒ Object



65
66
67
68
69
70
# File 'lib/bandcamp/request.rb', line 65

def search name
  type :search
  self.query = {name: name}
  response = dispatch
  response["results"].empty? ? nil : response["results"]
end

#track(trackid) ⇒ Object



30
31
32
33
34
35
# File 'lib/bandcamp/request.rb', line 30

def track trackid
  self.type :track
  self.query = { track_id: trackid }
  response = dispatch
  response.empty? ? nil : response
end

#type(req_type) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/bandcamp/request.rb', line 72

def type req_type
  self.path = case req_type
              when :track
                '/api/track/3/info'
              when :album
                '/api/album/2/info'
              when :discography
                '/api/band/3/discography'
              when :search
                '/api/band/3/search'
              when :band
                '/api/band/3/info'
              when :url
                '/api/url/1/info'
              else
                raise UnknownTypeError, "The Bandcamp API does not support this type of request."
              end
end

#uriObject



91
92
93
# File 'lib/bandcamp/request.rb', line 91

def uri
  @uri.to_s
end

#url(bandcamp_url) ⇒ Object



51
52
53
54
55
56
# File 'lib/bandcamp/request.rb', line 51

def url bandcamp_url
  type :url
  self.query = {url: bandcamp_url}
  response = dispatch
  response["error"] ? nil : response
end