ruby-fiix-cmms-client 0.1.0 → 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: e9869f6020778153f7836b0d77af9238ffa5f334192098a76bb421117cbd70d8
4
- data.tar.gz: c065c5d1e9a47cf28e70ecda4c9698d6b5212f78105302bccdb129e40c7e65cf
3
+ metadata.gz: 853c18ca53326409075479df9752eb7275b87db31ae147e058d3d5a2cb413c18
4
+ data.tar.gz: 2a7a76bc625691de142cb31dd32031ad15b8feba1613208e6e17a9808b361fc4
5
5
  SHA512:
6
- metadata.gz: 6273384a2acc4ba6297dac1cf182b97f621446e95f825b1172e70fb99bcf46e4839b1aecc429c19d7d83cfb865fc2c9acb2167e9c7787a3daf206a35d835142a
7
- data.tar.gz: da1cc68df6842f83a835442ecd6d5f2fb59fc992106bc7c9dc6707bbbc0b6062099d5e4659158afbf5b79eae2a16198626fa10134920600506545effc0e0f6b2
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,10 +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
- multipart_request(normalized, file)
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
+ }
57
+
58
+ upload_request(descriptions: [description], files: { field_name.to_s => file })
51
59
  end
52
60
 
53
61
  private
@@ -77,13 +85,13 @@ module Ruby
77
85
  raise ConnectionError, "Failed to connect to Fiix API: #{e.message}"
78
86
  end
79
87
 
80
- def multipart_request(params, file)
88
+ def upload_request(descriptions:, files:)
81
89
  boundary = "----RubyFiixCmms#{SecureRandom.hex(16)}"
82
- body = build_multipart_body(params, file, boundary)
90
+ body = build_upload_body(descriptions, files, boundary)
83
91
 
84
- http = build_http
85
- req = Net::HTTP::Post.new(@uri.request_uri)
86
- req["Authorization"] = @signature
92
+ http = build_http(@upload_uri)
93
+ req = Net::HTTP::Post.new(@upload_uri.request_uri)
94
+ req["Authorization"] = @upload_sig
87
95
  req["Content-Type"] = "multipart/form-data; boundary=#{boundary}"
88
96
  req.body = body
89
97
 
@@ -92,26 +100,25 @@ module Ruby
92
100
  raise ConnectionError, "Failed to connect to Fiix API: #{e.message}"
93
101
  end
94
102
 
95
- def build_http
96
- http = Net::HTTP.new(@uri.host, @uri.port)
103
+ def build_http(uri = @uri)
104
+ http = Net::HTTP.new(uri.host, uri.port)
97
105
  http.use_ssl = true
98
106
  http
99
107
  end
100
108
 
101
- def build_multipart_body(params, file, boundary)
109
+ def build_upload_body(descriptions, files, boundary)
102
110
  parts = []
103
111
 
104
- params.each do |key, value|
105
- parts << "--#{boundary}\r\n" \
106
- "Content-Disposition: form-data; name=\"#{key}\"\r\n\r\n" \
107
- "#{value}\r\n"
108
- end
112
+ parts << "--#{boundary}\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"
109
116
 
110
- if file
117
+ files.each do |field_name, file|
111
118
  filename = file.respond_to?(:path) ? File.basename(file.path) : "upload"
112
119
  file_content = file.respond_to?(:read) ? file.read : file
113
120
  parts << "--#{boundary}\r\n" \
114
- "Content-Disposition: form-data; name=\"file\"; filename=\"#{filename}\"\r\n" \
121
+ "Content-Disposition: form-data; name=\"#{field_name}\"; filename=\"#{filename}\"\r\n" \
115
122
  "Content-Type: application/octet-stream\r\n\r\n" \
116
123
  "#{file_content}\r\n"
117
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.0"
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.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ronald Langeveld