Class: SoundCouch::Client

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

Instance Method Summary collapse

Constructor Details

#initialize(uri, access_token) ⇒ Client

Returns a new instance of Client


5
6
7
8
# File 'lib/soundcouch/client.rb', line 5

def initialize(uri, access_token)
  @uri, @access_token = URI(uri), access_token
  @http = HTTPkit::Client.start(config)
end

Instance Method Details

#configObject


10
11
12
13
14
15
16
# File 'lib/soundcouch/client.rb', line 10

def config
  { address: @uri.host, port: @uri.port, ssl: @uri.scheme == 'https',
    handlers: [HTTPkit::Client::SSLHandler.new,
               HTTPkit::Client::KeepAliveHandler.new,
               HTTPkit::Client::TimeoutsHandler.new],
    timeout: 120, connect_timeout: 120 }
end

#headersObject


34
35
36
# File 'lib/soundcouch/client.rb', line 34

def headers
  { 'Authorization' => 'OAuth ' + @access_token }
end

#request(http_method, uri, body = nil) ⇒ Object


18
19
20
21
22
# File 'lib/soundcouch/client.rb', line 18

def request(http_method, uri, body = nil)
  request = Couch::Request.new(http_method, uri, headers, body)
  response = @http.perform(request).sync
  Couch::Response.build(response)
end

#upload(track, filename, asset_data) ⇒ Object


24
25
26
27
28
29
30
31
32
# File 'lib/soundcouch/client.rb', line 24

def upload(track, filename, asset_data)
  mimetype = 'application/octet-stream'
  io       = UploadIO.new(StringIO.new(asset_data), mimetype, filename)

  request = FormRequest.new(:post, '/tracks', headers,
                            'track' => track.merge('asset_data' => io))
  response = @http.perform(request).sync
  Couch::Response.build(response)
end