Class: Earworm::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/earworm/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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_idObject

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(options = {})
  post_opts = nil
  if options.key?(:file)
    post_opts = {
      'cid'  => @client_id,
      'cvr'  => "Earworm #{VERSION}",
      'rmd'  => 1,
      'enc'  => '',
    }.merge(Fingerprint.new(options[:file]).to_hash)
  end
  if options.key?(:puid)
    post_opts = {
      'cid'  => @client_id,
      'cvr'  => "Earworm #{VERSION}",
      'rmd'  => 1,
      'enc'  => '',
    }.merge(PUID.new(options[: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