pdfmonkey 0.1.0 → 0.2.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: 64be5f191201c74accbf5eb0c747fac13d4573717b7d1650a88a2d8ac7f7c763
4
- data.tar.gz: 160ddcfec092f8417f1e65f453c77bd754e9f81b9c7ef07f0dda9fb96a2bf706
3
+ metadata.gz: b12f7afdcc1eefd026e54906aed36c86d17b467ea000e8e0b7379b567c34c5d4
4
+ data.tar.gz: f953fd4a0568f46d8aec981b72cf94eb86c6c8c9fe5d42ecfc0eddbf8cb57c38
5
5
  SHA512:
6
- metadata.gz: f9ad7a42896a544d776df651008b617d674c1f14104c663d98a25a22cfc3e85dd2764e6f3f334bd1cff7dab097a0005dce61532bbf05a5b37c0fe715298be8aa
7
- data.tar.gz: 55925f2faa3d549bc2412194ba9412d56ff3ebcea99ebf686b8f7dec471e169bc86929552e29d0d3a45aca2fe09a15986c8e43db41206c4733947158ab7f1d2e
6
+ metadata.gz: 98348f398fffbabc5ea8b480cbfe6906a1e7eb252f04eb695483688fa36ae0352731498d6eddbe23d81ed38195f82237d11f5fe5ecdfac2bd67dad5a36c8aed1
7
+ data.tar.gz: 5e123b0f7e8f645f0ba96fed50d1aa5bfa4a1e76a8827d673f701d4c065bcc2252ce8f330d8bb719c87f6b6626a59c377265a519d691fa8acdccb4eb08d19992
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ # Changelog
2
+
3
+ ## 0.2.0
4
+
5
+ * Handling HTTP and API errors and exposing the error messages
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- pdfmonkey (0.1.0)
4
+ pdfmonkey (0.2.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/README.md CHANGED
@@ -104,6 +104,28 @@ curl <url of your app> \
104
104
  }'
105
105
  ```
106
106
 
107
+ #### Error handling
108
+
109
+ In case of error, be it an HTTP layer error or an API error, `document.status` will be set to `'error'` and `document.error` will contain the error message.
110
+
111
+ ```ruby
112
+ # Using an unknown template
113
+
114
+ tempalte_id = 'unknown'
115
+ data = { name: 'John Doe' }
116
+
117
+ document = Pdfmonkey::Document.generate(template_id, data)
118
+
119
+ document.status # => 'error'
120
+ document.errors # => ["Couldn't find DocumentTemplate with 'id'=unknown"]
121
+
122
+ # If the network is down
123
+ document = Pdfmonkey::Document.generate(template_id, data)
124
+
125
+ document.status # => 'error'
126
+ document.errors # => ["Failed to open TCP connection to api.pdfmonkey.io:443 (getaddrinfo: nodename nor servname provided, or not known)"]
127
+ ```
128
+
107
129
  ## Development
108
130
 
109
131
  After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
@@ -10,7 +10,14 @@ module Pdfmonkey
10
10
 
11
11
  def call(method, resource)
12
12
  response = send_request(method, resource)
13
- extract_attributes(response, resource)
13
+
14
+ if response.is_a?(Net::HTTPSuccess)
15
+ extract_attributes(response, resource)
16
+ else
17
+ extract_errors(response)
18
+ end
19
+ rescue StandardError => e
20
+ { errors: [e.message], status: 'error' }
14
21
  end
15
22
 
16
23
  private def build_get_request(uri, _resource)
@@ -28,6 +35,11 @@ module Pdfmonkey
28
35
  JSON.parse(response.body).fetch(member)
29
36
  end
30
37
 
38
+ private def extract_errors(response)
39
+ payload = JSON.parse(response.body)
40
+ { errors: payload['errors'], status: 'error' }
41
+ end
42
+
31
43
  private def headers
32
44
  {
33
45
  'Authorization' => "Bearer #{config.private_key}",
@@ -14,6 +14,7 @@ module Pdfmonkey
14
14
  created_at
15
15
  document_template_id
16
16
  download_url
17
+ errors
17
18
  id
18
19
  meta
19
20
  payload
@@ -22,7 +23,7 @@ module Pdfmonkey
22
23
  updated_at
23
24
  ].freeze
24
25
 
25
- COMPLETE_STATUSES = %w[failure success].freeze
26
+ COMPLETE_STATUSES = %w[error failure success].freeze
26
27
  COLLECTION = 'documents'
27
28
  MEMBER = 'document'
28
29
 
@@ -57,7 +58,10 @@ module Pdfmonkey
57
58
  end
58
59
 
59
60
  def to_json
60
- { document: attributes.to_h }.to_json
61
+ attrs = attributes.to_h
62
+ attrs.delete(:errors)
63
+
64
+ { document: attrs }.to_json
61
65
  end
62
66
 
63
67
  private def save
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Pdfmonkey
4
- VERSION = '0.1.0'
4
+ VERSION = '0.2.0'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pdfmonkey
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
  - Simon Courtois
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-01-11 00:00:00.000000000 Z
11
+ date: 2019-05-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -91,6 +91,7 @@ files:
91
91
  - ".rspec"
92
92
  - ".rubocop.yml"
93
93
  - ".travis.yml"
94
+ - CHANGELOG.md
94
95
  - CODE_OF_CONDUCT.md
95
96
  - Gemfile
96
97
  - Gemfile.lock