Class: Lightcast::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/lightcast-ruby/connection.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url:, access_token: nil, scope: 'classification_api') ⇒ Connection

Returns a new instance of Connection.



9
10
11
12
13
# File 'lib/lightcast-ruby/connection.rb', line 9

def initialize(url:, access_token: nil, scope: 'classification_api')
  @access_token = access_token
  @url          = url
  @scope = scope
end

Instance Attribute Details

#access_tokenObject

Returns the value of attribute access_token.



7
8
9
# File 'lib/lightcast-ruby/connection.rb', line 7

def access_token
  @access_token
end

#urlObject

Returns the value of attribute url.



7
8
9
# File 'lib/lightcast-ruby/connection.rb', line 7

def url
  @url
end

Instance Method Details

#delete(path, **params) ⇒ Object



15
16
17
# File 'lib/lightcast-ruby/connection.rb', line 15

def delete(path, **params)
  request(:delete, path, params)
end

#get(path, **params) ⇒ Object



19
20
21
# File 'lib/lightcast-ruby/connection.rb', line 19

def get(path, **params)
  request(:get, path, params)
end

#head(path, **params) ⇒ Object



23
24
25
# File 'lib/lightcast-ruby/connection.rb', line 23

def head(path, **params)
  request(:head, path, params)
end

#post(path, params = {}, options = {}) ⇒ Object



27
28
29
# File 'lib/lightcast-ruby/connection.rb', line 27

def post(path, params = {}, options = {})
  request(:post, path, params, options)
end

#put(path, **params) ⇒ Object



31
32
33
# File 'lib/lightcast-ruby/connection.rb', line 31

def put(path, **params)
  request(:put, path, params)
end

#request(method, path, params, options = {}) ⇒ Object

rubocop:disable Metrics/MethodLength



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/lightcast-ruby/connection.rb', line 35

def request(method, path, params, options = {}) # rubocop:disable Metrics/MethodLength
  response = connection.public_send(method, path, params) do |request|
    request.headers['accept'] = 'application/json'
    request.headers['Authorization'] = "Bearer #{@access_token}" if @access_token

    if options[:body] == :form
      request.headers['Content-Type'] = 'application/x-www-form-urlencoded'
      request.body = URI.encode_www_form(params)
    end
  end

  error = Error.from_response(response)

  raise error if error

  response.body
end