ruby-fiix-cmms-client 0.1.1 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 85d93105c64e8d17809da0de80ba5113b108fa85db720633ab0c47570d463be5
4
- data.tar.gz: b93b7c6cd2b7cb56f680d08e270427515afc0e7ac0bb6d31529ddc858693cd7a
3
+ metadata.gz: 853c18ca53326409075479df9752eb7275b87db31ae147e058d3d5a2cb413c18
4
+ data.tar.gz: 2a7a76bc625691de142cb31dd32031ad15b8feba1613208e6e17a9808b361fc4
5
5
  SHA512:
6
- metadata.gz: 34e93a0578c2059165477ac8a61c0e6b7307a6661380d2090f51dc680654b34e372594499a5365dc6a4311f32a2ff84a37138194b31415847ea98afa9edc88ac
7
- data.tar.gz: 04f2f8b3a0c94945a229bbac2da2f0bd5f06d5d8d5aad376e810d4d8a3123f0d13af4a1f1b5b7fd7e0d8c8a6c8c9ed0bfb670d4ce7b9c176224cc801c429b93c
6
+ metadata.gz: 1c21bdbf18edd94292bbaaa4c6f4031047ebc497115a50e7d429e2d8ee7ad8e4799757aef506d7a8d50c1fa483c1ff960884a618e8e027ec6f6e841350d5c15f
7
+ data.tar.gz: 9daceaebdf2a2681a64b35b9e410280818f587144f0ea2bac4ec893c230800d2f391f44db673c87e7642c8a5b78ead4b65694e91dad85912a69adbcab923631d
@@ -30,9 +30,13 @@ module Ruby
30
30
 
31
31
  validate_credentials!
32
32
 
33
- @base_url = Auth.base_url(@subdomain, @api_key, @access_key)
34
- @signature = Auth.sign(@api_secret, @base_url)
35
- @uri = URI.parse(@base_url)
33
+ @base_url = Auth.base_url(@subdomain, @api_key, @access_key)
34
+ @signature = Auth.sign(@api_secret, @base_url)
35
+ @uri = URI.parse(@base_url)
36
+
37
+ @upload_url = Auth.upload_url(@subdomain, @api_key, @access_key)
38
+ @upload_sig = Auth.sign(@api_secret, @upload_url)
39
+ @upload_uri = URI.parse(@upload_url)
36
40
  end
37
41
 
38
42
  OPERATIONS.each do |method_name, request_type|
@@ -44,16 +48,14 @@ module Ruby
44
48
  end
45
49
  end
46
50
 
47
- def upload_file(context)
48
- normalized = normalize_context(context)
49
- file = normalized.delete("file")
50
-
51
- # Build the JSON payload with _maCn and clientVersion, same as regular requests
52
- payload = { "_maCn" => "AddRequest" }
53
- .merge(DEFAULT_CLIENT_VERSION)
54
- .merge(normalized)
51
+ def upload_file(source_info:, source_id:, file:, field_name: "file_part_1")
52
+ description = {
53
+ "sourceInfo" => source_info.to_s,
54
+ "sourceIdString" => source_id.to_s,
55
+ "fieldName" => field_name.to_s
56
+ }
55
57
 
56
- multipart_request(payload, file)
58
+ upload_request(descriptions: [description], files: { field_name.to_s => file })
57
59
  end
58
60
 
59
61
  private
@@ -83,13 +85,13 @@ module Ruby
83
85
  raise ConnectionError, "Failed to connect to Fiix API: #{e.message}"
84
86
  end
85
87
 
86
- def multipart_request(params, file)
88
+ def upload_request(descriptions:, files:)
87
89
  boundary = "----RubyFiixCmms#{SecureRandom.hex(16)}"
88
- body = build_multipart_body(params, file, boundary)
90
+ body = build_upload_body(descriptions, files, boundary)
89
91
 
90
- http = build_http
91
- req = Net::HTTP::Post.new(@uri.request_uri)
92
- req["Authorization"] = @signature
92
+ http = build_http(@upload_uri)
93
+ req = Net::HTTP::Post.new(@upload_uri.request_uri)
94
+ req["Authorization"] = @upload_sig
93
95
  req["Content-Type"] = "multipart/form-data; boundary=#{boundary}"
94
96
  req.body = body
95
97
 
@@ -98,26 +100,25 @@ module Ruby
98
100
  raise ConnectionError, "Failed to connect to Fiix API: #{e.message}"
99
101
  end
100
102
 
101
- def build_http
102
- http = Net::HTTP.new(@uri.host, @uri.port)
103
+ def build_http(uri = @uri)
104
+ http = Net::HTTP.new(uri.host, uri.port)
103
105
  http.use_ssl = true
104
106
  http
105
107
  end
106
108
 
107
- def build_multipart_body(params, file, boundary)
109
+ def build_upload_body(descriptions, files, boundary)
108
110
  parts = []
109
111
 
110
- # Send all params as a single JSON field named "data"
111
112
  parts << "--#{boundary}\r\n" \
112
- "Content-Disposition: form-data; name=\"data\"\r\n" \
113
- "Content-Type: text/plain;charset=utf-8\r\n\r\n" \
114
- "#{JSON.generate(params)}\r\n"
113
+ "Content-Disposition: form-data; name=\"descriptions\"\r\n" \
114
+ "Content-Type: application/json\r\n\r\n" \
115
+ "#{JSON.generate(descriptions)}\r\n"
115
116
 
116
- if file
117
+ files.each do |field_name, file|
117
118
  filename = file.respond_to?(:path) ? File.basename(file.path) : "upload"
118
119
  file_content = file.respond_to?(:read) ? file.read : file
119
120
  parts << "--#{boundary}\r\n" \
120
- "Content-Disposition: form-data; name=\"file\"; filename=\"#{filename}\"\r\n" \
121
+ "Content-Disposition: form-data; name=\"#{field_name}\"; filename=\"#{filename}\"\r\n" \
121
122
  "Content-Type: application/octet-stream\r\n\r\n" \
122
123
  "#{file_content}\r\n"
123
124
  end
@@ -15,6 +15,12 @@ module Ruby
15
15
  "&signatureMethod=HmacSHA256&signatureVersion=1"
16
16
  end
17
17
 
18
+ def upload_url(subdomain, api_key, access_key)
19
+ "https://#{subdomain}.macmms.com/api/upload" \
20
+ "?action=uploadFile&appKey=#{api_key}&accessKey=#{access_key}" \
21
+ "&signatureMethod=HmacSHA256&signatureVersion=1"
22
+ end
23
+
18
24
  def sign(api_secret, url)
19
25
  message = url.sub("https://", "")
20
26
  OpenSSL::HMAC.hexdigest("SHA256", api_secret, message)
@@ -4,7 +4,7 @@ module Ruby
4
4
  module Fiix
5
5
  module Cmms
6
6
  module Client
7
- VERSION = "0.1.1"
7
+ VERSION = "0.2.0"
8
8
  end
9
9
  end
10
10
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby-fiix-cmms-client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ronald Langeveld