Skip to content

Commit

Permalink
Merge pull request #33 from rightsup/fix-recording
Browse files Browse the repository at this point in the history
Adds Recording::find & moves Track::search into Recording::search
  • Loading branch information
localhots committed May 13, 2015
2 parents 426fed8 + f85f74b commit c297969
Show file tree
Hide file tree
Showing 42 changed files with 168 additions and 43 deletions.
4 changes: 4 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
source 'https://rubygems.org'
gemspec

group :test do
gem "debugger"
end
20 changes: 18 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions lib/mb.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
# -*- encoding : utf-8 -*-
require "musicbrainz"
MB = MusicBrainz
8 changes: 7 additions & 1 deletion lib/musicbrainz.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# -*- encoding : utf-8 -*-
#!/bin/env ruby
# encoding: utf-8

require "digest/sha1"
require "fileutils"
require "date"
Expand All @@ -20,6 +24,7 @@
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"
Expand All @@ -32,7 +37,8 @@
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"
Expand Down
2 changes: 1 addition & 1 deletion lib/musicbrainz/bindings/artist.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# encoding: UTF-8
# -*- encoding : utf-8 -*-
module MusicBrainz
module Bindings
module Artist
Expand Down
1 change: 1 addition & 0 deletions lib/musicbrainz/bindings/artist_release_groups.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# -*- encoding : utf-8 -*-
module MusicBrainz
module Bindings
module ArtistReleaseGroups
Expand Down
2 changes: 1 addition & 1 deletion lib/musicbrainz/bindings/artist_search.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# encoding: UTF-8
# -*- encoding : utf-8 -*-
module MusicBrainz
module Bindings
module ArtistSearch
Expand Down
20 changes: 20 additions & 0 deletions lib/musicbrainz/bindings/recording.rb
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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|
{
Expand Down
1 change: 1 addition & 0 deletions lib/musicbrainz/bindings/release.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# -*- encoding : utf-8 -*-
module MusicBrainz
module Bindings
module Release
Expand Down
1 change: 1 addition & 0 deletions lib/musicbrainz/bindings/release_group.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# -*- encoding : utf-8 -*-
module MusicBrainz
module Bindings
module ReleaseGroup
Expand Down
1 change: 1 addition & 0 deletions lib/musicbrainz/bindings/release_group_releases.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# -*- encoding : utf-8 -*-
module MusicBrainz
module Bindings
module ReleaseGroupReleases
Expand Down
2 changes: 1 addition & 1 deletion lib/musicbrainz/bindings/release_group_search.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# encoding: UTF-8
# -*- encoding : utf-8 -*-
module MusicBrainz
module Bindings
module ReleaseGroupSearch
Expand Down
1 change: 1 addition & 0 deletions lib/musicbrainz/bindings/release_tracks.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# -*- encoding : utf-8 -*-
module MusicBrainz
module Bindings
module ReleaseTracks
Expand Down
1 change: 1 addition & 0 deletions lib/musicbrainz/bindings/track.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# -*- encoding : utf-8 -*-
module MusicBrainz
module Bindings
module Track
Expand Down
1 change: 1 addition & 0 deletions lib/musicbrainz/client.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# -*- encoding : utf-8 -*-
module MusicBrainz
class Client
include ClientModules::TransparentProxy
Expand Down
1 change: 1 addition & 0 deletions lib/musicbrainz/client_modules/caching_proxy.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# -*- encoding : utf-8 -*-
module MusicBrainz
module ClientModules
module CachingProxy
Expand Down
1 change: 1 addition & 0 deletions lib/musicbrainz/client_modules/failsafe_proxy.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# -*- encoding : utf-8 -*-
module MusicBrainz
module ClientModules
module FailsafeProxy
Expand Down
1 change: 1 addition & 0 deletions lib/musicbrainz/client_modules/transparent_proxy.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# -*- encoding : utf-8 -*-
module MusicBrainz
module ClientModules
module TransparentProxy
Expand Down
1 change: 1 addition & 0 deletions lib/musicbrainz/configuration.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# -*- encoding : utf-8 -*-
module MusicBrainz
class Configuration
attr_accessor :app_name, :app_version, :contact,
Expand Down
1 change: 1 addition & 0 deletions lib/musicbrainz/deprecated.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# -*- encoding : utf-8 -*-
module MusicBrainz
module Deprecated
module ProxyConfig
Expand Down
1 change: 1 addition & 0 deletions lib/musicbrainz/middleware.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# -*- encoding : utf-8 -*-
module MusicBrainz
class Middleware < Faraday::Middleware
def call(env)
Expand Down
1 change: 1 addition & 0 deletions lib/musicbrainz/models/artist.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# -*- encoding : utf-8 -*-
module MusicBrainz
class Artist < BaseModel
field :id, String
Expand Down
14 changes: 12 additions & 2 deletions lib/musicbrainz/models/base_model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,16 @@ def client
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
Expand All @@ -53,6 +58,11 @@ def escape_strings(hash)
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
Expand Down
20 changes: 20 additions & 0 deletions lib/musicbrainz/models/recording.rb
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions lib/musicbrainz/models/release.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# -*- encoding : utf-8 -*-
module MusicBrainz
class Release < BaseModel
field :id, String
Expand Down
1 change: 1 addition & 0 deletions lib/musicbrainz/models/release_group.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# -*- encoding : utf-8 -*-
module MusicBrainz
class ReleaseGroup < BaseModel
field :id, String
Expand Down
5 changes: 1 addition & 4 deletions lib/musicbrainz/models/track.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# -*- encoding : utf-8 -*-
module MusicBrainz
class Track < BaseModel
field :position, Integer
Expand All @@ -12,10 +13,6 @@ def find(id)
create_model: :track
})
end

