class SP::Duh::JSONAPI::Service

Public Class Methods

new(pg_connection, url, default_adapter = SP::Duh::JSONAPI::Adapters::Db) click to toggle source
# File lib/sp/duh/jsonapi/service.rb, line 34
def initialize(pg_connection, url, default_adapter = SP::Duh::JSONAPI::Adapters::Db)
  @pg_connection = pg_connection
  @url           = url
  protocol       = :db
  @configuration = Configuration.new(pg_connection, url)
  @adapter       = default_adapter
  adapter unless url.nil?
end
protocols() click to toggle source
# File lib/sp/duh/jsonapi/service.rb, line 14
def self.protocols ; [ :db, :http ] ; end

Public Instance Methods

adapter() click to toggle source
# File lib/sp/duh/jsonapi/service.rb, line 19
def adapter
  raise Exceptions::ServiceSetupError.new('JSONAPI prefix not specified', nil) if url.blank?
  @adapter_instance ||= @adapter.new(self)
  SP::Duh::JSONAPI::Model::Base.adapter ||= @adapter_instance
  @adapter_instance
end
clear_jsonapi_args() click to toggle source
# File lib/sp/duh/jsonapi/service.rb, line 61
def clear_jsonapi_args                       ; @parameters = nil ; end
close() click to toggle source
# File lib/sp/duh/jsonapi/service.rb, line 52
def close
  @pg_connection.close if !@pg_connection.nil? && !@pg_connection.finished?
  @adapter_instance = nil
  @adapter          = nil
  @url              = nil
  @configuration    = nil
end
configuration() click to toggle source
# File lib/sp/duh/jsonapi/service.rb, line 18
def configuration ; @configuration ; end
connection() click to toggle source
# File lib/sp/duh/jsonapi/service.rb, line 15
def connection ; @pg_connection ; end
parameters() click to toggle source
# File lib/sp/duh/jsonapi/service.rb, line 62
def parameters                               ; @parameters ; end
protocol() click to toggle source
# File lib/sp/duh/jsonapi/service.rb, line 26
def protocol ; @protocol ; end
protocol=(value) click to toggle source
# File lib/sp/duh/jsonapi/service.rb, line 27
def protocol=(value)
  if !value.to_sym.in?(Service.protocols)
    raise Exceptions::ServiceProtocolError.new(protocol: value.to_sym, protocols: Service.protocols.join(', '))
  end
  @protocol = value.to_sym
end
set_jsonapi_parameters(parameters = nil) click to toggle source
# File lib/sp/duh/jsonapi/service.rb, line 60
def set_jsonapi_parameters(parameters = nil) ; @parameters = parameters ; end
set_url(value) click to toggle source
# File lib/sp/duh/jsonapi/service.rb, line 17
def set_url(value) ; @url = value ; end
setup() click to toggle source
# File lib/sp/duh/jsonapi/service.rb, line 43
def setup
  begin
    create_jsonapi_function()
  rescue StandardError => e
    raise Exceptions::ServiceSetupError.new(nil, e)
  end
  configuration.setup()
end
url() click to toggle source
# File lib/sp/duh/jsonapi/service.rb, line 16
def url ; @url ; end

Private Instance Methods

create_jsonapi_function() click to toggle source
# File lib/sp/duh/jsonapi/service.rb, line 66
def create_jsonapi_function
  connection.exec %Q[

    CREATE OR REPLACE FUNCTION public.jsonapi (
      IN method               text,
      IN uri                  text,
      IN body                 text,
      IN user_id              text,
      IN company_id           text,
      IN company_schema       text,
      IN sharded_schema       text,
      IN accounting_schema    text,
      IN accounting_prefix    text,
      OUT http_status         integer,
      OUT response            text
    ) RETURNS record AS '$libdir/pg-jsonapi.so', 'jsonapi' LANGUAGE C;

    CREATE OR REPLACE FUNCTION public.inside_jsonapi (
    ) RETURNS boolean AS '$libdir/pg-jsonapi.so', 'inside_jsonapi' LANGUAGE C;

    CREATE OR REPLACE FUNCTION public.get_jsonapi_user (
    ) RETURNS text AS '$libdir/pg-jsonapi.so', 'get_jsonapi_user' LANGUAGE C;

    CREATE OR REPLACE FUNCTION public.get_jsonapi_company (
    ) RETURNS text AS '$libdir/pg-jsonapi.so', 'get_jsonapi_company' LANGUAGE C;

    CREATE OR REPLACE FUNCTION public.get_jsonapi_company_schema (
    ) RETURNS text AS '$libdir/pg-jsonapi.so', 'get_jsonapi_company_schema' LANGUAGE C;

    CREATE OR REPLACE FUNCTION public.get_jsonapi_sharded_schema (
    ) RETURNS text AS '$libdir/pg-jsonapi.so', 'get_jsonapi_sharded_schema' LANGUAGE C;

    CREATE OR REPLACE FUNCTION public.get_jsonapi_accounting_schema (
    ) RETURNS text AS '$libdir/pg-jsonapi.so', 'get_jsonapi_accounting_schema' LANGUAGE C;

    CREATE OR REPLACE FUNCTION public.get_jsonapi_accounting_prefix (
    ) RETURNS text AS '$libdir/pg-jsonapi.so', 'get_jsonapi_accounting_prefix' LANGUAGE C;

  ]
end