credova 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b8e49e35b8bb7e538575a92ae35a2e62a24b4812a7fb8d7d6aa42c62d33cffac
4
- data.tar.gz: 943709d8daeecf7061cd54b085e8d89b3b2b54387f000bb5ff5f18a81f75c571
3
+ metadata.gz: 9c7b9f63eb58dc7bf593f4b686a6a7ab01e1e52c025a0bc4267610ab885ed547
4
+ data.tar.gz: 1ce8e493fd73683012c62401c20aef6ff60642bf8ca8b4dabdf572c4b8d8d8bc
5
5
  SHA512:
6
- metadata.gz: 1c3f6bc4a6fd36cea80a7a5a9b99b94b9ce0d33aff3f26b74cc4f984972db8b1b7c2aa316953d58257e39b381f5e7a331e736a9490358385c11c711408da4762
7
- data.tar.gz: cd87d085afcbe982e49c992cafc9cbf56d417e488c8bb5aa90eaeb45df8c62205de3143d766d0f3706d8f2294f05151ef37abb30cb901b5a3031ef31e56edf2b
6
+ metadata.gz: 88abc56e0bb002e2e1f9c75a12757b79272a942ce1921c160c281cd27ff117369baf8d60a50a8fc42fea3ba9c5dab6e3231264c73ed90a7d5c01677c23b1ea6a
7
+ data.tar.gz: a8b281388bc4f7898816f0cc168979434012c52627fa5c489cd2eb6aa324e1e46658dd092b0c3c4aa87700b6358829da812932ff45217b7bb302ff86860fc457
data/lib/credova/api.rb CHANGED
@@ -9,26 +9,52 @@ module Credova
9
9
 
10
10
  USER_AGENT = "CredovaRubyGem/#{Credova::VERSION}".freeze
11
11
 
12
+ FILE_UPLOAD_ATTRS = {
13
+ permitted: %i( file_name file_type file_contents ).freeze,
14
+ reqired: %i( file_type file_contents ).freeze,
15
+ }
16
+
12
17
  def get_request(endpoint, headers = {})
13
18
  request = Net::HTTP::Get.new(request_url(endpoint))
14
19
 
15
- process_request(request, {}, headers)
20
+ submit_request(request, {}, headers)
16
21
  end
17
22
 
18
23
  def post_request(endpoint, data = {}, headers = {})
19
24
  request = Net::HTTP::Post.new(request_url(endpoint))
20
25
 
21
- process_request(request, data, headers)
26
+ submit_request(request, data, headers)
27
+ end
28
+
29
+ def post_file_request(endpoint, file_data, headers = {})
30
+ request = Net::HTTP::Post.new(request_url(endpoint))
31
+
32
+ submit_file_request(request, file_data, headers)
22
33
  end
23
34
 
24
35
  private
25
36
 
26
- def process_request(request, data, headers)
37
+ def submit_request(request, data, headers)
27
38
  set_request_headers(request, headers)
28
39
 
29
- uri = URI(request.path)
30
40
  request.body = data.is_a?(Hash) ? data.to_json : data
31
41
 
42
+ process_request(request)
43
+ end
44
+
45
+ def submit_file_request(request, file_data, headers)
46
+ boundary = ::SecureRandom.hex(15)
47
+
48
+ headers.merge!(content_type_header("multipart/form-data; boundary=#{boundary}"))
49
+
50
+ build_multipart_request_body(request, file_data, boundary)
51
+ set_request_headers(request, headers)
52
+ process_request(request)
53
+ end
54
+
55
+ def process_request(request)
56
+ uri = URI(request.path)
57
+
32
58
  response = Net::HTTP.start(uri.host, uri.port, use_ssl: true) do |http|
33
59
  http.request(request)
34
60
  end
@@ -36,6 +62,23 @@ module Credova
36
62
  Credova::Response.new(response)
37
63
  end
38
64
 
65
+ def build_multipart_request_body(request, file_data, boundary)
66
+ file_type = file_data[:file_type]
67
+ file_contents = file_data[:file_contents]
68
+ file_name = file_data[:file_name] || "ffl-document.#{file_type}"
69
+ content_type = "application/#{file_data[:file_type]}"
70
+
71
+ body = []
72
+ body << "--#{boundary}\r\n"
73
+ body << "Content-Disposition: form-data; name=\"file\"; filename=\"#{file_name}\"\r\n"
74
+ body << "Content-Type: #{content_type}\r\n"
75
+ body << "\r\n"
76
+ body << "#{file_contents}\r\n"
77
+ body << "--#{boundary}--\r\n"
78
+
79
+ request.body = body.join
80
+ end
81
+
39
82
  def set_request_headers(request, headers)
40
83
  request['User-Agent'] = USER_AGENT
41
84
 
@@ -58,14 +101,6 @@ module Credova
58
101
  ].join('/')
59
102
  end
60
103
 
61
- def extract_file_extension(ffl_document_url)
62
- metadata_raw = Net::HTTP.get(URI.parse("#{ffl_document_url}/metadata"))
63
- metadata_json = JSON.parse(metadata_raw)
64
- file_name = metadata_json['filename']
65
-
66
- file_name[(file_name.rindex('.') + 1)..-1]
67
- end
68
-
69
104
  def standardize_body_data(submitted_data, permitted_data_attrs)
70
105
  _submitted_data = submitted_data.deep_transform_keys(&:to_sym)
71
106
  permitted_data = (_submitted_data.select! { |k, v| permitted_data_attrs.include?(k) } || _submitted_data)
@@ -79,15 +79,12 @@ module Credova
79
79
  post_request(endpoint, {}, auth_header(@client.access_token))
80
80
  end
81
81
 
82
- def upload_invoice(public_id, invoice_url)
82
+ def upload_invoice(public_id, invoice_file_data)
83
+ requires!(invoice_file_data, *FILE_UPLOAD_ATTRS[:required])
84
+
83
85
  endpoint = ENDPOINTS[:upload_invoice] % public_id
84
- data = { form_data: ['file=@', invoice_url, '; type=application/', extract_file_extension(invoice_url)].join }
85
- headers = [
86
- *auth_header(@client.access_token),
87
- *content_type_header('multipart/form-data'),
88
- ].to_h
89
86
 
90
- post_request(endpoint, data, headers)
87
+ post_file_request(endpoint, invoice_file_data, auth_header(@client.access_token))
91
88
  end
92
89
 
93
90
  end
data/lib/credova/ffl.rb CHANGED
@@ -41,15 +41,12 @@ module Credova
41
41
  get_request(endpoint, auth_header(@client.access_token))
42
42
  end
43
43
 
44
- def upload_document(ffl_public_id, ffl_document_url)
44
+ def upload_document(ffl_public_id, ffl_file_data)
45
+ requires!(ffl_file_data, *FILE_UPLOAD_ATTRS[:required])
46
+
45
47
  endpoint = ENDPOINTS[:upload_document] % ffl_public_id
46
- data = { form_data: ['file=@', ffl_document_url, '; type=application/', extract_file_extension(ffl_document_url)].join }
47
- headers = [
48
- *auth_header(@client.access_token),
49
- *content_type_header('multipart/form-data'),
50
- ].to_h
51
48
 
52
- post_request(endpoint, data, headers)
49
+ post_file_request(endpoint, ffl_file_data, auth_header(@client.access_token))
53
50
  end
54
51
 
55
52
  end
@@ -1,3 +1,3 @@
1
1
  module Credova
2
- VERSION = "0.2.0"
2
+ VERSION = "0.3.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: credova
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jeffrey Dill
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-03-04 00:00:00.000000000 Z
11
+ date: 2019-03-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler