class Musicbrainz::Wrapper
Attributes
password[RW]
username[RW]
Public Class Methods
append_inc(url, hash)
click to toggle source
# 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)
click to toggle source
Query String releases(:artist => “The Rolling Stones”, :release => “Sticky Fingers”, :inc => {:isrcs => true, :artist-rels => true}) release/?query=artist:The%20Rolling%20Stones+release=Sticky%20Fingers&inc=isrcs&fmt=json release(:id => “id”, :inc => {:isrcs => true, :artist-rels => true}) recording/8d2338a2-4acc-4d99-b2a7-1151f428cab6?inc=isrcs+artist-rels&fmt=json
# 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)
click to toggle source
# 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)
click to toggle source
# 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
new(args)
click to toggle source
# 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
parse_response(response, type)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# File lib/wrapper/wrapper.rb, line 180 def self.value_or_default(value, default) if value value else default end end
Public Instance Methods
artist(params)
click to toggle source
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
# 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)
click to toggle source
# 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)
click to toggle source
Cover Art Archive
# 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)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
Hit Musicbrainz
API
# 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)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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