class Nvlope

Constants

VERSION

Attributes

logger[W]
api_version[RW]
client_id[RW]
client_secret[RW]
domain[RW]
grant_type[RW]
logger[W]
password[RW]
username[RW]

Public Class Methods

Arguments(hash) click to toggle source
# File lib/nvlope.rb, line 21
def Arguments hash
  Nvlope::Arguments.new hash
end
logger() click to toggle source
# File lib/nvlope.rb, line 25
def logger
  @logger ||= Logger.new('/dev/null')
end
new(arguments) click to toggle source
# File lib/nvlope.rb, line 31
def initialize arguments
  arguments = Nvlope::Arguments(arguments)

  @username       = arguments.require(:username     ).to_s
  @password       = arguments.require(:password     ).to_s
  @client_id      = arguments.require(:client_id    ).to_s
  @client_secret  = arguments.require(:client_secret).to_s
  @grant_type     = arguments.optional(:grant_type ){ 'password'                }.to_s
  @domain         = arguments.optional(:domain     ){ 'https://api.nvlope.com/' }.to_s
  @api_version    = arguments.optional(:api_version){ 'v1'                      }.to_s
  @logger         = arguments.optional(:logger)
end

Public Instance Methods

access_token() click to toggle source
# File lib/nvlope.rb, line 55
def access_token
  @access_token ||= AccessToken.new(self, get_access_token)
end
access_token?() click to toggle source
# File lib/nvlope.rb, line 51
def access_token?
  !@access_token.nil?
end
authenticated_request(method, path, options={}) click to toggle source
# File lib/nvlope.rb, line 71
def authenticated_request method, path, options={}
  options[:headers] ||= {}
  options[:headers]['Authorization'] ||= "Bearer #{access_token}"
  Request.new(self, method, path, options).perform!
end
get_access_token() click to toggle source
# File lib/nvlope.rb, line 77
def get_access_token
  @access_token = begin
    response = unauthenticated_request(:post, 'oauth2/token',
      query: {
        grant_type:    grant_type,
        username:      username,
        password:      password,
        client_id:     client_id,
        client_secret: client_secret,
      }
    )
    response['access_token']
  end
end
get_session() click to toggle source
# File lib/nvlope.rb, line 100
def get_session
  request(:get, '/oauth2/session')
end
inspect() click to toggle source
# File lib/nvlope.rb, line 117
def inspect
  attributes = to_hash.map{|k,v| "#{k}: #{v.inspect}" }.join(', ')
  %(#<#{self.class} #{attributes}>)
end
logger() click to toggle source
# File lib/nvlope.rb, line 46
def logger
  @logger || self.class.logger
end
messages() click to toggle source
# File lib/nvlope.rb, line 63
def messages
  @messages ||= Messages.new(self)
end
revoke_access_token() click to toggle source
# File lib/nvlope.rb, line 92
def revoke_access_token
  # DELETE v1/oauth2/token
  response = request(:delete, 'oauth2/token')
  return false unless response.code < 300
  @access_token = nil
  response
end
session() click to toggle source
# File lib/nvlope.rb, line 59
def session
  @session ||= Session.new(self, get_session)
end
to_hash() click to toggle source
# File lib/nvlope.rb, line 105
def to_hash
  {
    username:      username,
    password:      password,
    client_id:     client_id,
    client_secret: client_secret,
    grant_type:    grant_type,
    domain:        domain,
    api_version:   api_version,
  }
end
unauthenticated_request(method, path, options={}) click to toggle source
# File lib/nvlope.rb, line 67
def unauthenticated_request method, path, options={}
  Request.new(self, method, path, options).perform!
end