class Atomsphere::Api::Client

Public Class Methods

new() click to toggle source
# File lib/atomsphere/api/client.rb, line 9
def initialize
  validate!
end

Public Instance Methods

bulk_get(path, ids) click to toggle source
# File lib/atomsphere/api/client.rb, line 35
def bulk_get path, ids
  request = { type: 'GET', request: ids.map{ |id| {id: id} } }
  path = [*path] << :bulk
  post path, request
end
get(path) click to toggle source
# File lib/atomsphere/api/client.rb, line 26
def get path
  header = {'Accept' => 'application/json'}

  request = Net::HTTP::Get.new api_uri(path), header
  request.basic_auth config.username, config.password

  http_request request
end
post(path, data) click to toggle source
# File lib/atomsphere/api/client.rb, line 13
def post path, data
  header = {
    'Content-Type' => 'application/json',
    'Accept'       => ' application/json'
  }

  request = Net::HTTP::Post.new api_uri(path), header
  request.body = (String === data ? data : data.to_json)
  request.basic_auth config.username, config.password

  http_request request
end

Private Instance Methods

api_uri(path=nil) click to toggle source
# File lib/atomsphere/api/client.rb, line 71
def api_uri path=nil
  URI.parse config.base_uri + 
    config.account_id + 
    '/' + (Array === path ? path.join('/') : path.to_s)
end
config() click to toggle source
# File lib/atomsphere/api/client.rb, line 59
def config
  @config ||= Atomsphere.configuration
end
generate_otp() click to toggle source
# File lib/atomsphere/api/client.rb, line 67
def generate_otp
  totp.now
end
http() click to toggle source
# File lib/atomsphere/api/client.rb, line 77
def http
  h = Net::HTTP.new api_uri.host, api_uri.port
  h.use_ssl = true

  h
end
http_request(request) click to toggle source
# File lib/atomsphere/api/client.rb, line 84
def http_request request
  request['X-Boomi-OTP'] = generate_otp if config.otp_secret

  begin
    response = Response.new(request, http.request(request))
  rescue => e
    raise ApiError.new(request, response)
  else
    raise ApiError.new(
      request,
      response,
      e,
      'response code was nil'
    ) if response.code.nil?

    if response.code >= 400
      begin
        json = JSON.parse(response.response.body)
        if json['@type'].downcase.eql?('error')
           message = json['message']
        end
      rescue JSON::ParserError
      ensure
        message ||= "API responded with error #{response.code}: #{response.message}"
      end

      raise ApiError.new(
        request,
        response,
        e,
        message
      )
    end
  end

  response
end
totp() click to toggle source
# File lib/atomsphere/api/client.rb, line 63
def totp
  ROTP::TOTP.new(config.otp_secret) if config.otp_secret
end
validate!() click to toggle source
# File lib/atomsphere/api/client.rb, line 49
def validate!
  raise ArgumentError, "Atomsphere not configured" if config.nil?

  required_vars = Atomsphere::Configuration::REQUIRED
  if (missing_vars = required_vars.select{ |v| config.send(v).nil? }).size > 0
    raise ArgumentError, "Atomsphere configuration incomplete," +
      " required vars missing: #{missing_vars.join('; ')}"
  end
end