musicbrainz 0.7.1 → 0.7.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (55) hide show
  1. data/.gitignore +1 -4
  2. data/.travis.yml +2 -2
  3. data/Contributors +1 -0
  4. data/README.md +91 -40
  5. data/lib/mb.rb +2 -0
  6. data/lib/musicbrainz.rb +27 -22
  7. data/lib/musicbrainz/bindings/artist.rb +23 -0
  8. data/lib/musicbrainz/bindings/artist_release_groups.rb +13 -0
  9. data/lib/musicbrainz/bindings/artist_search.rb +23 -0
  10. data/lib/musicbrainz/bindings/release.rb +28 -0
  11. data/lib/musicbrainz/bindings/release_group.rb +18 -0
  12. data/lib/musicbrainz/bindings/release_group_releases.rb +13 -0
  13. data/lib/musicbrainz/bindings/release_tracks.rb +13 -0
  14. data/lib/musicbrainz/bindings/track.rb +16 -0
  15. data/lib/musicbrainz/client.rb +72 -0
  16. data/lib/musicbrainz/client_modules/caching_proxy.rb +46 -0
  17. data/lib/musicbrainz/client_modules/failsafe_proxy.rb +38 -0
  18. data/lib/musicbrainz/client_modules/transparent_proxy.rb +12 -0
  19. data/lib/musicbrainz/configuration.rb +60 -0
  20. data/lib/musicbrainz/deprecated.rb +36 -0
  21. data/lib/musicbrainz/middleware.rb +23 -0
  22. data/lib/musicbrainz/models/artist.rb +47 -0
  23. data/lib/musicbrainz/models/base_model.rb +63 -0
  24. data/lib/musicbrainz/models/release.rb +27 -0
  25. data/lib/musicbrainz/models/release_group.rb +28 -0
  26. data/lib/musicbrainz/models/track.rb +17 -0
  27. data/lib/musicbrainz/version.rb +3 -0
  28. data/musicbrainz.gemspec +10 -12
  29. data/spec/bindings/release_spec.rb +43 -0
  30. data/spec/client_modules/cache_spec.rb +62 -0
  31. data/spec/deprecated/cache_config_spec.rb +32 -0
  32. data/spec/deprecated/proxy_config_spec.rb +32 -0
  33. data/spec/{requests → models}/artist_spec.rb +3 -9
  34. data/spec/{requests → models}/release_group_spec.rb +2 -2
  35. data/spec/{requests → models}/release_spec.rb +1 -1
  36. data/spec/{requests → models}/track_spec.rb +0 -0
  37. data/spec/spec_helper.rb +13 -7
  38. metadata +63 -58
  39. data/Rakefile +0 -13
  40. data/lib/deprecated.rb +0 -25
  41. data/lib/musicbrainz/artist.rb +0 -63
  42. data/lib/musicbrainz/base.rb +0 -71
  43. data/lib/musicbrainz/release.rb +0 -43
  44. data/lib/musicbrainz/release_group.rb +0 -41
  45. data/lib/musicbrainz/track.rb +0 -23
  46. data/lib/parsers/artist.rb +0 -47
  47. data/lib/parsers/base.rb +0 -41
  48. data/lib/parsers/release.rb +0 -28
  49. data/lib/parsers/release_group.rb +0 -27
  50. data/lib/parsers/track.rb +0 -18
  51. data/lib/tools/cache.rb +0 -48
  52. data/lib/tools/proxy.rb +0 -62
  53. data/lib/version.rb +0 -5
  54. data/spec/misc/deprecated_spec.rb +0 -38
  55. data/spec/tools/cache_spec.rb +0 -59
