Class: LumidatumClient

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(authentication_token, model_id = nil, host_address = "https://www.lumidatum.com", http_client = nil, file_handler = File) ⇒ LumidatumClient

Returns a new instance of LumidatumClient.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/lumidatum_client.rb', line 13

def initialize(authentication_token, model_id=nil, host_address="https://www.lumidatum.com", http_client=nil, file_handler=File)
  if authentication_token == nil
    raise ArgumentError, "authentication_token must not be nil"
  end

  @authentication_token = authentication_token
  @model_id = model_id
  @host_address = host_address.chomp("/")

  if http_client == nil
    @http_client = HTTPClient.new
  else
    @http_client = http_client;
  end

  @file_handler = file_handler
end

Instance Attribute Details

#authentication_tokenObject

Returns the value of attribute authentication_token.



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

def authentication_token
  @authentication_token
end

#file_handlerObject

Returns the value of attribute file_handler.



11
12
13
# File 'lib/lumidatum_client.rb', line 11

def file_handler
  @file_handler
end

#host_addressObject

Returns the value of attribute host_address.



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

def host_address
  @host_address
end

#http_clientObject

Returns the value of attribute http_client.



10
11
12
# File 'lib/lumidatum_client.rb', line 10

def http_client
  @http_client
end

#model_idObject

Returns the value of attribute model_id.



8
9
10
# File 'lib/lumidatum_client.rb', line 8

def model_id
  @model_id
end

Instance Method Details

#_getModelIdOrError(model_id) ⇒ Object



200
201
202
203
204
205
206
207
# File 'lib/lumidatum_client.rb', line 200

def _getModelIdOrError(model_id)
  model_id = if model_id == nil then @model_id else model_id end
  if model_id == nil
    raise ArgumentError, "model_id must be set during client instantiation or provided during your instance method call."
  end

  return model_id
end

#api(http_method, url_endpoint, url_query_parameters, parameters, deserialize_response: true) ⇒ Object



169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/lumidatum_client.rb', line 169

def api(http_method, url_endpoint, url_query_parameters, parameters, deserialize_response: true)
  formatted_url = "#{@host_address}/#{url_endpoint}"
  if url_query_parameters
    formatted_url = formatted_url + "?" + URI.encode_www_form(url_query_parameters)
  end

  headers = {"authorization" => @authentication_token, "content-type" => "application/json"}

  if http_method == "GET"
    api_response = @http_client.get(formatted_url, header: headers)
  elsif http_method == "POST"
    if parameters.class != String
      parameters_str = JSON.generate(parameters)
    else
      parameters_str = parameters
    end
    api_response = @http_client.post(formatted_url, header: headers, body: parameters_str)
  end

  if deserialize_response
    if api_response.status == 200 or api_response.status == 201

      return JSON.parse(api_response.body)
    end
  else

    return api_response
  end
end

#getAvailableReports(report_type, sub_type, model_id, zipped: true, latest: true) ⇒ Object



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/lumidatum_client.rb', line 129

def getAvailableReports(report_type, sub_type, model_id, zipped: true, latest: true)
  if sub_type
    url_query_parameters = {:model_id => model_id, :report_type => report_type, :sub_type => sub_type, :zipped => zipped, :latest => true}
  else
    url_query_parameters = {:model_id => model_id, :report_type => report_type, :zipped => zipped, :latest => true}
  end

  list_reports_response = api("GET", "api/data", url_query_parameters, model_id, deserialize_response: false)
  list_reports_response_object = JSON.parse(list_reports_response.body)

  if list_reports_response.status != 200
    raise IOError, "HTTP #{list_reports_response.status}: #{list_reports_response_object["error"]} "
  end

  if list_reports_response_object["latest_key_name"] != nil

    return list_reports_response_object["latest_key_name"]
  else

    return list_reports_response_object["available_key_names"]
  end
end

#getItemRecommendations(parameters, model_id: nil, deserialize_response: true) ⇒ Object



32
33
34
35
36
# File 'lib/lumidatum_client.rb', line 32

def getItemRecommendations(parameters, model_id: nil, deserialize_response: true)
  model_id = _getModelIdOrError(model_id)

  return api("POST", "api/predict/#{model_id}", nil, parameters, deserialize_response: deserialize_response)
end

#getLatestLtvReport(download_file_path, sub_type: nil, model_id: nil, zipped: true, stream_download: true) ⇒ Object

File download



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