def search(artist_name, track_name)
super({artist: artist_name, recording: track_name})
end
end
end
end
1 change: 1 addition & 0 deletions lib/musicbrainz/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# -*- encoding : utf-8 -*-
module MusicBrainz
VERSION = "0.7.7"
end
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# -*- encoding: utf-8 -*-
# -*- encoding : utf-8 -*-

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 [
{
Expand Down
2 changes: 1 addition & 1 deletion spec/bindings/release_group_search_spec.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# -*- encoding: utf-8 -*-
# -*- encoding : utf-8 -*-

require "spec_helper"

Expand Down
2 changes: 1 addition & 1 deletion spec/bindings/release_spec.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# -*- encoding: utf-8 -*-
# -*- encoding : utf-8 -*-

require "spec_helper"

Expand Down
2 changes: 1 addition & 1 deletion spec/client_modules/cache_spec.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# encoding: utf-8
# -*- encoding : utf-8 -*-

require "ostruct"
require "spec_helper"
Expand Down
2 changes: 1 addition & 1 deletion spec/deprecated/cache_config_spec.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# -*- encoding: utf-8 -*-
# -*- encoding : utf-8 -*-

require "spec_helper"

Expand Down
2 changes: 1 addition & 1 deletion spec/deprecated/proxy_config_spec.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# -*- encoding: utf-8 -*-
# -*- encoding : utf-8 -*-

require "spec_helper"

Expand Down
2 changes: 1 addition & 1 deletion spec/models/artist_spec.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# -*- encoding: utf-8 -*-
# -*- encoding : utf-8 -*-

require "spec_helper"

Expand Down
2 changes: 1 addition & 1 deletion spec/models/base_model_spec.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# -*- encoding: utf-8 -*-
# -*- encoding : utf-8 -*-

require "spec_helper"

Expand Down
32 changes: 32 additions & 0 deletions spec/models/recording_spec.rb
Original file line number Diff line number Diff line change
@@ -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
3 changes: 1 addition & 2 deletions spec/models/release_group_spec.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# -*- encoding: utf-8 -*-
# -*- encoding : utf-8 -*-

require "spec_helper"

Expand Down Expand Up @@ -61,7 +61,6 @@
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
Expand Down
2 changes: 1 addition & 1 deletion spec/models/release_spec.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# -*- encoding: utf-8 -*-
# -*- encoding : utf-8 -*-

require "spec_helper"

Expand Down
Loading

0 comments on commit c297969

Please sign in to comment.