class Plaider::Client

Constants

DATE_FORMAT
DEV_BASE_URL
OPEN_TIMEOUT
PROD_BASE_URL
READ_TIMEOUT

Public Class Methods

new(options={}) click to toggle source
# File lib/plaider/client.rb, line 15
def initialize(options={})
  options[:open_timeout] ||= OPEN_TIMEOUT
  options[:read_timeout] ||= READ_TIMEOUT
  options[:verbose] ||= false
  Plaider::Configurable::KEYS.each do |key|
    instance_variable_set(:"@#{key}", !options[key].nil? ? options[key] : Plaider.instance_variable_get(:"@#{key}"))
  end
end

Public Instance Methods

access_token() click to toggle source
# File lib/plaider/client.rb, line 84
def access_token
  @access_token
end
add_user(institution_type, username, password, email) click to toggle source
# File lib/plaider/client.rb, line 51
def add_user(institution_type, username, password, email)
  validate(institution_type: institution_type, username: username, password: password, email: email)
  response = post('/connect', {type: institution_type, username: username, password: password, email: email, login_only: true})
  status_code = response[:status_code].to_i
  @access_token = response[:result][:access_token] if [200, 201].include?(status_code)
  response
end
balance() click to toggle source
# File lib/plaider/client.rb, line 47
def balance
  post('/balance')
end
categories() click to toggle source
# File lib/plaider/client.rb, line 33
def categories
  get('/categories')
end
category(category_id) click to toggle source
# File lib/plaider/client.rb, line 37
def category(category_id)
  validate(category_id: category_id)
  get("/categories/#{category_id}")
end
delete_user() click to toggle source
# File lib/plaider/client.rb, line 78
def delete_user
  response = delete('/connect')
  @access_token = nil
  response
end
entity(entity_id) click to toggle source
# File lib/plaider/client.rb, line 42
def entity(entity_id)
  validate(entity_id: entity_id)
  get("/entities/#{entity_id}")
end
institution(institution_id) click to toggle source
# File lib/plaider/client.rb, line 28
def institution(institution_id)
  validate(institution_id: institution_id)
  get("/institutions/#{institution_id}")
end
institutions() click to toggle source
# File lib/plaider/client.rb, line 24
def institutions
  get('/institutions')
end
transactions(account_id = nil, start_date = nil, end_date = nil, pending = false) click to toggle source
# File lib/plaider/client.rb, line 64
def transactions(account_id = nil, start_date = nil, end_date = nil, pending = false)
  params = {}
  params[:account_id] = account_id if account_id
  params[:gte] = format_date(start_date)
  params[:lte] = format_date(end_date)
  params[:pending] = pending
  post('/connect/get', params)
end
update_user(username, password) click to toggle source
# File lib/plaider/client.rb, line 73
def update_user(username, password)
  validate(username: username, password: password)
  patch('/connect', {username: username, password: password})
end
user_confirmation(mfa) click to toggle source
# File lib/plaider/client.rb, line 59
def user_confirmation(mfa)
  validate(mfa: mfa)
  post('/connect/step', {mfa: mfa})
end

Protected Instance Methods

delete(path) click to toggle source
# File lib/plaider/client.rb, line 110
def delete(path)
  request = Net::HTTP::Delete.new(path)
  request.set_form_data(credentials.merge(access_token: @access_token)) if !!@access_token
  process(request)
end
get(path) click to toggle source
# File lib/plaider/client.rb, line 90
def get(path)
  process(Net::HTTP::Get.new(path))
end
patch(path, params = {}) click to toggle source
# File lib/plaider/client.rb, line 102
def patch(path, params = {})
  request = Net::HTTP::Patch.new(path)
  params.merge!(credentials)
  params.merge!(access_token: @access_token) if !!@access_token
  request.set_form_data(params)
  process(request)
end
post(path, params = {}) click to toggle source
# File lib/plaider/client.rb, line 94
def post(path, params = {})
  request = Net::HTTP::Post.new(path)
  params.merge!(credentials)
  params.merge!(access_token: @access_token) if !!@access_token
  request.set_form_data(params)
  process(request)
end

Private Instance Methods

credentials() click to toggle source
# File lib/plaider/client.rb, line 126
def credentials
  @credentials ||= {client_id: @client_id, secret: @secret}
end
format_date(date) click to toggle source
# File lib/plaider/client.rb, line 146
def format_date(date)
  !!date ? date.strftime(DATE_FORMAT) : date
end
http() click to toggle source
# File lib/plaider/client.rb, line 130
def http
  unless defined?(@http)
    uri = URI.parse(@environment == 'production' ? PROD_BASE_URL : DEV_BASE_URL)
    @http = Net::HTTP.new(uri.host, uri.port)
    @http.use_ssl = true
    @http.verify_mode = OpenSSL::SSL::VERIFY_NONE
    @http.set_debug_output($stdout) if @verbose
  end
  @http
end
process(request) click to toggle source
# File lib/plaider/client.rb, line 141
def process(request)
  response = http.request(request)
  {status_code: response.code, result: JSON.parse(response.body, {symbolize_names: true})}
end
validate(args) click to toggle source
# File lib/plaider/client.rb, line 118
def validate(args)
  args.each do |name, value|
    if value.nil? || value.to_s.empty?
      raise ArgumentError.new("#{name} is required")
    end
  end
end