Class: Adcloud::Connection

Inherits:
Entity
  • Object
show all
Defined in:
lib/adcloud/connection.rb

Constant Summary

Constants inherited from Entity

Entity::MAX_PER_PAGE

Instance Attribute Summary collapse

Attributes inherited from Entity

#errors

Instance Method Summary collapse

Methods inherited from Entity

all, all!, api_name, #create, create, #destroy, find, find_by_name, #meta, #update

Instance Attribute Details

#authenticationObject

Returns the value of attribute authentication.



5
6
7
# File 'lib/adcloud/connection.rb', line 5

def authentication
  @authentication
end

Instance Method Details

#authentication_tokenObject



19
20
21
# File 'lib/adcloud/connection.rb', line 19

def authentication_token
  self.authentication.token
end

#connection(auth = true) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/adcloud/connection.rb', line 23

def connection(auth = true)
  auth_header = auth && { :Authorization =>  "Bearer #{authentication_token}" } || {}
  connection ||= Faraday.new(:url => url, :headers => {}.merge(auth_header)) do |faraday|
    faraday.request  :url_encoded                     # form-encode POST params
    # log requests to STDOUT
    faraday.response :logger if Adcloud.config.debug
    faraday.use ResponseErrorHandler
    faraday.adapter  Faraday.default_adapter          # make requests with Net::HTTP
    faraday.response :json, :content_type => /\bjson$/
  end
end

#delete(path, params = {}) ⇒ Object



50
51
52
53
# File 'lib/adcloud/connection.rb', line 50

def delete(path, params = {})
  response = connection.delete path, params
  response.body
end

#get(path, params = {}) ⇒ Object



40
41
42
43
# File 'lib/adcloud/connection.rb', line 40

def get(path, params = {})
  response = connection.get path, params
  response.body
end

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



35
36
37
38
# File 'lib/adcloud/connection.rb', line 35

def post(path, params = {})
  response = connection.post path, params
  response.body
end

#put(path, params = {}) ⇒ Object



45
46
47
48
# File 'lib/adcloud/connection.rb', line 45

def put(path, params = {})
  response = connection.put path, params
  response.body
end

#urlObject



15
16
17
# File 'lib/adcloud/connection.rb', line 15

def url
  "#{Adcloud.config.protocol}://#{Adcloud.config.host}:#{Adcloud.config.port}/#{Adcloud.config.api_version}/"
end