1
0
Fork 0

Merge pull request #33 from rightsup/fix-recording

Adds Recording::find & moves Track::search into Recording::search
This commit is contained in:
Gregory Eremin 2015-05-13 12:52:59 +03:00
commit c2979690e0
42 changed files with 168 additions and 43 deletions

View File

@ -1,2 +1,6 @@
source 'https://rubygems.org'
gemspec
group :test do
gem "debugger"
end

View File

@ -121,11 +121,10 @@ MusicBrainz::Release.find(id)
}
```
MusicBrainz::Track
MusicBrainz::Track (depreciated, now called Recording)
```ruby
# Class Methods
MusicBrainz::Track.find(id)
MusicBrainz::ReleaseGroup.search(artist_name, track_name)
# Fields
{
@ -136,6 +135,23 @@ MusicBrainz::ReleaseGroup.search(artist_name, track_name)
}
```
MusicBrainz::Recording
```ruby
# Class Methods
MusicBrainz::Recording.find(id)
MusicBrainz::Recording.search(track_name, artist_name)
# Fields
{
:id => Integer,
:mbid => Integer,
:title => String,
:artist => String,
:releases => String,
:score => Integer
}
```
### Testing
```
bundle exec rspec

View File

@ -1,2 +1,3 @@
# -*- encoding : utf-8 -*-
require "musicbrainz"
MB = MusicBrainz

View File

@ -1,3 +1,7 @@
# -*- encoding : utf-8 -*-
#!/bin/env ruby
# encoding: utf-8
require "digest/sha1"
require "fileutils"
require "date"
@ -20,6 +24,7 @@ require "musicbrainz/models/artist"
require "musicbrainz/models/release_group"
require "musicbrainz/models/release"
require "musicbrainz/models/track"
require "musicbrainz/models/recording"
require "musicbrainz/bindings/artist"
require "musicbrainz/bindings/artist_search"
@ -32,7 +37,8 @@ require "musicbrainz/bindings/release_group_releases"
require "musicbrainz/bindings/release"
require "musicbrainz/bindings/release_tracks"
require "musicbrainz/bindings/track"
require "musicbrainz/bindings/track_search"
require "musicbrainz/bindings/recording"
require "musicbrainz/bindings/recording_search"
module MusicBrainz
GH_PAGE_URL = "http://git.io/brainz"

View File

@ -1,4 +1,4 @@
# encoding: UTF-8
# -*- encoding : utf-8 -*-
module MusicBrainz
module Bindings
module Artist

View File

@ -1,3 +1,4 @@
# -*- encoding : utf-8 -*-
module MusicBrainz
module Bindings
module ArtistReleaseGroups

View File

@ -1,4 +1,4 @@
# encoding: UTF-8
# -*- encoding : utf-8 -*-
module MusicBrainz
module Bindings
module ArtistSearch

View File

@ -0,0 +1,20 @@
# -*- encoding : utf-8 -*-
module MusicBrainz
module Bindings
module Recording
def parse(xml)
xml = xml.xpath('./recording') unless xml.xpath('./recording').empty?
{
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)
}
end
extend self
end
end
end

View File

@ -1,7 +1,7 @@
# encoding: UTF-8
# -*- encoding : utf-8 -*-
module MusicBrainz
module Bindings
module TrackSearch
module RecordingSearch
def parse(xml)
xml.xpath('./recording-list/recording').map do |xml|
{

View File

@ -1,3 +1,4 @@
# -*- encoding : utf-8 -*-
module MusicBrainz
module Bindings
module Release

View File

@ -1,3 +1,4 @@
# -*- encoding : utf-8 -*-
module MusicBrainz
module Bindings
module ReleaseGroup

View File

@ -1,3 +1,4 @@
# -*- encoding : utf-8 -*-
module MusicBrainz
module Bindings
module ReleaseGroupReleases

View File

@ -1,4 +1,4 @@
# encoding: UTF-8
# -*- encoding : utf-8 -*-
module MusicBrainz
module Bindings
module ReleaseGroupSearch

View File

@ -1,3 +1,4 @@
# -*- encoding : utf-8 -*-
module MusicBrainz
module Bindings
module ReleaseTracks

View File

@ -1,3 +1,4 @@
# -*- encoding : utf-8 -*-
module MusicBrainz
module Bindings
module Track

View File

@ -1,3 +1,4 @@
# -*- encoding : utf-8 -*-
module MusicBrainz
class Client
include ClientModules::TransparentProxy

View File

@ -1,3 +1,4 @@
# -*- encoding : utf-8 -*-
module MusicBrainz
module ClientModules
module CachingProxy

View File

@ -1,3 +1,4 @@
# -*- encoding : utf-8 -*-
module MusicBrainz
module ClientModules
module FailsafeProxy

View File

@ -1,3 +1,4 @@
# -*- encoding : utf-8 -*-
module MusicBrainz
module ClientModules
module TransparentProxy

View File

@ -1,3 +1,4 @@
# -*- encoding : utf-8 -*-
module MusicBrainz
class Configuration
attr_accessor :app_name, :app_version, :contact,

View File

@ -1,3 +1,4 @@
# -*- encoding : utf-8 -*-
module MusicBrainz
module Deprecated
module ProxyConfig

View File

@ -1,3 +1,4 @@
# -*- encoding : utf-8 -*-
module MusicBrainz
class Middleware < Faraday::Middleware
def call(env)

View File

@ -1,3 +1,4 @@
# -*- encoding : utf-8 -*-
module MusicBrainz
class Artist < BaseModel
field :id, String

View File

@ -24,11 +24,16 @@ module MusicBrainz
MusicBrainz.client
end
def find(hash)
underscored_name = underscore_name.to_sym
client.load(underscored_name, hash, { binding: underscored_name, create_model: underscored_name })
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 })
underscored_name = underscore_name
client.load(underscored_name.to_sym, { query: query_val, limit: 10 }, { binding: underscored_name.insert(-1,"_search").to_sym })
end
class ::String
@ -53,6 +58,11 @@ module MusicBrainz
hash
end
def underscore_name
# self.name[13..-1] => removes MusicBrainz::
self.name[13..-1].underscore
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