data/Rakefile DELETED
@@ -1,13 +0,0 @@
1
- #!/usr/bin/env rake
2
- require "bundler/gem_tasks"
3
- require "rspec/core/rake_task"
4
-
5
- RSpec::Core::RakeTask.new("spec")
6
-
7
- task :default => :spec
8
- task :test => :spec
9
-
10
- desc "Open an irb session preloaded with this library"
11
- task :console do
12
- sh "irb -rubygems -I lib -r musicbrainz.rb"
13
- end
data/lib/deprecated.rb DELETED
@@ -1,25 +0,0 @@
1
- # -*- encoding: utf-8 -*-
2
-
3
- module MusicBrainz
4
- class << self
5
- def query_interval
6
- $stdout.send :puts, "WARNING! MusicBrainz.query_interval is deprecated. Use MusicBrainz::Tools::Proxy.query_interval"
7
- MusicBrainz::Tools::Proxy.query_interval
8
- end
9
-
10
- def query_interval=(sec)
11
- $stdout.send :puts, "WARNING! MusicBrainz.query_interval= is deprecated. Use MusicBrainz::Tools::Proxy.query_interval"
12
- MusicBrainz::Tools::Proxy.query_interval = sec
13
- end
14
-
15
- def cache_path
16
- $stdout.send :puts, "WARNING! MusicBrainz.cache_path is deprecated. Use MusicBrainz::Tools::Cache.cache_path"
17
- MusicBrainz::Tools::Cache.cache_path
18
- end
19
-
20
- def cache_path=(path)
21
- $stdout.send :puts, "WARNING! MusicBrainz.cache_path= is deprecated. Use MusicBrainz::Tools::Cache.cache_path"
22
- MusicBrainz::Tools::Cache.cache_path = path
23
- end
24
- end
25
- end
@@ -1,63 +0,0 @@
1
- # -*- encoding: utf-8 -*-
2
-
3
- module MusicBrainz
4
- class Artist < Base
5
-
6
- field :id, String
7
- field :type, String
8
- field :name, String
9
- field :country, String
10
- field :date_begin, Time
11
- field :date_end, Time
12
- field :urls, Hash
13
-
14
- def release_groups
15
- @release_groups ||= nil
16
- if @release_groups.nil? and !id.nil?
17
- @release_groups = self.class.load({
18
- :parser => :artist_release_groups,
19
- :create_models => MusicBrainz::ReleaseGroup
20
- }, {
21
- :resource => :release_group,
22
- :artist => id
23
- })
24
- @release_groups.sort!{ |a, b| a.first_release_date <=> b.first_release_date }
25
- end
26
- @release_groups
27
- end
28
-
29
- class << self
30
- def find(mbid)
31
- load({
32
- :parser => :artist_model,
33
- :create_model => MusicBrainz::Artist
34
- }, {
35
- :resource => :artist,
36
- :id => mbid,
37
- :inc => [:url_rels]
38
- })
39
- end
40
-
41
- def search(name)
42
- load({
43
- :parser => :artist_search
44
- }, {
45
- :resource => :artist,
46
- :query => "artist:" << CGI.escape(name).gsub(/\!/, '\!'),
47
- :limit => 10
48
- })
49
- end
50
-
51
- def discography(mbid)
52
- artist = find(mbid)
53
- artist.release_groups.each { |rg| rg.releases.each { |r| r.tracks } }
54
- artist
55
- end
56
-
57
- def find_by_name(name)
58
- matches = search(name)
59
- matches.length.zero? ? nil : find(matches.first[:mbid])
60
- end
61
- end
62
- end
63
- end
@@ -1,71 +0,0 @@
1
- # -*- encoding: utf-8 -*-
2
-
3
- module MusicBrainz
4
- class Base
5
- class << self
6
- def field(name, type)
7
- @fields ||= {}
8
- @fields[name] = type
9
-
10
- define_method(name) {
11
- instance_variable_get("@#{name}")
12
- }
13
- define_method("#{name}=") { |val|
14
- instance_variable_set("@#{name}", validate_type(val, type))
15
- }
16
- end
17
-
18
- def load(params, query)
19
- parser = MusicBrainz::Parsers.get_by_name(params[:parser])
20
- xml = MusicBrainz::Tools::Proxy.query(query)
21
- result = parser[:const].send(parser[:method], Nokogiri::XML(xml))
22
- if params[:create_model]
23
- result_model = params[:create_model].new
24
- result.each { |field, value|
25
- result_model.send("#{field}=".to_sym, value)
26
- }
27
- result_model
28
- elsif params[:create_models]
29
- result_models = []
30
- result.each { |item|
31
- result_model = params[:create_models].new
32
- item.each { |field, value|
33
- result_model.send("#{field}=".to_sym, value)
34
- }
35
- result_models << result_model
36
- }
37
- result_models
38
- else
39
- result
40
- end
41
- end
42
- end
43
-
44
- def initialize
45
- self.class.instance_variable_get("@fields").each { |name, type|
46
- instance_variable_set("@#{name}", nil)
47
- }
48
- end
49
-
50
- def validate_type(val, type)
51
- if type == Integer
52
- val.to_i
53
- elsif type == Float
54
- val.to_f
55
- elsif type == String
56
- val.to_s
57
- elsif type == Time
58
- if val.nil? or val == ""
59
- val = "2030-12-31"
60
- elsif val.split("-").length == 1
61
- val << "-12-31"
62
- elsif val.split("-").length == 2
63
- val << "-31"
64
- end
65
- Time.utc(*val.split("-"))
66
- else
67
- val
68
- end
69
- end
70
- end
71
- end
@@ -1,43 +0,0 @@
1
- # -*- encoding: utf-8 -*-
2
-
3
- module MusicBrainz
4
- class Release < Base
5
-
6
- field :id, String
7
- field :title, String
8
- field :status, String
9
- field :format, String
10
- field :date, Time
11
- field :country, String
12
-
13
- def tracks
14
- @tracks ||= nil
15
- if @tracks.nil? and !id.nil?
16
- @tracks = self.class.load({
17
- :parser => :release_tracks,
18
- :create_models => MusicBrainz::Track
19
- }, {
20
- :resource => :release,
21
- :id => id,
22
- :inc => [:recordings, :media],
23
- :limit => 100
24
- })
25
- @tracks.sort{ |a, b| a.position <=> b.position }
26
- end
27
- @tracks
28
- end
29
-
30
- class << self
31
- def find(mbid)
32
- load({
33
- :parser => :release_model,
34
- :create_model => MusicBrainz::Release
35
- }, {
36
- :resource => :release,
37
- :id => mbid,
38
- :inc => [:media]
39
- })
40
- end
41
- end
42
- end
43
- end
@@ -1,41 +0,0 @@
1
- # -*- encoding: utf-8 -*-
2
-
3
- module MusicBrainz
4
- class ReleaseGroup < Base
5
-
6
- field :id, String
7
- field :type, String
8
- field :title, String
9
- field :disambiguation, String
10
- field :first_release_date, Time
11
-
12
- def releases
13
- @releases ||= nil
14
- if @releases.nil? and !id.nil?
15
- @releases = self.class.load({
16
- :parser => :release_group_releases,
17
- :create_models => MusicBrainz::Release
18
- }, {
19
- :resource => :release,
20
- :release_group => self.id,
21
- :inc => [:media],
22
- :limit => 100
23
- })
24
- @releases.sort!{ |a, b| a.date <=> b.date }
25
- end
26
- @releases
27
- end
28
-
29
- class << self
30
- def find(mbid)
31
- load({
32
- :parser => :release_group_model,
33
- :create_model => MusicBrainz::ReleaseGroup
34
- }, {
35
- :resource => :release_group,
36
- :id => mbid
37
- })
38
- end
39
- end
40
- end
41
- end
@@ -1,23 +0,0 @@
1
- # -*- encoding: utf-8 -*-
2
-
3
- module MusicBrainz
4
- class Track < Base
5
-
6
- field :position, Integer
7
- field :recording_id, String
8
- field :title, String
9
- field :length, Integer
10
-
11
- class << self
12
- def find(mbid)
13
- load({
14
- :parser => :track_model,
15
- :create_model => MusicBrainz::Track
16
- }, {
17
- :resource => :recording,
18
- :id => mbid
19
- })
20
- end
21
- end
22
- end
23
- end
@@ -1,47 +0,0 @@
1
- # -*- encoding: utf-8 -*-
2
-
3
- module MusicBrainz
4
- module Parsers
5
- class Artist < Base
6
- class << self
7
- def model(xml)
8
- res = {
9
- :id => safe_get_attr(xml, "artist", "id"),
10
- :type => safe_get_attr(xml, "artist", "type"),
11
- :name => safe_get_value(xml, "artist > name").gsub(/[`’]/, "'"),
12
- :country => safe_get_value(xml, "artist > country"),
13
- :date_begin => safe_get_value(xml, "artist > life-span > begin"),
14
- :date_end => safe_get_value(xml, "artist > life-span > end"),
15
- :urls => {}
16
- }
17
- xml.css("relation-list[target-type='url'] > relation").each { |rel|
18
- res[:urls][rel.attr("type").downcase.split(" ").join("_").to_sym] = rel.css("target").text
19
- }
20
- res
21
- end
22
-
23
- def search(xml)
24
- xml.css("artist-list > artist").map do |a|
25
- {
26
- :name => a.first_element_child.text.gsub(/[`’]/, "'"),
27
- :sort_name => safe_get_value(a, "sort-name").gsub(/[`’]/, "'"),
28
- :score => (safe_get_attr(a, nil, "score").to_i rescue 0),
29
- :desc => safe_get_value(a, "disambiguation"),
30
- :type => safe_get_attr(a, nil, "type"),
31
- :mbid => safe_get_attr(a, nil, "id"),
32
- :aliases => a.css("alias-list > alias").map { |item| item.text }
33
- }
34
- end
35
- end
36
-
37
- def release_groups(xml)
38
- release_groups = []
39
- xml.css("release-group").each do |rg|
40
- release_groups << MusicBrainz::Parsers::ReleaseGroup.model(rg)
41
- end
42
- release_groups
43
- end
44
- end
45
- end
46
- end
47
- end
data/lib/parsers/base.rb DELETED
@@ -1,41 +0,0 @@
1
- # -*- encoding: utf-8 -*-
2
-
3
- module MusicBrainz
4
- module Parsers
5
- class << self
6
- def get_by_name(name)
7
- case name
8
- when :artist_model
9
- { :const => MusicBrainz::Parsers::Artist, :method => :model }
10
- when :artist_search
11
- { :const => MusicBrainz::Parsers::Artist, :method => :search }
12
- when :artist_release_groups
13
- { :const => MusicBrainz::Parsers::Artist, :method => :release_groups }
14
- when :release_group_model
15
- { :const => MusicBrainz::Parsers::ReleaseGroup, :method => :model }
16
- when :release_group_releases
17
- { :const => MusicBrainz::Parsers::ReleaseGroup, :method => :releases }
18
- when :release_model
19
- { :const => MusicBrainz::Parsers::Release, :method => :model }
20
- when :release_tracks
21
- { :const => MusicBrainz::Parsers::Release, :method => :tracks }
22
- when :track_model
23
- { :const => MusicBrainz::Parsers::Track, :method => :model }
24
- end
25
- end
26
- end
27
-
28
- class Base
29
- class << self
30
- def safe_get_attr(xml, path, name)
31
- node = path.nil? ? xml : (xml.css(path).first unless xml.css(path).empty?)
32
- node.attr(name) unless node.nil? or node.attr(name).nil?
33
- end
34
-
35
- def safe_get_value(xml, path)
36
- xml.css(path).first.text unless xml.css(path).empty?
37
- end
38
- end
39
- end
40
- end
41
- end
@@ -1,28 +0,0 @@
1
- # -*- encoding: utf-8 -*-
2
-
3
- module MusicBrainz
4
- module Parsers
5
- class Release < Base
6
- class << self
7
- def model(xml)
8
- {
9
- :id => safe_get_attr(xml, nil, "id") || safe_get_attr(xml, "release", "id"),
10
- :title => safe_get_value(xml, "title"),
11
- :status => safe_get_value(xml, "status"),
12
- :country => safe_get_value(xml, "country"),
13
- :format => safe_get_value(xml, "medium-list > medium > format"),
14
- :date => safe_get_value(xml, "date")
15
- }
16
- end
17
-
18
- def tracks(xml)
19
- tracks = []
20
- xml.css("medium-list > medium > track-list > track").each do |r|
21
- tracks << MusicBrainz::Parsers::Track.model(r)
22
- end
23
- tracks
24
- end
25
- end
26
- end
27
- end
28
- end
@@ -1,27 +0,0 @@
1
- # -*- encoding: utf-8 -*-
2
-
3
- module MusicBrainz
4
- module Parsers
5
- class ReleaseGroup < Base
6
- class << self
7
- def model(xml)
8
- {
9
- :id => safe_get_attr(xml, nil, "id") || safe_get_attr(xml, "release-group", "id"),
10
- :type => safe_get_attr(xml, nil, "type") || safe_get_attr(xml, "release-group", "type"),
11
- :title => safe_get_value(xml, "title"),
12
- :disambiguation => safe_get_value(xml, "disambiguation"),
13
- :first_release_date => safe_get_value(xml, "first-release-date")
14
- }
15
- end
16
-
17
- def releases(xml)
18
- releases = []
19
- xml.css("release").each do |r|
20
- releases << MusicBrainz::Parsers::Release.model(r)
21
- end
22
- releases
23
- end
24
- end
25
- end
26
- end
27
- end
data/lib/parsers/track.rb DELETED
@@ -1,18 +0,0 @@
1
- # -*- encoding: utf-8 -*-
2
-
3
- module MusicBrainz
4
- module Parsers
5
- class Track < Base
6
- class << self
7
- def model(xml)
8
- {
9
- :position => safe_get_value(xml, "position"),
10
- :recording_id => safe_get_attr(xml, "recording", "id"),
11
- :title => safe_get_value(xml, "recording > title"),
12
- :length => safe_get_value(xml, "length") || safe_get_value(xml, "recording > length")
13
- }
14
- end
15
- end
16
- end
17
- end
18
- end