Class: OfficialFM::Client

Inherits:
Object
  • Object
show all
Includes:
Playlists, Projects, Tracks
Defined in:
lib/officialfm/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Projects

#project, #project_playlists, #project_tracks, #projects

Methods included from Playlists

#playlist, #playlist_tracks, #playlists

Methods included from Tracks

#track, #tracks

Constructor Details

#initialize(options = {}) ⇒ Client

Returns a new instance of Client.



12
13
14
15
# File 'lib/officialfm/client.rb', line 12

def initialize(options={})
  @api_key = options[:api_key] || OfficialFM.api_key
  connection(options)
end

Instance Attribute Details

#api_keyObject (readonly)

Returns the value of attribute api_key.



10
11
12
# File 'lib/officialfm/client.rb', line 10

def api_key
  @api_key
end

Instance Method Details

#api_urlString

Provides the URL for accessing the API

Returns:

  • (String)


53
54
55
# File 'lib/officialfm/client.rb', line 53

def api_url
  "http://api.official.fm"
end

#connection(options = {}) ⇒ Faraday::Connection

Faraday::Connection used for all HTTP requests

Returns:

  • (Faraday::Connection)


21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/officialfm/client.rb', line 21

def connection(options={})
  config = {
    :url     => api_url,
    :headers => default_headers
  }.merge(options)

  @connection ||= Faraday.new(config) do |builder|
    builder.adapter Faraday.default_adapter

    builder.request :url_encoded

    builder.response :mashify
    builder.response :json
  end

end

#default_headersObject



39
40
41
42
43
44
45
46
47
48
# File 'lib/officialfm/client.rb', line 39

def default_headers
  headers = {
    'X-API-Version' => '2.0',
    :accept         => 'application/json',
    :user_agent     => "officialfm v2 ruby gem version #{OfficialFM::VERSION}"
  }
  headers['X-API-KEY'] = @api_key if @api_key

  headers
end