orangedata 0.0.5 → 0.0.6

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.
@@ -64,22 +64,79 @@ module OrangeData
64
64
  }
65
65
  end
66
66
 
67
- def post_entity(sub_url, data)
67
+ class IntermediateResult
68
+ def initialize(
69
+ success:false, sub_url:nil, data:,
70
+ attempt_retry:false, retry_in:nil, retry_count:0, transport:nil,
71
+ errors:nil
72
+ )
73
+ @success = success
74
+ @sub_url = sub_url
75
+ @data = data
76
+ @attempt_retry = attempt_retry
77
+ @retry_in = retry_in
78
+ @retry_count = retry_count
79
+ @transport = transport
80
+ @errors = errors
81
+ end
82
+
83
+ attr_reader :retry_in, :errors, :retry_count
84
+
85
+ def success?
86
+ @success == true
87
+ end
88
+
89
+ def should_retry?
90
+ @attempt_retry || false
91
+ end
92
+
93
+ def retry
94
+ raise "not-retriable" unless should_retry?
95
+ @transport.post_entity(@sub_url, @data, raise_errors:false, result_class:self.class, retry_count:(retry_count + 1))
96
+ end
97
+
98
+ protected
99
+ def get_result_with get_method
100
+ raise "Non-success" unless success?
101
+ @transport.send(get_method,
102
+ @data.respond_to?(:inn) && @data.inn || @data[:inn] || @data["inn"],
103
+ @data.respond_to?(:id) && @data.id || @data[:id] || @data["id"],
104
+ )
105
+ end
106
+ end
107
+
108
+ class ReceiptIntermediateResult < IntermediateResult
109
+ def get_result
110
+ get_result_with(:get_document)
111
+ end
112
+ end
113
+
114
+ class CorrectionIntermediateResult < IntermediateResult
115
+ def get_result
116
+ get_result_with(:get_correction)
117
+ end
118
+ end
119
+
120
+ def post_entity(sub_url, data, raise_errors:true, result_class:IntermediateResult, retry_count:0)
68
121
  res = post_request(sub_url, data)
69
122
 
70
123
  case res.status
71
124
  when 201
72
- return true
125
+ return result_class.new(success: true, data:data, sub_url:sub_url, retry_count:0, transport:self)
73
126
  when 409
74
- raise "Conflict"
127
+ raise "Conflict" if raise_errors
128
+ return result_class.new(data:data, sub_url:sub_url, errors:["Duplicate id"], retry_count:0)
75
129
  when 400
76
- raise "Invalid doc: #{res.body['errors'] || res.body}"
130
+ raise "Invalid doc: #{res.body['errors'] || res.body}" if raise_errors
131
+ return result_class.new(data:data, sub_url:sub_url, errors:res.body['errors'], retry_count:0)
77
132
  when 503
78
133
  if res.headers['Retry-After']
79
- raise "Document queue full, retry in #{res.headers['Retry-After']}"
134
+ raise "Document queue full, retry in #{res.headers['Retry-After']}" if raise_errors
135
+ return result_class.new(attempt_retry:true, retry_in:res.headers['Retry-After'].to_i, data:data, sub_url:sub_url, retry_count:0, transport:self)
80
136
  end
81
137
  end
82
- raise "Unknown code from OD: #{res.status} #{res.reason_phrase} #{res.body}"
138
+ raise "Unknown code from OD: #{res.status} #{res.reason_phrase} #{res.body}" if raise_errors
139
+ result_class.new(attempt_retry:true, data:data, sub_url:sub_url, retry_count:0, transport:self)
83
140
  end
84
141
 
85
142
  def get_entity(sub_url)
@@ -117,16 +174,16 @@ module OrangeData
117
174
  end
118
175
  end
119
176
 
120
- def post_document(data)
121
- post_entity 'documents', data
177
+ def post_document(data, raise_errors:true)
178
+ post_entity 'documents', data, raise_errors:raise_errors, result_class:ReceiptIntermediateResult
122
179
  end
123
180
 
124
181
  def get_document(inn, document_id)
125
182
  ReceiptResult.from_hash(get_entity("documents/#{inn}/status/#{document_id}"))
126
183
  end
127
184
 
128
- def post_correction(data)
129
- post_entity 'corrections', data
185
+ def post_correction(data, raise_errors:true)
186
+ post_entity 'corrections', data, raise_errors:raise_errors, result_class:CorrectionIntermediateResult
130
187
  end
131
188
 
132
189
  def get_correction(inn, document_id)
@@ -2,6 +2,6 @@
2
2
 
3
3
  module OrangeData
4
4
 
5
- VERSION = "0.0.5"
5
+ VERSION = "0.0.6"
6
6
 
7
7
  end
data/tmp/.keep ADDED
File without changes
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: orangedata
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Vasily Fedoseyev
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-10-28 00:00:00.000000000 Z
11
+ date: 2019-06-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday
@@ -147,6 +147,7 @@ files:
147
147
  - ".rspec"
148
148
  - ".rubocop.yml"
149
149
  - ".travis.yml"
150
+ - CHANGELOG.md
150
151
  - Gemfile
151
152
  - Gemfile.lock
152
153
  - LICENSE.txt
@@ -164,6 +165,7 @@ files:
164
165
  - lib/orange_data/version.rb
165
166
  - lib/orangedata.rb
166
167
  - orangedata.gemspec
168
+ - tmp/.keep
167
169
  homepage: https://github.com/Vasfed/orangedata
168
170
  licenses:
169
171
  - MIT