Class: Notu::Api

Inherits:
Object
  • Object
show all
Defined in:
lib/notu/api.rb

Direct Known Subclasses

UserApi

Constant Summary collapse

DEFAULT_API_KEY =
'91f5d6a201de58e0c0a0d858573dddf0'.freeze
FORMAT =
'json'.freeze
HOST =
'ws.audioscrobbler.com'.freeze
VERSION =
'2.0'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key: DEFAULT_API_KEY) ⇒ Api

Returns a new instance of Api.



12
13
14
# File 'lib/notu/api.rb', line 12

def initialize(api_key: DEFAULT_API_KEY)
  @api_key = api_key.try(:squish).presence || raise(Error.new('API key must be specified'))
end

Instance Attribute Details

#api_keyObject (readonly)

Returns the value of attribute api_key.



10
11
12
# File 'lib/notu/api.rb', line 10

def api_key
  @api_key
end

Instance Method Details

#url(params = {}) ⇒ Object



16
17
18
19
20
21
# File 'lib/notu/api.rb', line 16

def url(params = {})
  params = (params || {}).symbolize_keys
  params.merge!(api_key:, format: FORMAT)
  query_string = params.map { |name, value| "#{CGI.escape(name.to_s)}=#{CGI.escape(value.to_s)}" }.join('&')
  "https://#{HOST}/#{VERSION}?#{query_string}"
end