Class: TinyQueries

Inherits:
Object
  • Object
show all
Defined in:
lib/tiny-queries.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeTinyQueries

Constructor



20
21
22
23
# File 'lib/tiny-queries.rb', line 20

def initialize()
	@connector 	= nil
	@globals 	= {}
end

Instance Attribute Details

#connectorObject

:connector should be set to the path to connector.php



13
14
15
# File 'lib/tiny-queries.rb', line 13

def connector
  @connector
end

#globalsObject

:globals is a hash to be used for global query parameters Global query parameters are sent to each query call along with the regular query parameters



17
18
19
# File 'lib/tiny-queries.rb', line 17

def globals
  @globals
end

Instance Method Details

#run(term, params = {}) ⇒ Object

Runs a query term and returns the result as a hash Params:

term

query term, like “a” or “a(b)” or “a:b”

params

a hash for the query parameters



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/tiny-queries.rb', line 29

def run(term, params={})

	# Check if connector is set
	if @connector.nil?
		raise "You need to set a PHP-connector first - calling queries without connector is not supported yet"
	end

	if params.class != Hash
		raise "Parameters should be passed as a Hash"
	end
	
	# For backwards compatibility check if there is "php " prepended to @connector and remove it
	@connector = @connector.sub(/^php\s+/, '');
	
	# Execute the command and capture the output
	json = exec "php", @connector, term, JSON.generate( params ), JSON.generate( @globals );
	
	result = JSON.parse( json )
	
	# Check if result contains error message
	if result.class == Hash and result.has_key?("error")
		raise result["error"]
	end
	
	return result
end