Class: Trophonius::Single

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config:) ⇒ Single

Returns a new instance of Single.



10
11
12
13
14
15
# File 'lib/trophonius_single.rb', line 10

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

Instance Attribute Details

#queryObject (readonly)

Returns the value of attribute query.



9
10
11
# File 'lib/trophonius_single.rb', line 9

def query
  @query
end

Instance Method Details

#run_script(script:, scriptparameter:) ⇒ Object



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
# File 'lib/trophonius_single.rb', line 51

def run_script(script:, scriptparameter:)
  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, "Bearer #{token}", '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)

  return ret_val
end

#where(fieldData) ⇒ Object



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/trophonius_single.rb', line 17

def where(fieldData)
  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)

  return ret_val
end