Class: OfficialFM::Client

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Authentication, Playlists, Tracks, Users
Defined in:
lib/officialfm/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Playlists

#improve, #playlist, #playlist_create!, #playlist_picture!, #playlist_remove!, #playlist_unvote!, #playlist_vote!, #playlist_votes, #playlists, #update_playlist!

Methods included from Tracks

#add_track_to_playlist!, #charts, #latest, #remove_track_from_playlist!, #track, #track_picture!, #track_unvote!, #track_vote!, #track_votes, #tracks, #update_track!, #upload!

Methods included from Users

#dropbox, #newsfeed, #own_playlists, #own_tracks, #picture!, #profile, #subscribe!, #unsubscribe!, #update_profile!, #user, #user_contacts, #user_playlists, #user_subscribers, #user_subscriptions, #user_tracks, #users, #voted_playlists, #voted_tracks

Constructor Details

#initialize(options = {}) ⇒ Client

Returns a new instance of Client.



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/officialfm/client.rb', line 19

def initialize(options={})
  @api_key = options[:api_key] || OfficialFM.api_key
  @api_secret = options[:api_secret] || OfficialFM.api_secret
  @access_token = options[:access_token]
  @access_secret = options[:access_secret]
  # Note: Although the default of the API is to return XML, I think
  # json is more appropriate in the Ruby world
  
  @format = options[:format] || :json
  connection unless @access_token
end

Instance Attribute Details

#api_keyObject (readonly)

Returns the value of attribute api_key.



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

def api_key
  @api_key
end

#api_secretObject (readonly)

Returns the value of attribute api_secret.



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

def api_secret
  @api_secret
end

Instance Method Details

#api_urlString

Provides the URL for accessing the API

Returns:

  • (String)


86
87
88
# File 'lib/officialfm/client.rb', line 86

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

#connectionFaraday::Connection

Raw HTTP connection, either Faraday::Connection

Returns:

  • (Faraday::Connection)


55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/officialfm/client.rb', line 55

def connection
  params = 

  options = {
    :url => api_url,
    :headers => default_headers
  }

  @connection ||= Faraday.new(options) do |builder|
    builder.use Faraday::Request::OfficialFMOAuth, authentication if authenticated?
    builder.use Faraday::Request::Multipart
    builder.use Faraday::Request::UrlEncoded
    
    builder.use Faraday::Response::Mashify
    builder.use Faraday::Response::ParseJson
    builder.adapter Faraday.default_adapter
  end

end

#default_headersObject



76
77
78
79
80
81
# File 'lib/officialfm/client.rb', line 76

def default_headers
  headers = {
    :accept =>  'application/json',
    :user_agent => "officialfm ruby gem version #{OfficialFM::VERSION}"
  }
end

#simple_params(options = nil) ⇒ Object

GET request arguments for simple API calls



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/officialfm/client.rb', line 32

def simple_params (options = nil)
  params = {
    :key    => @api_key,
    :format => @format,
  }
  if options
      options.each { |key, value|
        case key
        when 'limit'
          params['api_max_responses'] = value
        when 'embed'
          params['api_embed_codes'] = value
        else
          params[key] = value
        end
      }
  end
  params
end