class Akamai::Core::Client::Client

Attributes

access_token[R]
client_secret[R]
client_token[R]
host[R]
ssl[R]

Public Class Methods

new(host:, client_token:, access_token:, client_secret:, ssl: true) click to toggle source
# File lib/akamai/core/client/client.rb, line 8
def initialize(host:, client_token:, access_token:, client_secret:, ssl: true)
  @host = host
  @client_token = client_token
  @access_token = access_token
  @client_secret = client_secret
  @ssl = ssl
end

Public Instance Methods

delete(path, headers = nil) click to toggle source
# File lib/akamai/core/client/client.rb, line 32
def delete(path, headers = nil)
  request("delete", path, nil, headers)
end
get(path, headers = nil) click to toggle source
# File lib/akamai/core/client/client.rb, line 16
def get(path, headers = nil)
  request("get", path, nil, headers)
end
head(path, headers = nil) click to toggle source
# File lib/akamai/core/client/client.rb, line 36
def head(path, headers = nil)
  request("head", path, nil, headers)
end
patch(path, body = nil, headers = nil) click to toggle source
# File lib/akamai/core/client/client.rb, line 28
def patch(path, body = nil, headers = nil)
  request("patch", path, body, headers)
end
post(path, body = nil, headers = nil) click to toggle source
# File lib/akamai/core/client/client.rb, line 20
def post(path, body = nil, headers = nil)
  request("post", path, body, headers)
end
put(path, body = nil, headers = nil) click to toggle source
# File lib/akamai/core/client/client.rb, line 24
def put(path, body = nil, headers = nil)
  request("put", path, body, headers)
end

Private Instance Methods

authorization(method, path, body = nil, headers = nil) click to toggle source
# File lib/akamai/core/client/client.rb, line 77
def authorization(method, path, body = nil, headers = nil)
  Authority.new(
    client: self, method: method.upcase, protocol: protocol,
    host: host, path: path, body: body, headers: headers
  ).publish_authorization
end
http_client() click to toggle source
# File lib/akamai/core/client/client.rb, line 67
def http_client
  @http_client ||= Net::HTTP.new(uri.host, uri.port).tap do |http|
    http.use_ssl = !!ssl
  end
end
protocol() click to toggle source
# File lib/akamai/core/client/client.rb, line 73
def protocol
  ssl ? "https" : "http"
end
request(method, path, body = nil, optional_headers = nil) click to toggle source
# File lib/akamai/core/client/client.rb, line 42
def request(method, path, body = nil, optional_headers = nil)
  response = Response.new(
    "".tap do |raw_response|
      http_client.start do |session|
        headers = {
          "Authorization" => authorization(method, path, body,
                                           optional_headers),
          "Content-Type" => "application/json"
        }.merge(optional_headers ? optional_headers : {})
        raw_response = if /^get$|^delete$|^head$/ =~ method
                         session.send(method, path, headers)
                       else
                         session.send(method, path, body, headers)
                       end
      end
      break raw_response
    end
  )

  Error.new(response.body).tap do |error|
    error.raise_error if error.exist?
  end
  response
end
uri() click to toggle source
# File lib/akamai/core/client/client.rb, line 84
def uri
  @uri ||= URI("#{protocol}://#{host}")
end