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



77
78
79
80
81
82
83
# File 'lib/trophonius_request.rb', line 77

def self.get_token
  if Connection.valid_connection?
    Connection.token
  else
    Connection.connect
  end
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



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

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
    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



107
108
109
110
111
112
113
114
115
# File 'lib/trophonius_request.rb', line 107

def self.retrieve_all(layout_name, sort)
  if !sort.empty?
    sort_order = sort.to_json.to_s
    url = URI("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("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



89
90
91
92
# File 'lib/trophonius_request.rb', line 89

def self.retrieve_first(layout_name)
  url = URI("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



98
99
100
101
# File 'lib/trophonius_request.rb', line 98

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



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

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

  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