reg.api2 0.0.8 → 0.0.9

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.
data/lib/reg_api2/impl.rb CHANGED
@@ -75,6 +75,82 @@ module RegApi2
75
75
  # @!attribute [rw] pem_password
76
76
  # @return [String] X.509 certificate password (nil by default).
77
77
  attr_accessor :pem_password
78
+ # @!attribute [rw] dump_requests_to
79
+ # @return [String,Symbol,Lambda] Where to dump outgoing API request (nil by default).
80
+ #
81
+ # | Value | Dump to |
82
+ # | ----- | ------- |
83
+ # | nil | Nothing |
84
+ # | :stdout, "stdout" | $stdout |
85
+ # | :stderr, "stderr" | $stderr |
86
+ # | lambda | Calls this lambda with requested path and form hash |
87
+ attr_accessor :dump_requests_to
88
+ # @!attribute [rw] dump_responses_to
89
+ # @return [String,Symbol,Lambda] Where to dump incoming API response (nil by default).
90
+ #
91
+ # | Value | Dump to |
92
+ # | ----- | ------- |
93
+ # | nil | Nothing |
94
+ # | :stdout, "stdout" | $stdout |
95
+ # | :stderr, "stderr" | $stderr |
96
+ # | lambda | Calls this lambda with incoming parsed JSON data |
97
+ attr_accessor :dump_responses_to
98
+
99
+ private :dump_requests_to, :dump_responses_to
100
+
101
+ # Dumps outgoing API requests to given `to` or code block.
102
+ # @param [String,Symbol,Lambda] to Where to dump incoming API response (nil by default).
103
+ #
104
+ # | Value | Dump to |
105
+ # | ----- | ------- |
106
+ # | nil | Nothing |
107
+ # | :stdout, "stdout" | $stdout |
108
+ # | :stderr, "stderr" | $stderr |
109
+ # | lambda | Calls this lambda with requested path and form hash |
110
+ #
111
+ # @param [Code] code_block Code block to be executed on every API request.
112
+ # @yield [path, form] Request path and form to be sent.
113
+ # @return [NilClass] nil
114
+ # @example Dump outgoing API requests to code block
115
+ # RegApi2.dump_requests { |path, form| p path; p form }
116
+ def dump_requests(to = nil, &code_block)
117
+ if to
118
+ self.dump_requests_to= to
119
+ return nil
120
+ end
121
+ if block_given?
122
+ self.dump_requests_to= code_block
123
+ return nil
124
+ end
125
+ self.dump_requests_to= nil
126
+ end
127
+
128
+ # Dumps incoming API responses to given `to` or code block.
129
+ # @param [String,Symbol,Lambda] to Where to dump outgoing API response (nil by default).
130
+ #
131
+ # | Value | Dump to |
132
+ # | ----- | ------- |
133
+ # | nil | Nothing |
134
+ # | :stdout, "stdout" | $stdout |
135
+ # | :stderr, "stderr" | $stderr |
136
+ # | lambda | Calls this lambda with incoming parsed JSON data |
137
+ #
138
+ # @param [Code] code_block Code block to be executed on every API response.
139
+ # @yield [json] Response parsed JSON data to be handled.
140
+ # @return [NilClass] nil
141
+ # @example Dump incoming API responses to `$stdout`
142
+ # RegApi2.dump_responses :stdout
143
+ def dump_responses(to = nil, &code_block)
144
+ if to
145
+ self.dump_responses_to= to
146
+ return nil
147
+ end
148
+ if block_given?
149
+ self.dump_responses_to= code_block
150
+ return nil
151
+ end
152
+ self.dump_responses_to= nil
153
+ end
78
154
 
79
155
  # Default IO encoding
80
156
  DEFAULT_IO_ENCODING = 'utf-8'
@@ -144,12 +220,38 @@ module RegApi2
144
220
  # @param [Hash] form
145
221
  # @return void
146
222
  def form_to_be_sent(path, form)
223
+ case dump_requests_to
224
+ when :stderr, "stderr"
225
+ $stderr.puts "RegApi2.Request:\n#{path}\n#{form}"
226
+ when :stdout, "stdout"
227
+ $stdout.puts "RegApi2.Request:\n#{path}\n#{form}"
228
+ when Proc
229
+ dump_requests_to.call(path, form)
230
+ when nil
231
+ ;
232
+ else
233
+ raise ArgumentError.new( "Bad dump_requests_to field: #{dump_requests_to.inspect}" )
234
+ end
235
+ nil
147
236
  end
148
237
 
149
238
  # Placeholder to inspect got response.
150
239
  # @param [Net::HTTPResponse] response
151
240
  # @return void
152
241
  def got_response(response)
242
+ case dump_responses_to
243
+ when :stderr, "stderr"
244
+ $stderr.puts "RegApi2.Response:\n#{response}"
245
+ when :stdout, "stdout"
246
+ $stdout.puts "RegApi2.Response:\n#{response}"
247
+ when Proc
248
+ dump_responses_to.call(response)
249
+ when nil
250
+ ;
251
+ else
252
+ raise ArgumentError.new( "Bad dump_responses_to field: #{dump_responses_to.inspect}" )
253
+ end
254
+ nil
153
255
  end
154
256
 
155
257
  # Gets form data for POST request
@@ -188,6 +290,8 @@ module RegApi2
188
290
  raise NetError.new(res.body) unless res.code == '200'
189
291
 
190
292
  json = Yajl::Parser.parse(res.body)
293
+ got_response(json)
294
+
191
295
  raise ApiError.from_json(json) if json['result'] == 'error'
192
296
 
193
297
  res_contract = RegApi2::ResultContract.new(defopts)
@@ -212,7 +316,6 @@ module RegApi2
212
316
 
213
317
  req.set_form_data(form)
214
318
  res = http.request(req)
215
- got_response(res)
216
319
 
217
320
  handle_response(defopts, res)
218
321
  end
@@ -214,7 +214,7 @@ module RegApi2
214
214
  # | Field | Description |
215
215
  # | ----- | ----------- |
216
216
  # | services | A list of requested services. |
217
- # | bills | A list of IDs of invoices associated with this service. |
217
+ # | bills | A list of IDs of invoices associated with this service. May be nil. |
218
218
  #
219
219
  # @note Accessibility: partners
220
220
  # @note Support of service lists: yes
@@ -1,5 +1,5 @@
1
1
  # -*- encoding : utf-8 -*-
2
2
  module RegApi2
3
3
  # Gem version.
4
- VERSION = "0.0.8".freeze
4
+ VERSION = "0.0.9".freeze
5
5
  end
data/lib/reg_api2.rb CHANGED
@@ -33,6 +33,11 @@ require 'reg_api2/zone'
33
33
  # * {RegApi2.folder} API category implemented as {RegApi2::Folder} methods.
34
34
  # * {RegApi2.zone} API category implemented as {RegApi2::Zone} methods.
35
35
  #
36
+ # Provides dump hooks:
37
+ #
38
+ # * {RegApi2.dump_requests} to dump outgoing API request forms.
39
+ # * {RegApi2.dump_responses} to dump incoming API responses.
40
+ #
36
41
  # Please read {file:README.md} for API overview.
37
42
  #
38
43
  # @example List of services by specified identifiers
@@ -44,6 +49,8 @@ require 'reg_api2/zone'
44
49
  # { surprise: "surprise.ru" }
45
50
  # ])
46
51
  #
52
+ # @example Dump incoming API responses to `$stderr`
53
+ # RegApi2.dump_responses :stderr
47
54
 
48
55
  module RegApi2
49
56
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: reg.api2
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.8
4
+ version: 0.0.9
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-10-07 00:00:00.000000000 Z
12
+ date: 2013-10-08 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: yajl-ruby
@@ -252,7 +252,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
252
252
  version: '0'
253
253
  segments:
254
254
  - 0
255
- hash: 1698904277702567800
255
+ hash: 890297353583224213
256
256
  required_rubygems_version: !ruby/object:Gem::Requirement
257
257
  none: false
258
258
  requirements:
@@ -261,7 +261,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
261
261
  version: '0'
262
262
  segments:
263
263
  - 0
264
- hash: 1698904277702567800
264
+ hash: 890297353583224213
265
265
  requirements: []
266
266
  rubyforge_project:
267
267
  rubygems_version: 1.8.24