Class: Musicbrainz::Wrapper
- Inherits:
-
Object
- Object
- Musicbrainz::Wrapper
- Defined in:
- lib/wrapper/wrapper.rb
Constant Summary collapse
- @@api_url =
"http://musicbrainz.org/ws/2/"
- @@cover_art_archive_url =
"http://archive.org/download/"
Instance Attribute Summary collapse
-
#password ⇒ Object
Returns the value of attribute password.
-
#username ⇒ Object
Returns the value of attribute username.
Class Method Summary collapse
- .append_inc(url, hash) ⇒ Object
-
.create_query_string(start, params) ⇒ Object
releases(:artist => “The Rolling Stones”, :release => “Sticky Fingers”, :inc => => true, :artist-rels => true) release/?query=artist:The%20Rolling%20Stones+release=Sticky%20Fingers&inc=isrcs&fmt=json release(:id => “id”, :inc => => true, :artist-rels => true) recording/8d2338a2-4acc-4d99-b2a7-1151f428cab6?inc=isrcs+artist-rels&fmt=json.
- .hash_to_query_string(hash) ⇒ Object
- .inc_values_array(hash) ⇒ Object
- .parse_response(response, type) ⇒ Object
- .query_array(hash) ⇒ Object
- .question_mark_or_ampersand(string) ⇒ Object
- .value_or_default(value, default) ⇒ Object
Instance Method Summary collapse
-
#artist(params) ⇒ Object
artist, label, recording, release, release-group, work, area, url, isrc, iswc, discid discids, media, isrcs, artist-credits, various-artists, aliases, annotation, tags, ratings, user-tags, user-ratings artist-rels, label-rels, recording-rels, release-rels, release-group-rels, url-rels, work-rels recording-level-rels, work-level-rels.
- #artists(params) ⇒ Object
-
#cover_art_archive_query(id) ⇒ Object
Cover Art Archive.
-
#initialize(args) ⇒ Wrapper
constructor
A new instance of Wrapper.
- #label(params) ⇒ Object
- #labels(params) ⇒ Object
-
#query(ending, type) ⇒ Object
Hit Musicbrainz API.
- #recording(params) ⇒ Object
- #recordings(params) ⇒ Object
- #release(params) ⇒ Object
- #release_group(params) ⇒ Object
- #release_groups(params) ⇒ Object
- #releases(params) ⇒ Object
- #send_query(query) ⇒ Object
- #work(params) ⇒ Object
- #works(params) ⇒ Object
Constructor Details
#initialize(args) ⇒ Wrapper
Returns a new instance of Wrapper.
14 15 16 17 18 |
# File 'lib/wrapper/wrapper.rb', line 14 def initialize args args.each do |k, v| instance_variable_set("@#{k}", v) unless v.nil? end end |
Instance Attribute Details
#password ⇒ Object
Returns the value of attribute password.
7 8 9 |
# File 'lib/wrapper/wrapper.rb', line 7 def password @password end |
#username ⇒ Object
Returns the value of attribute username.
7 8 9 |
# File 'lib/wrapper/wrapper.rb', line 7 def username @username end |
Class Method Details
.append_inc(url, hash) ⇒ Object
156 157 158 159 160 161 162 |
# File 'lib/wrapper/wrapper.rb', line 156 def self.append_inc(url, hash) if hash[:inc] "#{url}#{question_mark_or_ampersand(url)}inc=#{inc_values_array(hash[:inc]).join('+')}" else url end end |
.create_query_string(start, params) ⇒ Object
releases(:artist => “The Rolling Stones”, :release => “Sticky Fingers”, :inc => => true, :artist-rels => true) release/?query=artist:The%20Rolling%20Stones+release=Sticky%20Fingers&inc=isrcs&fmt=json release(:id => “id”, :inc => => true, :artist-rels => true) recording/8d2338a2-4acc-4d99-b2a7-1151f428cab6?inc=isrcs+artist-rels&fmt=json
127 128 129 130 131 132 133 134 |
# File 'lib/wrapper/wrapper.rb', line 127 def self.create_query_string(start, params) if params.is_a?(Hash) && params.count > 0 ending = hash_to_query_string(params) "#{start}/#{ending}#{question_mark_or_ampersand(ending)}fmt=json&limit=#{value_or_default(params[:limit], 25)}&offset=#{value_or_default(params[:offset], 0)}" else "#{start}/" end end |
.hash_to_query_string(hash) ⇒ Object
136 137 138 139 140 141 142 143 144 |
# File 'lib/wrapper/wrapper.rb', line 136 def self.hash_to_query_string(hash) if hash[:id] string = hash[:id] else string = '?query=' string += query_array(hash).join('+') end append_inc(string, hash) end |
.inc_values_array(hash) ⇒ Object
164 165 166 167 168 169 170 |
# File 'lib/wrapper/wrapper.rb', line 164 def self.inc_values_array(hash) array = [] hash.each do |k, v| array << k.to_s.gsub('_', '-') if v == true end array end |
.parse_response(response, type) ⇒ Object
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 |
# File 'lib/wrapper/wrapper.rb', line 203 def self.parse_response(response, type) if response.is_a?(Net::HTTPOK) json = JSON.parse(response.body) rescue {} if type == :multiple if json["artist"] json["artist"] elsif json["labels"] json["labels"] elsif json["release-groups"] json["release-groups"] elsif json["releases"] json["releases"] elsif json["recording"] json["recording"] elsif json["work"] json["work"] else json end else json end end end |
.query_array(hash) ⇒ Object
146 147 148 149 150 151 152 153 154 |
# File 'lib/wrapper/wrapper.rb', line 146 def self.query_array(hash) array = [] hash.each do |k, v| if k != :inc && k != :limit && k != :offset array << "#{k}:#{URI.encode(v, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]"))}" end end array end |
.question_mark_or_ampersand(string) ⇒ Object
172 173 174 175 176 177 178 |
# File 'lib/wrapper/wrapper.rb', line 172 def self.question_mark_or_ampersand(string) if string.include?('?') '&' else '?' end end |
.value_or_default(value, default) ⇒ Object
180 181 182 183 184 185 186 |
# File 'lib/wrapper/wrapper.rb', line 180 def self.value_or_default(value, default) if value value else default end end |
Instance Method Details
#artist(params) ⇒ Object
artist, label, recording, release, release-group, work, area, url, isrc, iswc, discid discids, media, isrcs, artist-credits, various-artists, aliases, annotation, tags, ratings, user-tags, user-ratings artist-rels, label-rels, recording-rels, release-rels, release-group-rels, url-rels, work-rels recording-level-rels, work-level-rels
25 26 27 28 29 |
# File 'lib/wrapper/wrapper.rb', line 25 def artist(params) # artist(:id => "id", :inc => {:isrcs => true, :artist-rels => true}) response = self.query(Musicbrainz::Wrapper.create_query_string('artist', params), :single) Musicbrainz::Artist.new(response) if !response.nil? end |
#artists(params) ⇒ Object
31 32 33 34 35 36 37 38 39 40 |
# File 'lib/wrapper/wrapper.rb', line 31 def artists(params) response = self.query(Musicbrainz::Wrapper.create_query_string('artist', params), :multiple) if !response.nil? array = [] response.each do |a| array << Musicbrainz::Artist.new(a) end array end end |
#cover_art_archive_query(id) ⇒ Object
Cover Art Archive
229 230 231 232 233 234 |
# File 'lib/wrapper/wrapper.rb', line 229 def cover_art_archive_query(id) require 'open-uri' query = "#{@@cover_art_archive_url}mbid-#{id}/index.json" file = open(query).read() rescue nil json = JSON.parse(file) rescue {} end |
#label(params) ⇒ Object
42 43 44 45 |
# File 'lib/wrapper/wrapper.rb', line 42 def label(params) response = self.query(Musicbrainz::Wrapper.create_query_string('label', params), :single) Musicbrainz::Label.new(response) if !response.nil? end |
#labels(params) ⇒ Object
47 48 49 50 51 52 53 54 55 56 |
# File 'lib/wrapper/wrapper.rb', line 47 def labels(params) response = self.query(Musicbrainz::Wrapper.create_query_string('label', params), :multiple) if !response.nil? array = [] response.each do |a| array << Musicbrainz::Label.new(a) end array end end |
#query(ending, type) ⇒ Object
Hit Musicbrainz API
189 190 191 192 193 |
# File 'lib/wrapper/wrapper.rb', line 189 def query(ending, type) query = @@api_url + ending response = self.send_query(query) Musicbrainz::Wrapper.parse_response(response, type) end |
#recording(params) ⇒ Object
58 59 60 61 |
# File 'lib/wrapper/wrapper.rb', line 58 def recording(params) response = self.query(Musicbrainz::Wrapper.create_query_string('recording', params), :single) Musicbrainz::Recording.new(response) if !response.nil? end |
#recordings(params) ⇒ Object
63 64 65 66 67 68 69 70 71 72 |
# File 'lib/wrapper/wrapper.rb', line 63 def recordings(params) response = self.query(Musicbrainz::Wrapper.create_query_string('recording', params), :multiple) if !response.nil? array = [] response.each do |a| array << Musicbrainz::Recording.new(a) end array end end |
#release(params) ⇒ Object
74 75 76 77 |
# File 'lib/wrapper/wrapper.rb', line 74 def release(params) response = self.query(Musicbrainz::Wrapper.create_query_string('release', params), :single) Musicbrainz::Release.new(response) if !response.nil? end |
#release_group(params) ⇒ Object
90 91 92 93 |
# File 'lib/wrapper/wrapper.rb', line 90 def release_group(params) response = self.query(Musicbrainz::Wrapper.create_query_string('release-group', params), :single) Musicbrainz::ReleaseGroup.new(response) if !response.nil? end |
#release_groups(params) ⇒ Object
95 96 97 98 99 100 101 102 103 104 |
# File 'lib/wrapper/wrapper.rb', line 95 def release_groups(params) response = self.query(Musicbrainz::Wrapper.create_query_string('release-group', params), :multiple) if !response.nil? array = [] response.each do |a| array << Musicbrainz::ReleaseGroup.new(a) end array end end |
#releases(params) ⇒ Object
79 80 81 82 83 84 85 86 87 88 |
# File 'lib/wrapper/wrapper.rb', line 79 def releases(params) response = self.query(Musicbrainz::Wrapper.create_query_string('release', params), :multiple) if !response.nil? array = [] response.each do |a| array << Musicbrainz::Release.new(a) end array end end |
#send_query(query) ⇒ Object
195 196 197 198 199 200 201 |
# File 'lib/wrapper/wrapper.rb', line 195 def send_query(query) sleep 1 uri = URI.parse(query) http = Net::HTTP.new(uri.host, uri.port) request = Net::HTTP::Get.new(uri.request_uri) http.request(request) end |
#work(params) ⇒ Object
106 107 108 109 |
# File 'lib/wrapper/wrapper.rb', line 106 def work(params) response = self.query(Musicbrainz::Wrapper.create_query_string('work', params), :single) Musicbrainz::Work.new(response) if !response.nil? end |
#works(params) ⇒ Object
111 112 113 114 115 116 117 118 119 120 |
# File 'lib/wrapper/wrapper.rb', line 111 def works(params) response = self.query(Musicbrainz::Wrapper.create_query_string('work', params), :multiple) if !response.nil? array = [] response.each do |a| array << Musicbrainz::Work.new(a) end array end end |