Class: Nestling::Client

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

Constant Summary collapse

HOST =
'developer.echonest.com'
USER_AGENT =
"Nestling/#{Version::STRING}"
DEFAULT_PARAMS =
{ :format => 'json' }

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key = nil) ⇒ Client



9
10
11
12
13
# File 'lib/nestling/client.rb', line 9

def initialize(api_key = nil)
  @http = Net::HTTP.new(HOST)
  @api_key = api_key || Nestling.api_key
  @default_params = { :api_key => @api_key }.merge!(DEFAULT_PARAMS)
end

Instance Attribute Details

#api_keyObject (readonly)

Returns the value of attribute api_key.



3
4
5
# File 'lib/nestling/client.rb', line 3

def api_key
  @api_key
end

Instance Method Details

#artist(id) ⇒ Object Also known as: artists



15
16
17
# File 'lib/nestling/client.rb', line 15

def artist(id)
  Nestling::Artist.new(id, self)
end

#get(meth, params = {}) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
# File 'lib/nestling/client.rb', line 35

def get(meth, params = {})
  path = "/api/v4/#{meth}?" << convert_params(params)
  response = @http.get(path, {'User-Agent' => USER_AGENT})
  hash = MultiJson.load(response.body)

  if (code = hash["response"]["status"]["code"].to_i) != 0
    raise ERRNO[code], hash["response"]["status"]["message"]
  end

  hash
end

#playlistObject



31
32
33
# File 'lib/nestling/client.rb', line 31

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

#songObject Also known as: songs



21
22
23
# File 'lib/nestling/client.rb', line 21

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

#trackObject



27
28
29
# File 'lib/nestling/client.rb', line 27

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