module Cryptoprocessing::Connection

Public Instance Methods

agent() click to toggle source

@return [Net::HTTP]

# File lib/cryptoprocessing/connection.rb, line 14
def agent
  base_uri = URI.parse(endpoint)
  @agent = Net::HTTP.new(base_uri.host, base_uri.port)
  @agent.use_ssl = true if base_uri.scheme == 'https'
  # @agent.cert_store = self.class.whitelisted_certificates
  @agent.ssl_version = :TLSv1
  @agent
end
auth_headers(method, path, body) click to toggle source
# File lib/cryptoprocessing/connection.rb, line 67
def auth_headers(method, path, body)
  {:Authorization => "Bearer #{@access_token}"}
end
delete(path, params) { |resp| ... } click to toggle source

HTTP DELETE method

@param [String] path

# File lib/cryptoprocessing/connection.rb, line 132
def delete(path, params)
  headers = {}

  request('DELETE', path, nil, headers) do |resp|
    yield(resp) if block_given?
  end
end
endpoint() click to toggle source
# File lib/cryptoprocessing/connection.rb, line 9
def endpoint
  api_endpoint
end
get(path, params = {}) { |resp| ... } click to toggle source

HTTP GET method

@param [String] path

# File lib/cryptoprocessing/connection.rb, line 79
def get(path, params = {})
  uri = path
  if params.count > 0
    uri += "?#{URI.encode_www_form(params)}"
  end

  headers = {}

  request('GET', uri, nil, headers) do |resp|
    if params[:fetch_all] == true &&
        resp.body.has_key?('pagination') &&
        resp.body['pagination']['next_uri'] != nil
      params[:starting_after] = resp.body['data'].last['id']
      get(path, params) do |page|
        body = resp.body
        body['data'] += page.data
        resp.body = body
        yield(resp) if block_given?
      end
    else
      yield(resp) if block_given?
    end
  end
end
post(path, params) { |resp| ... } click to toggle source

HTTP POST method

@param [String] path

# File lib/cryptoprocessing/connection.rb, line 120
def post(path, params)
  headers = {}

  request('POST', path, params.to_json, headers) do |resp|
    yield(resp) if block_given?
  end
end
put(path, params) { |resp| ... } click to toggle source

HTTP PUT method

@param [String] path

# File lib/cryptoprocessing/connection.rb, line 108
def put(path, params)
  headers = {}

  request('PUT', path, params.to_json, headers) do |resp|
    yield(resp) if block_given?
  end
end
request(method, path, body = nil, headers = {}) { |out| ... } click to toggle source

@param [String] method @param [String] path @param [String] body @param [String] headers

# File lib/cryptoprocessing/connection.rb, line 28
def request(method, path, body = nil, headers = {})
  # Prepend configured namespace
  path = "#{@api_namespace}#{path}"

  case method
  when 'GET' then
    req = Net::HTTP::Get.new(path)
  when 'PUT' then
    req = Net::HTTP::Put.new(path)
  when 'POST' then
    req = Net::HTTP::Post.new(path)
  when 'DELETE' then
    req = Net::HTTP::Delete.new(path)
  else
    raise
  end

  req.body = body

  # All requests with JSON encoded body
  req['Content-Type'] = 'application/json'

  # Set User Agent to Gem name and version
  req['User-Agent'] = user_agent

  auth_headers(method, path, body).each do |key, val|
    req[key] = val
  end
  headers.each do |key, val|
    req[key] = val
  end

  resp = agent.request(req)
  out = Cryptoprocessing::NetHTTPResponse.new(resp)
  Cryptoprocessing::check_response_status(out)
  yield(out) if block_given?
  out.data
end
reset_agent() click to toggle source
# File lib/cryptoprocessing/connection.rb, line 71
def reset_agent
  @agent = nil
end