Class: Trophonius::Single

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config:) ⇒ Single



9
10
11
12
13
14
# File 'lib/single.rb', line 9

def initialize(config:)
  @config = config
  @query = {}
  @translations = {}
  @all_fields = {}
end

Instance Attribute Details

#queryObject (readonly)

Returns the value of attribute query.



7
8
9
# File 'lib/single.rb', line 7

def query
  @query
end

Instance Method Details

#firstObject



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/single.rb', line 51

def first
  uri = URI::RFC2396_Parser.new
  url =
    URI(
      uri.escape(
        "http#{@config[:ssl] == true ? 's' : ''}://#{@config[:host]}/fmi/data/v1/databases/#{@config[:database]}/layouts/#{
          @config[:layout_name]
        }/records?_limit=1"
      )
    )

  token = setup_connection
  response = make_request(url, token, 'get', @query.to_json)

  r_results = response['response']['data']
  if response['messages'][0]['code'] != '0' && response['messages'][0]['code'] != '401'
    close_connection(token)
    Error.throw_error(response['messages'][0]['code'])
  elsif response['messages'][0]['code'] == '401'
    close_connection(token)
    return RecordSet.new(@config[:layout_name], @config[:non_modifiable_fields])
  else
    ret_val = RecordSet.new(@config[:layout_name], @config[:non_modifiable_fields])
    r_results.each do |r|
      hash = build_result(r)
      ret_val << hash
    end
  end
  close_connection(token)

  ret_val
end

#run_script(script:, scriptparameter:) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/single.rb', line 84

def run_script(script:, scriptparameter:)
  uri = URI::RFC2396_Parser.new
  url =
    URI(
      uri.escape(
        "http#{@config[:ssl] == true ? 's' : ''}://#{@config[:host]}/fmi/data/v1/databases/#{@config[:database]}/layouts/#{
          @config[:layout_name]
        }/records?_limit=1&script=#{script}&script.param=#{scriptparameter}"
      )
    )

  token = setup_connection
  result = make_request(url, token.to_s, 'get', '{}')
  ret_val = ''

  if result['messages'][0]['code'] != '0'
    close_connection(token)
    Error.throw_error(result['messages'][0]['code'])
  elsif result['response']['scriptResult'] == '403'
    close_connection(token)
    Error.throw_error(403)
  else
    ret_val = result['response']['scriptResult']
  end

  close_connection(token)

  ret_val
end

#where(fieldData) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/single.rb', line 16

def where(fieldData)
  uri = URI::RFC2396_Parser.new
  url =
    URI(
      uri.escape(
        "http#{@config[:ssl] == true ? 's' : ''}://#{@config[:host]}/fmi/data/v1/databases/#{@config[:database]}/layouts/#{
          @config[:layout_name]
        }/_find"
      )
    )
  @query.merge!(query: [fieldData])
  @query.merge!(limit: '10000000')
  token = setup_connection
  response = make_request(url, token, 'post', @query.to_json)

  r_results = response['response']['data']
  if response['messages'][0]['code'] != '0' && response['messages'][0]['code'] != '401'
    close_connection(token)
    Error.throw_error(response['messages'][0]['code'])
  elsif response['messages'][0]['code'] == '401'
    close_connection(token)
    return RecordSet.new(@config[:layout_name], @config[:non_modifiable_fields])
  else
    ret_val = RecordSet.new(@config[:layout_name], @config[:non_modifiable_fields])
    r_results.each do |r|
      hash = build_result(r)
      ret_val << hash
    end
  end
  @query = {}
  close_connection(token)

  ret_val
end