Module: Trophonius::Request

Defined in:
lib/trophonius_request.rb

Class Method Summary collapse

Class Method Details

.get_tokenString

Gets the current FileMaker token

Returns:

  • (String)

    a valid FileMaker token



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

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
# 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 "Error was #{e}"
    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



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/trophonius_request.rb', line 118

def self.retrieve_all(layout_name, sort)
  if !sort.empty?
    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}"
          }"
        )
      )
  else
    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}"
          }"
        )
      )
  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



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

def self.retrieve_first(layout_name)
  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



102
103
104
105
106
107
108
109
110
111
112
# File 'lib/trophonius_request.rb', line 102

def self.run_script(script, scriptparameter, layout_name)
  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



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

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