Class: POSLavu::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/poslavu/client.rb

Overview

POSLavu::Client communicates with the POSLavu API over HTTPS.

You must provide a dataname, token, and key to create a Client. From here, you can call Client#invoke to run arbitrary API commands.

Clients don’t hold any state except for a Faraday::Connection, which may or may not use persistent connections depending on your default Faraday adapter.

Defined Under Namespace

Classes: CommandFailedError, CommunicationError, Error

Constant Summary collapse

URL =

The API endpoint as a string

"https://admin.poslavu.com/cp/reqserv/"

Instance Method Summary collapse

Constructor Details

#initialize(dataname, token, key) ⇒ Client

Create a new Client with the specified credentials. These values can be retrieved from this page as required.



38
39
40
41
42
43
44
# File 'lib/poslavu/client.rb', line 38

def initialize(dataname, token, key)
  @parameters = {
    'dataname' => dataname,
    'token' => token,
    'key' => key
  }
end

Instance Method Details

#datanameObject

Returns this Client’s dataname.



47
48
49
# File 'lib/poslavu/client.rb', line 47

def dataname
  @parameters['dataname']
end

#inspectObject

:nodoc:



51
52
53
# File 'lib/poslavu/client.rb', line 51

def inspect #:nodoc:
  "#<POSLavu::Client dataname=#{@parameters['dataname'].inspect}>"
end

#invoke(command, parameters = {}) ⇒ Object

Invokes a command, accepting a parameters hash, and returning an array of POSLavu::Row objects.

The POSLavu API flattens all these parameters into a single POST request. command is broken out as a convenience because #table handles querying, and specifying 'cmd' => 'foo' repeatedly doesn’t feel necessary.



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/poslavu/client.rb', line 73

def invoke(command, parameters = {})
  final_parameters = @parameters.merge(parameters).merge('cmd' => command)

  response = connection.post URL, final_parameters

  fragment = Nokogiri::XML.fragment(response.body)
  elements = fragment.children.select(&:element?)   # .element_children doesn't work
  
  if elements.empty?
    if fragment.to_s.strip.empty?
      # did we actually get no data?
      return []
    else
      # this is apparently how errors are signalled
      raise CommandFailedError, fragment.to_s
    end
  else
    # assume all the elements are <row>s, and let Row explode if we're wrong
    elements.map { |element|
      POSLavu::Row.from_nokogiri(element)
    }
  end
  
rescue Faraday::Error::ClientError
  raise CommunicationError, $!.to_s
end

#table(table) ⇒ Object

Returns an object that allows you to access the specified table.

# Find all orders for a given table
client.table('orders').where('table_id' => 5).each { |row|
  # ...
}

See POSLavu::QueryScope for the query syntax.



63
64
65
# File 'lib/poslavu/client.rb', line 63

def table(table)
  POSLavu::QueryScope.new(self, table)
end