Class: Echonest::Api

Inherits:
Object
  • Object
show all
Includes:
TraditionalApiMethods
Defined in:
lib/echonest/api.rb

Defined Under Namespace

Classes: Error

Constant Summary collapse

VERSION =
'4.2'
BASE_URL =
'http://developer.echonest.com/api/v4/'
USER_AGENT =
'%s/%s' % ['ruby-echonest', ::Echonest::VERSION]

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from TraditionalApiMethods

included

Constructor Details

#initialize(api_key) ⇒ Api

Returns a new instance of Api.



19
20
21
22
23
24
25
# File 'lib/echonest/api.rb', line 19

def initialize(api_key)
  @api_key = api_key
  @user_agent = HTTPClient.new(:agent_name => USER_AGENT)
  # for big files
  @user_agent.send_timeout = 60 * 30
  @user_agent.receive_timeout = 60 * 10
end

Instance Attribute Details

#user_agentObject (readonly)

Returns the value of attribute user_agent.



17
18
19
# File 'lib/echonest/api.rb', line 17

def user_agent
  @user_agent
end

Instance Method Details

#artist(name = nil) ⇒ Object



31
32
33
34
35
36
37
# File 'lib/echonest/api.rb', line 31

def artist(name=nil)
  if name
    ApiMethods::Artist.new_from_name(self, name)
  else
    ApiMethods::Artist.new(self)
  end
end

#build_params(params) ⇒ Object



58
59
60
61
# File 'lib/echonest/api.rb', line 58

def build_params(params)
  params = params.
    merge(default_params)
end

#build_params_to_list(params) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/echonest/api.rb', line 63

def build_params_to_list(params)
  result = []
  hash_to_list = lambda{|kv| [kv[0].to_s, kv[1]]}
  params.each do |param|
    if param.instance_of? Array
      Array(param[1]).map do |p1|
        result << [param[0].to_s, p1]
      end
    else
      result << hash_to_list.call(params)
    end
  end
  default_params.each do |kv|
    result << hash_to_list.call(kv) unless params.include? kv[0]
  end
  result
end

#catalogObject



39
40
41
# File 'lib/echonest/api.rb', line 39

def catalog
  ApiMethods::Catalog.new(self)
end

#default_paramsObject



51
52
53
54
55
56
# File 'lib/echonest/api.rb', line 51

def default_params
  {
    :format => 'json',
    :api_key => @api_key
  }
end

#playlistObject



47
48
49
# File 'lib/echonest/api.rb', line 47

def playlist
  ApiMethods::Playlist.new(self)
end

#request(name, method, params, file = nil) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
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
125
126
127
128
# File 'lib/echonest/api.rb', line 81

def request(name, method, params, file = nil)
  uri = URI.join(BASE_URL, name.to_s)
  if file
    query = build_params(params).sort_by do |param|
      param[0].to_s
    end.inject([]) do |m, param|
      m << [URI.encode(param[0].to_s), URI.encode(param[1])].join('=')
    end.join('&')

    uri.query = query
    file = file.read unless file.is_a?(String)
    connection = @user_agent.__send__(
      method.to_s + '_async',
      uri,
      file,
      {
        'Content-Type' => 'application/octet-stream'
      })

      # Show some feedback for big ole' POSTs
      n=0
      print "8"
      begin
        sleep 2
        n+=2
        print (n%6==0 ? "D 8" : "=")
      end while !connection.finished?

      res = connection.pop
      response_body = res.content.read
  else
    Echonest.debug "#{method.to_s.upcase} #{uri}?#{build_params_to_list(params).inject(''){ |str, (key,val)| str << "#{key}=#{val}&"; str }}"

    response_body = @user_agent.__send__(
      method.to_s + '_content',
      uri,
      build_params_to_list(params))
  end

  response = Response.new(response_body)
  unless response.success?
    raise Error.new(response.status.message)
  end

  response
rescue HTTPClient::BadResponseError => e
  raise Error.new('%s: %s' % [name, e.message])
end

#songObject



43
44
45
# File 'lib/echonest/api.rb', line 43

def song
  ApiMethods::Song.new(self)
end

#trackObject



27
28
29
# File 'lib/echonest/api.rb', line 27

def track
  ApiMethods::Track.new(self)
end