module BimsApiClient

Constants

VERSION

Attributes

configuration[RW]

Public Class Methods

configure() { |configuration| ... } click to toggle source
# File lib/bims_api_client.rb, line 21
def self.configure
  yield(configuration)
end
reset() click to toggle source
# File lib/bims_api_client.rb, line 17
def self.reset
  @configuration = Configuration.new
end

Public Instance Methods

init() click to toggle source
# File lib/bims_api_client.rb, line 27
def init
  success = false
  begin
    success = setup_redis(@configuration.redis_url) && set_api_base_uri(@configuration.instance_url)
  rescue => e #debug the error
    puts e.to_s
  end
  return success
end
login(username = (@configuration.username), password = (@configuration.password)) click to toggle source
# File lib/bims_api_client.rb, line 64
def login(username = (@configuration.username), password = (@configuration.password))
  success = false
  begin
    return false if not_initialized?
    response_object = _login(username, password)
    if response_object[@configuration.status_key] == @configuration.status_value_ok
      @redis.set('bims_sid', response_object[@configuration.data_key][@configuration.data_session_key][@configuration.data_session_id_key].to_s)
      success = true
    end
  rescue => e
    puts e.to_s #debug the error
  end
  return success
end
logout() click to toggle source
# File lib/bims_api_client.rb, line 79
def logout
  begin
    return false if not_initialized?
    sid = @redis.get('bims_sid')
    @redis.set('bims_sid', nil)
    response_object = _logout(sid)
    response_object[@configuration.status_key] == @configuration.status_value_ok ? true : false
  rescue => e
    puts e.to_s #debug the error
  end
end
not_initialized?() click to toggle source
# File lib/bims_api_client.rb, line 37
def not_initialized?
  !@configuration.instance_url && !@configuration.instance_name
end
session_active?() click to toggle source
# File lib/bims_api_client.rb, line 60
def session_active?
  (@redis.get('bims_sid') && !@redis.get('bims_sid').empty?) ? true : false
end
set_api_base_uri(uri) click to toggle source
# File lib/bims_api_client.rb, line 45
def set_api_base_uri(uri)
  success = false
  begin
    base_uri uri
    success = true
  rescue => e
    puts e.to_s #debug the error
  end
  return success
end
set_error_object(data = nil) click to toggle source
# File lib/bims_api_client.rb, line 56
def set_error_object(data = nil)
  {status: 'error', code: -1, data: data}
end
setup_redis(redis_url) click to toggle source
# File lib/bims_api_client.rb, line 41
def setup_redis(redis_url)
  @redis = Redis.new(url: redis_url)
end

Private Instance Methods

_login(username, password) click to toggle source

Call to the BIMS API “login” method and gets the whole response body

# File lib/bims_api_client.rb, line 93
def _login(username, password)
  begin
    return set_error_object({message: @configuration.not_initialized_message}) if not_initialized?
    url = "#{base_uri}/#{@configuration.users_path}/login.#{@configuration.api_request_format}"
    response = post url, body: {user: username, password: Digest::MD5.hexdigest(password)}
    JSON.parse (response.body && !response.body.empty?) ? response.body : set_error_object #returns the complete object
  rescue => e
    puts e.to_s #debug the error
  end
end
_logout(sid) click to toggle source

Call to the BIMS API “logout” method and gets the whole response body

# File lib/bims_api_client.rb, line 105
def _logout(sid)
  begin
    return set_error_object({message: @configuration.not_initialized_message}) if not_initialized?
    url = "#{base_uri}/#{@configuration.users_path}/logout.#{@configuration.api_request_format}"
    response = post url, body: {sid: sid}
    JSON.parse (response.body && !response.body.empty?) ? response.body : set_error_object #returns the complete object
  rescue => e
    puts e.to_s #debug the error
  end
end
_sales_add(sale_data) click to toggle source

Call to the BIMS API “sales_add” method and gets the whole response body

# File lib/bims_api_client.rb, line 117
def _sales_add(sale_data)
  begin
    return set_error_object({message: @configuration.not_initialized_message}) if not_initialized?
    url = "#{base_uri}/#{@configuration.sales_path}/add.#{@configuration.api_request_format}"
    response = post url, body: {data: sale_data}
    JSON.parse (response.body && !response.body.empty?) ? response.body : set_error_object #returns the complete object
  rescue => e
    puts e.to_s #debug the error
  end
end