View File

@ -0,0 +1,20 @@
module MusicBrainz
class Recording < BaseModel
field :id, String
field :mbid, String
field :title, String
field :artist, String
field :releases, String
field :score, Integer
class << self
def find(id)
super({ id: id })
end
def search(track_name, artist_name)
super({recording: track_name, artist: artist_name})
end
end
end
end

View File

@ -1,3 +1,4 @@
# -*- encoding : utf-8 -*-
module MusicBrainz
class Release < BaseModel
field :id, String

View File

@ -1,3 +1,4 @@
# -*- encoding : utf-8 -*-
module MusicBrainz
class ReleaseGroup < BaseModel
field :id, String

View File

@ -1,3 +1,4 @@
# -*- encoding : utf-8 -*-
module MusicBrainz
class Track < BaseModel
field :position, Integer
@ -12,10 +13,6 @@ module MusicBrainz
create_model: :track
})
end
def search(artist_name, track_name)
super({artist: artist_name, recording: track_name})
end
end
end
end

View File

@ -1,3 +1,4 @@
# -*- encoding : utf-8 -*-
module MusicBrainz
VERSION = "0.7.7"
end

View File

@ -2,9 +2,9 @@
require "spec_helper"
describe MusicBrainz::Bindings::TrackSearch do
describe MusicBrainz::Bindings::RecordingSearch do
describe '.parse' do
it "gets correct Track (really recording) data" do
it "gets correct Recording data" do
response = '<metadata xmlns="http://musicbrainz.org/ns/mmd-2.0#" xmlns:ext="http://musicbrainz.org/ns/ext#-2.0"><recording-list offset="0" count="1"><recording id="0b382a13-32f0-4743-9248-ba5536a6115e" ext:score="100"><title>King Fred</title><artist-credit><name-credit><artist id="f52f7a92-d495-4d32-89e7-8b1e5b8541c8"><name>Too Much Joy</name></artist></name-credit></artist-credit><release-list><release id="8442e42b-c40a-4817-89a0-dbe663c94d2d"><title>Green Eggs and Crack</title></release></release-list></recording></recording-list></metadata>'
expect(described_class.parse(Nokogiri::XML.parse(response).remove_namespaces!.xpath('/metadata'))).to eq [
{

View File

@ -1,4 +1,4 @@
# encoding: utf-8
# -*- encoding : utf-8 -*-
require "ostruct"
require "spec_helper"

View File

@ -0,0 +1,32 @@
# -*- encoding: utf-8 -*-
require "spec_helper"
describe MusicBrainz::Recording do
describe '.find' do
it "gets no exception while loading release info" do
expect {
MusicBrainz::Recording.find("b3015bab-1540-4d4e-9f30-14872a1525f7")
}.to_not raise_error(Exception)
end
it "gets correct instance" do
track = MusicBrainz::Recording.find("b3015bab-1540-4d4e-9f30-14872a1525f7")
expect(track).to be_an_instance_of(MusicBrainz::Recording)
end
it "gets correct track data" do
track = MusicBrainz::Recording.find("b3015bab-1540-4d4e-9f30-14872a1525f7")
expect(track.title).to eq "Empire"
end
end
describe '.search' do
it "searches tracks (aka recordings) by artist name and title" do
matches = MusicBrainz::Recording.search('Bound for the floor', 'Local H')
expect(matches.length).to be > 0
expect(matches.first[:title]).to eq "Bound for the Floor"
expect(matches.first[:artist]).to eq "Local H"
end
end
end

View File

@ -61,7 +61,6 @@ describe MusicBrainz::ReleaseGroup do
allow_any_instance_of(MusicBrainz::Client).to receive(:get_contents)
.with('http://musicbrainz.org/ws/2/release-group/6f33e0f0-cde2-38f9-9aee-2c60af8d1a61?inc=url-rels')
.and_return({ status: 200, body: response})
release_group = MusicBrainz::ReleaseGroup.find_by_artist_and_title('Kasabian', 'Empire')
expect(release_group.id).to eq '6f33e0f0-cde2-38f9-9aee-2c60af8d1a61'
end

View File

@ -3,6 +3,7 @@
require "spec_helper"
describe MusicBrainz::Track do
describe '.find' do
it "gets no exception while loading release info" do
expect {
MusicBrainz::Track.find("b3015bab-1540-4d4e-9f30-14872a1525f7")
@ -21,3 +22,4 @@ describe MusicBrainz::Track do
expect(track.length).to eq 233013
end
end
end

View File

@ -1,3 +1,4 @@
# -*- encoding : utf-8 -*-
require "rubygems"
require "bundler/setup"
require "musicbrainz"