created new track_search binding to allow searching for tracks (really recordings). Modified existing model classes to call super() to handle search (open/closed principle, attempting to reduce duplicate code). Fixed xml namespace issue in specs.

This commit is contained in:
Thomas Wolfe
2013-05-05 18:14:44 -05:00
parent 687ce1b785
commit d8184565a2
13 changed files with 96 additions and 26 deletions
+1
View File
@@ -29,6 +29,7 @@ require "musicbrainz/bindings/release_group_releases"
require "musicbrainz/bindings/release"
require "musicbrainz/bindings/release_tracks"
require "musicbrainz/bindings/track"
require "musicbrainz/bindings/track_search"
module MusicBrainz
GH_PAGE_URL = "http://git.io/brainz"
+21
View File
@@ -0,0 +1,21 @@
# encoding: UTF-8
module MusicBrainz
module Bindings
module TrackSearch
def parse(xml)
xml.xpath('./recording-list/recording').map do |xml|
{
id: (xml.attribute('id').value rescue nil),
mbid: (xml.attribute('id').value rescue nil), # Old shit
title: (xml.xpath('./title').text.gsub(/[`]/, "'") rescue nil),
artist: (xml.xpath('./artist-credit/name-credit/artist/name').text rescue nil),
releases: (xml.xpath('./release-list/release/title').map{ |xml| xml.text } rescue []),
score: (xml.attribute('score').value.to_i rescue nil)
} rescue nil
end.delete_if{ |item| item.nil? }
end
extend self
end
end
end
+1 -5
View File
@@ -25,11 +25,7 @@ module MusicBrainz
end
def search(name)
name = CGI.escape(name).gsub(/\!/, '\!')
client.load(:artist, { query: "artist:#{name}", limit: 10 }, {
binding: :artist_search
})
super({artist: name})
end
def discography(mbid)
+32
View File
@@ -23,6 +23,38 @@ module MusicBrainz
def client
MusicBrainz.client
end
def search(hash)
hash = escape_strings(hash)
query_val = build_query(hash)
underscore_name = self.name[13..-1].underscore
client.load(underscore_name.to_sym, { query: query_val, limit: 10 }, { binding: underscore_name.insert(-1,"_search").to_sym })
end
class ::String
def underscore
self.gsub(/::/, '/').
gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
gsub(/([a-z\d])([A-Z])/,'\1_\2').
tr("-", "_").
downcase
end
end
def build_query(hash)
return ["#{hash.first[0].to_s}:\"#{hash.first[1]}\""] if hash.size ==1
arr ||= []
hash.each { |k, v| arr << "#{k.to_s}:\"#{hash[k]}\"" }
arr.join(' AND ')
end
def escape_strings(hash)
hash.each { |k, v| hash[k] = CGI.escape(v).gsub(/\!/, '\!') }
hash
end
# these probably should be private... but I'm not sure how to get it to work in a module...
# private_class_method :build_query, :escape_strings
end
module InstanceMethods
+8 -12
View File
@@ -25,20 +25,16 @@ module MusicBrainz
})
end
def search(artist_name, title, options = {})
artist_name = CGI.escape(artist_name).gsub(/\!/, '\!')
title = CGI.escape(title).gsub(/\!/, '\!')
query = ["artist:\"#{artist_name}\"", "releasegroup:\"#{title}\""]
query << "type: #{options[:type]}" if options[:type]
client.load(
:release_group, { query: query.join(' AND '), limit: 10 },
{ binding: :release_group_search }
)
def search(artist_name, title, type = nil)
if type
super({artist: artist_name, releasegroup: title, type: type})
else
super({artist: artist_name, releasegroup: title})
end
end
def find_by_artist_and_title(artist_name, title, options = {})
matches = search(artist_name, title, options)
def find_by_artist_and_title(artist_name, title, type = nil )
matches = search(artist_name, title, type)
matches.empty? ? nil : find(matches.first[:id])
end
end
+4
View File
@@ -12,6 +12,10 @@ module MusicBrainz
create_model: :track
})
end
def search(artist_name, track_name)
super({artist: artist_name, recording: track_name})
end
end
end
end