def getLatestLtvReport(download_file_path, sub_type: nil, model_id: nil, zipped: true, stream_download: true)
  model_id = _getModelIdOrError(model_id)

  latest_key_name = getAvailableReports("LTV", sub_type, model_id, zipped: zipped)

  presigned_response_object = getPresignedResponse(latest_key_name, model_id, is_download: true)

  report_response = @http_client.get(presigned_response_object["url"])
  open(download_file_path, "wb") do |file|
    file.write(report_response.body)
  end

  return report_response
end

#getLatestSegmentationReport(download_file_path, sub_type: nil, model_id: nil, zipped: true, stream_download: true) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/lumidatum_client.rb', line 115

def getLatestSegmentationReport(download_file_path, sub_type: nil, model_id: nil, zipped: true, stream_download: true)
  model_id = _getModelIdOrError(model_id)

  latest_key_name = getAvailableReports("SEG", sub_type, model_id, zipped: zipped)
  presigned_response_object = getPresignedResponse(latest_key_name, model_id, is_download: true)

  report_response = @http_client.get(presigned_response_object["url"])
  open(download_file_path, "wb") do |file|
    file.write(report_response.body)
  end

  return report_response
end

#getPresignedResponse(key_name, model_id, data_type: nil, file_name: nil, file_size: nil, is_download: false) ⇒ Object



152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/lumidatum_client.rb', line 152

def getPresignedResponse(key_name, model_id, data_type: nil, file_name: nil, file_size: nil, is_download: false)
  parameters = {"model_id" => model_id}

  if is_download
    parameters["key_name"] = key_name
    parameters["is_download"] = true
  else
    parameters["data_type"] = data_type
    parameters["file_name"] = file_name
    parameters["file_size"] = file_size
    parameters["presigned_url_http_method"] = "PUT"
  end

  return api("POST", "api/data", nil, parameters)
end

#getUserRecommendations(parameters, model_id: nil, deserialize_response: true) ⇒ Object



38
39
40
41
42
# File 'lib/lumidatum_client.rb', line 38

def getUserRecommendations(parameters, model_id: nil, deserialize_response: true)
  model_id = _getModelIdOrError(model_id)

  return api("POST", "api/predict/#{model_id}", nil, parameters, deserialize_response: deserialize_response)
end

#sendData(data_type, data_string, file_path, model_id) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/lumidatum_client.rb', line 70

def sendData(data_type, data_string, file_path, model_id)
  model_id = _getModelIdOrError(model_id)

  url_endpoint = "api/data"

  if data_string != nil
    url_query_parameters = {:model_id => model_id, :data_type => data_type}

    return api("POST", url_endpoint, url_query_parameters, data_string, deserialize_response: false)
  else
    file_name = File.basename(file_path)
    file_size = nil
    presigned_response_object = getPresignedResponse(
      nil,
      model_id,
      data_type: data_type,
      file_name: file_name,
      file_size: file_size
    )
  end

  @file_handler.open(file_path) do |upload_file|
    form_fields = presigned_response_object["fields"]
    form_fields["file"] = upload_file

    return @http_client.post(presigned_response_object["url"], form_fields)
  end
end

#sendFeedbackData(data_string: nil, file_path: nil, model_id: nil) ⇒ Object



64
65
66
67
68
# File 'lib/lumidatum_client.rb', line 64

def sendFeedbackData(data_string: nil, file_path: nil, model_id: nil)
  model_id = _getModelIdOrError(model_id)

  return sendData("feedback", data_string, file_path, model_id)
end

#sendItemData(data_string: nil, file_path: nil, model_id: nil) ⇒ Object



52
53
54
55
56
# File 'lib/lumidatum_client.rb', line 52

def sendItemData(data_string: nil, file_path: nil, model_id: nil)
  model_id = _getModelIdOrError(model_id)

  return sendData("items", data_string, file_path, model_id)
end

#sendTransactionData(data_string: nil, file_path: nil, model_id: nil) ⇒ Object



58
59
60
61
62
# File 'lib/lumidatum_client.rb', line 58

def sendTransactionData(data_string: nil, file_path: nil, model_id: nil)
  model_id = _getModelIdOrError(model_id)

  return sendData("transactions", data_string, file_path, model_id)
end

#sendUserData(data_string: nil, file_path: nil, model_id: nil) ⇒ Object

Data upload



46
47
48
49
50
# File 'lib/lumidatum_client.rb', line 46

def sendUserData(data_string: nil, file_path: nil, model_id: nil)
  model_id = _getModelIdOrError(model_id)

  return sendData("users", data_string, file_path, model_id)
end