Class: Earworm::Client
- Inherits:
-
Object
- Object
- Earworm::Client
- Defined in:
- lib/earworm/client.rb
Instance Attribute Summary collapse
-
#client_id ⇒ Object
Returns the value of attribute client_id.
Instance Method Summary collapse
- #fingerprint(filename) ⇒ Object
- #identify(options = {}) ⇒ Object
-
#initialize(client_id) ⇒ Client
constructor
A new instance of Client.
Constructor Details
#initialize(client_id) ⇒ Client
Returns a new instance of Client.
4 5 6 |
# File 'lib/earworm/client.rb', line 4 def initialize(client_id) @client_id = client_id end |
Instance Attribute Details
#client_id ⇒ Object
Returns the value of attribute client_id.
3 4 5 |
# File 'lib/earworm/client.rb', line 3 def client_id @client_id end |
Instance Method Details
#fingerprint(filename) ⇒ Object
45 46 47 |
# File 'lib/earworm/client.rb', line 45 def fingerprint(filename) Fingerprint.new(filename).to_s end |
#identify(options = {}) ⇒ Object
8 9 10 11 12 13 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 |
# File 'lib/earworm/client.rb', line 8 def identify( = {}) post_opts = nil if .key?(:file) post_opts = { 'cid' => @client_id, 'cvr' => "Earworm #{VERSION}", 'rmd' => 1, 'enc' => '', }.merge(Fingerprint.new([:file]).to_hash) end if .key?(:puid) post_opts = { 'cid' => @client_id, 'cvr' => "Earworm #{VERSION}", 'rmd' => 1, 'enc' => '', }.merge(PUID.new([:puid]).to_hash) end xml = Net::HTTP.post_form(URI.parse(URL), post_opts).body parser = REXML::Parsers::PullParser.new(xml) track = Track.new while parser.has_next? thing = parser.pull if thing.start_element? case thing[0] when 'title' track.title = parser.pull[0] when 'name' track.artist_name = parser.pull[0] when 'puid' track.puid_list << thing[1]['id'] end end end track end |