Module: Trophonius::Request

Defined in:
lib/trophonius_request.rb

Class Method Summary collapse

Class Method Details

.get_layout_field_names(layout_name) ⇒ JSON

Retrieves the fieldnames of a layout

Returns:

  • (JSON)

    The fieldnames of a layout



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/trophonius_request.rb', line 104

def self.get_layout_field_names(layout_name)
  uri = URI::RFC2396_Parser.new
  url =
    URI(
      uri.escape(
        "http#{Trophonius.config.ssl == true ? 's' : ''}://#{Trophonius.config.host}/fmi/data/v1/databases/#{
          Trophonius.config.database
        }/layouts/#{layout_name}"
      )
    )
  begin
    make_request(url, "Bearer #{get_token}", 'get', '{}')['response']['fieldMetaData'].map { |field| field['name'] }
  rescue StandardError => e
    puts e
    puts e.backtrace
    Error.throw_error('1631')
  end
end

.get_tokenString

Gets the current FileMaker token

Returns:

  • (String)

    a valid FileMaker token



79
80
81
# File 'lib/trophonius_request.rb', line 79

def self.get_token
  Connection.valid_connection? ? Connection.token : Connection.connect
end

.make_request(url_param, auth, method, body, params = '') ⇒ JSON

Crafts and runs a HTTP request of any type

Parameters:

  • urlparam: (URI)

    the url to make the request to

  • auth: (String)

    the authentication required for the request

  • method: (String)

    the type of HTTP request to make (i.e. get)

  • body: (JSONString)

    the body of the HTTP request

  • params: (String)

    optional parameters added to the request

Returns:

  • (JSON)

    parsed json of the response



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/trophonius_request.rb', line 23

def self.make_request(url_param, auth, method, body, params = '')
  ssl_verifyhost = Trophonius.config.local_network ? 0 : 2
  ssl_verifypeer = !Trophonius.config.local_network
  request =
    Typhoeus::Request.new(
      url_param,
      method: method.to_sym,
      body: body,
      params: params,
      ssl_verifyhost: ssl_verifyhost,
      ssl_verifypeer: ssl_verifypeer,
      headers: { 'Content-Type' => 'application/json', Authorization: auth.to_s }
    )
  temp = request.run
  begin
    JSON.parse(temp.response_body)
  rescue Exception => e
    puts e
    puts e.backtrace
    Error.throw_error('1631')
  end
end

.retrieve_all(layout_name, sort) ⇒ JSON

Retrieves the 10000000 records from FileMaker

Returns:

  • (JSON)

    The first 10000000 records from FileMaker



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/trophonius_request.rb', line 144

def self.retrieve_all(layout_name, sort)
  uri = URI::RFC2396_Parser.new
  if sort.empty?
    url =
      URI(
        uri.escape(
          "http#{Trophonius.config.ssl == true ? 's' : ''}://#{Trophonius.config.host}/fmi/data/v1/databases/#{
            Trophonius.config.database
          }/layouts/#{layout_name}/records?_limit=10000000#{
            Trophonius.config.count_result_script == '' ? '' : "&script=#{Trophonius.config.count_result_script}"
          }"
        )
      )
  else
    sort_order = sort.to_json.to_s
    url =
      URI(
        uri.escape(
          "http#{Trophonius.config.ssl == true ? 's' : ''}://#{Trophonius.config.host}/fmi/data/v1/databases/#{
            Trophonius.config.database
          }/layouts/#{layout_name}/records?_limit=10000000&_sort=#{sort_order}#{
            Trophonius.config.count_result_script == '' ? '' : "&script=#{Trophonius.config.count_result_script}"
          }"
        )
      )
  end
  make_request(url, "Bearer #{get_token}", 'get', '{}')
end

.retrieve_first(layout_name) ⇒ JSON

Retrieves the first record from FileMaker

Returns:

  • (JSON)

    The first record from FileMaker



87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/trophonius_request.rb', line 87

def self.retrieve_first(layout_name)
  uri = URI::RFC2396_Parser.new
  url =
    URI(
      uri.escape(
        "http#{Trophonius.config.ssl == true ? 's' : ''}://#{Trophonius.config.host}/fmi/data/v1/databases/#{
          Trophonius.config.database
        }/layouts/#{layout_name}/records?_limit=1"
      )
    )
  make_request(url, "Bearer #{get_token}", 'get', '{}')
end

.run_script(script, scriptparameter, layout_name) ⇒ JSON

Runs a FileMaker script

Returns:

  • (JSON)

    The script result from FileMaker



127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/trophonius_request.rb', line 127

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

.upload_file_request(url_param, auth, file) ⇒ JSON

Crafts and runs a HTTP request for uploading a file to a container

Parameters:

  • urlparam: (URI)

    the url to make the request to

  • auth: (String)

    the authentication required for the request

  • file: (Tempfile or File)

    file to upload

Returns:

  • (JSON)

    parsed json of the response



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/trophonius_request.rb', line 56

def self.upload_file_request(url_param, auth, file)
  url = URI(url_param.to_s)

  https = Net::HTTP.new(url.host, url.port)
  https.use_ssl = true

  request = Net::HTTP::Post.new(url)
  request['Authorization'] = auth.to_s
  request['Content-Type'] = 'multipart/form-data;'
  form_data = [['upload', file]]
  request.set_form form_data, 'multipart/form-data'
  response = https.request(request)
  begin
    JSON.parse(response.read_body)
  rescue Exception
    Error.throw_error('1631')
  end
end