class CampactUserService::Client

Constants

OPEN_TIMEOUT
TIMEOUT

Attributes

connection[R]
host[R]
port[R]
topt_authorization[R]

Public Class Methods

new(options) click to toggle source
# File lib/campact_user_service/client.rb, line 13
def initialize(options)
  @host = options.fetch(:host)
  @port = options[:port]
  @topt_authorization = options[:topt_authorization]
  faraday_options = default_faraday_options.merge(options.delete(:faraday) || {})
  adapter = faraday_options.delete(:adapter) || Faraday.default_adapter

  @connection = Faraday.new(endpoint, faraday_options) do |faraday|
    faraday.adapter adapter
  end
end

Private Instance Methods

auth_pass(secret) click to toggle source
# File lib/campact_user_service/client.rb, line 100
def auth_pass(secret)
  totp_secret = ROTP::Base32.encode(secret)

  ROTP::TOTP.new(totp_secret, {
    digest: 'sha256',
    digits: 8,
    interval: 30
  }).now
end
authorization(totp_options) click to toggle source
# File lib/campact_user_service/client.rb, line 91
def authorization(totp_options)
  user = totp_options.fetch(:user)
  secret = totp_options.fetch(:secret)

  token = [user, auth_pass(secret)].join(':')

  "Token #{token}"
end
default_faraday_options() click to toggle source
# File lib/campact_user_service/client.rb, line 62
def default_faraday_options
  {
    ssl: { verify: true },
    headers: {
      'Accept' => "application/json;q=0.1",
      'Accept-Charset' => "utf-8",
      'User-Agent' => 'campact_user_service'
    }
  }
end
endpoint() click to toggle source
# File lib/campact_user_service/client.rb, line 73
def endpoint
  endpoint = "https://#{host}"
  if !port.nil?
    endpoint << ":#{port}"
  end

  endpoint
end
format_cookies(cookies) click to toggle source
# File lib/campact_user_service/client.rb, line 82
def format_cookies(cookies)
  case cookies
    when String
      cookies
    when Hash
      cookies.map {|k,v| "#{k}=#{v};" }.join
  end
end
request(verb, path, options) click to toggle source
# File lib/campact_user_service/client.rb, line 33
def request(verb, path, options)
  response = connection.send(verb.to_sym) do |req|
    req.url path
    req.options.timeout = TIMEOUT
    req.options.open_timeout = OPEN_TIMEOUT
    if options.key?(:cookies)
      req.headers['Cookie'] = format_cookies(options[:cookies])
    end

    if topt_authorization
      req.headers['authorization'] = authorization(topt_authorization)
    end
  end

  case response.status
  when 200
    body = (response.body.nil? || response.body == '') ? '{ }' : response.body
    JSON.parse(body)
  when 201..299
    true
  when 404
    nil
  when 300..599
    raise ResponseError.new(response.status, response.body)
  else
    nil
  end
end