Class: Spot
- Inherits:
-
Object
- Object
- Spot
- Defined in:
- lib/spot.rb
Class Method Summary collapse
Instance Method Summary collapse
- #clean!(string) ⇒ Object
- #exclude?(compare) ⇒ Boolean
- #find(type, all, s) ⇒ Object
-
#initialize ⇒ Spot
constructor
A new instance of Spot.
- #method_missing(method, *args, &blk) ⇒ Object
- #page(value) ⇒ Object
- #prefix(value) ⇒ Object
- #prime ⇒ Object
- #result ⇒ Object
- #results ⇒ Object
- #strip ⇒ Object
- #territory(value) ⇒ Object
- #type_of(r) ⇒ Object
Constructor Details
#initialize ⇒ Spot
Returns a new instance of Spot.
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/spot.rb', line 14 def initialize @methods = { :artists => { :selector => :artists, :class => SpotContainer::Artist, :url => generate_url("artist") }, :songs => { :selector => :tracks, :class => SpotContainer::Song, :url => generate_url("track") }, :albums => { :selector => :albums, :class => SpotContainer::Album, :url => generate_url("album") } } @cache = {} @exclude = YAML.load(File.read("#{File.dirname(__FILE__)}/spot/exclude.yml")) @config = { :exclude => 2, :popularity => 7, :limit => 0.7, :offset => 10 } @options = {} end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &blk) ⇒ Object
51 52 53 54 55 56 57 58 59 |
# File 'lib/spot.rb', line 51 def method_missing(method, *args, &blk) if method.to_s =~ /^find(_all)?_([a-z]+)$/i find($2, !!$1, args.first) elsif scrape and content["info"].keys.include?(method.to_s) content["info"][method.to_s] else super(method, *args, &blk) end end |
Class Method Details
Instance Method Details
#clean!(string) ⇒ Object
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
# File 'lib/spot.rb', line 136 def clean!(string) string.strip! # Song - A + B + C => Song - A # Song - A abc/def => Song - A abc # Song - A & abc def => Song - A # Song - A "abc def" => Song - A # Song - A [B + C] => Song - A # Song A B.mp3 => Song A B # Song a.b.c.d.e => Song a b c d e # 10. Song => Song [/\.[a-z0-9]{2,3}$/, /\[[^\]]*\]/,/".*"/, /'.*'/, /[&|\/|\+][^\z]*/, /^(\d+.*?[^a-z]+?)/i].each do |reg| string = string.gsub(reg, '').strip end [/\(.+?\)/m, /feat(.*?)\s*[^\s]+/i, /[-]+/, /[\s]+/m, /\./, /\_/].each do |reg| string = string.gsub(reg, ' ').strip end {"ä" => "a", "å" => "a", "ö" => "o"}.each do |from, to| string.gsub!(/#{from}/i, to) end string.gsub(/\A\s|\s\z/, '').gsub(/\s+/, ' ').strip.downcase rescue Encoding::CompatibilityError return string end |
#exclude?(compare) ⇒ Boolean
164 165 166 |
# File 'lib/spot.rb', line 164 def exclude?(compare) @exclude.map { |value| !! compare.match(/#{value}/i) }.any? end |
#find(type, all, s) ⇒ Object
73 74 75 76 77 78 79 |
# File 'lib/spot.rb', line 73 def find(type, all, s) tap { @search = s @type = all ? type.to_sym : "#{type}s".to_sym raise NoMethodError.new(@type) unless @methods.keys.include?(@type) } end |
#page(value) ⇒ Object
61 62 63 |
# File 'lib/spot.rb', line 61 def page(value) tap { @page = value } end |
#prefix(value) ⇒ Object
69 70 71 |
# File 'lib/spot.rb', line 69 def prefix(value) tap { @prefix = value } end |
#prime ⇒ Object
65 66 67 |
# File 'lib/spot.rb', line 65 def prime tap { @prime = true } end |
#result ⇒ Object
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/spot.rb', line 93 def result @prime ? results.sort_by do |res| res.popularity end.reverse[0..4].map do |r| song, artist = type_of(r) match = "#{song} #{artist}".split(" ") raw = clean!(search).split(" ") if raw.length < match.length diff = match - raw res = diff.length.to_f/match.length else diff = raw - match res = diff.length.to_f/raw.length end if diff.length > 1 and not match.map{ |m| diff.include?(m) }.all? res =+ diff.map do |value| match.map do |m| Levenshtein.distance(value, m) end.inject(:+) end.inject(:+) / @config[:offset] end [res - r.popularity/@config[:popularity], r] end.reject do |distance, value| exclude?(value.to_s) or not value.valid? end.sort_by do |distance, _| distance end.map(&:last).first : results.first end |
#results ⇒ Object
81 82 83 |
# File 'lib/spot.rb', line 81 def results @_results ||= scrape end |
#strip ⇒ Object
85 86 87 |
# File 'lib/spot.rb', line 85 def strip tap { @strip = true } end |
#territory(value) ⇒ Object
89 90 91 |
# File 'lib/spot.rb', line 89 def territory(value) tap { @options.merge!(:territory => value) } end |
#type_of(r) ⇒ Object
126 127 128 129 130 131 132 133 134 |
# File 'lib/spot.rb', line 126 def type_of(r) if @type == :songs return r.name.to_s, r.artist.name.to_s elsif @type == :artists return r.song.title.to_s, r.name.to_s else return "", r.artist.to_s end end |