rest_baby 0.2 → 0.3

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.
Files changed (50) hide show
  1. checksums.yaml +4 -4
  2. data/ChangeLog +6 -1
  3. data/Gemfile +3 -1
  4. data/cucumber.yml +1 -1
  5. data/doc/MockRestService.html +5 -13
  6. data/doc/RestBaby/Client.html +13 -13
  7. data/doc/RestBaby.html +1 -1
  8. data/doc/_index.html +4 -45
  9. data/doc/class_list.html +1 -1
  10. data/doc/css/style.css +1 -1
  11. data/doc/feature_list.html +39 -39
  12. data/doc/file.README.html +5 -5
  13. data/doc/frames.html +5 -7
  14. data/doc/index.html +5 -5
  15. data/doc/js/app.js +7 -2
  16. data/doc/method_list.html +20 -26
  17. data/doc/requirements/features/headers/header_options.html +45 -33
  18. data/doc/requirements/features/headers.html +5 -4
  19. data/doc/requirements/features/print_response.html +26 -138
  20. data/doc/requirements/features/rest_client.html +23 -23
  21. data/doc/requirements/features.html +9 -8
  22. data/doc/requirements/step_transformers.html +206 -222
  23. data/doc/requirements/tags/authentication.html +3 -3
  24. data/doc/requirements/tags/core.html +3 -3
  25. data/doc/requirements/tags/delete.html +3 -3
  26. data/doc/requirements/tags/extended.html +3 -3
  27. data/doc/requirements/tags/get.html +3 -3
  28. data/doc/requirements/tags/headers.html +3 -3
  29. data/doc/requirements/tags/post.html +3 -3
  30. data/doc/requirements/tags/print.html +3 -3
  31. data/doc/requirements/tags/put.html +3 -3
  32. data/doc/requirements/tags/wip.html +187 -0
  33. data/doc/requirements/tags.html +40 -39
  34. data/doc/requirements.html +10 -9
  35. data/doc/step_list.html +166 -210
  36. data/doc/stepdefinition_list.html +36 -36
  37. data/doc/tag_list.html +12 -6
  38. data/doc/top-level-namespace.html +5 -14
  39. data/features/headers/header_options.feature +9 -9
  40. data/features/print_response.feature +0 -25
  41. data/features/step_definitions/header_steps.rb +8 -3
  42. data/features/step_definitions/rest_client_steps.rb +1 -6
  43. data/features/support/env.rb +0 -1
  44. data/features/support/mock_rest_service.rb +1 -5
  45. data/lib/rest_baby/version.rb +1 -1
  46. data/lib/rest_baby.rb +143 -88
  47. data/rest_baby.gemspec +2 -2
  48. metadata +28 -30
  49. data/doc/PinchRestService.html +0 -349
  50. data/features/support/pinch_rest_service.rb +0 -21
data/lib/rest_baby.rb CHANGED
@@ -1,138 +1,193 @@
1
- =begin rdoc
2
-
3
- == Authors
4
-
5
- Dave McNulla
6
-
7
- =end
1
+ # rest_baby.rb
2
+ # @author Dave McNulla of ResMed
3
+ # Originated: 2011
4
+ # This library was written to simplify communications with rest services.
8
5
 
9
6
  require 'net/http'
10
7
  require 'net/https'
11
- require "rest_baby/version"
8
+ require 'json'
9
+ require 'nokogiri'
10
+ require 'rest_baby/version'
12
11
 
13
- ##
14
12
  # RestBaby is a small rest client. It encapsulates some of the details for creating and using the rest service.
13
+ # @private
15
14
  module RestBaby
16
15
  # Sends and receives data to a restful web service
17
16
  class Client
17
+ # The WebService Response from the last call
18
+ attr_reader :wsresponse
19
+ # The user (for authentication)
20
+ attr_reader :user
21
+ # The password for the user (for authentication)
22
+ attr_reader :password
18
23
 
19
- ##
20
24
  # Creates a new rest client
21
25
  #
22
- # * +url+ = url of the rest service
26
+ # @param url [String] url of the rest service
23
27
  # eg. http://myrestservice.com:80/time
24
- # * +headers+ = default headers to use.
28
+ # @param headers [Hash] default headers to use.
25
29
  # eg. '{ \"Content-Type\" => \"application/json\"}'
26
- # can be multiple headers
27
- def initialize(url, headers = {})
28
- @uri = URI.parse(url)
30
+ # Can be multiple headers separated by commas inside the brackets.
31
+ def initialize(url, headers = {}, user = nil, password = nil)
32
+ @url = url
29
33
  @headers = headers
30
- @user = nil
31
- @password = nil
34
+ @user = user
35
+ @password = password
36
+
37
+ if ENV["DEBUG_HTTP"].nil?
38
+ @verbose = true
39
+ else
40
+ @verbose = ENV["DEBUG_HTTP"].downcase=='true' ? true : false
41
+ end
32
42
  end
33
43
 
34
- ##
35
- # Modifies the headers by merging new headers with current headers.
44
+ # Adds user/password to the rest client
36
45
  #
37
- # * +headers+ = new headers to merge with current headers
38
- def set_headers(headers)
39
- @headers = @headers.merge(headers)
46
+ # @param user [String] user that has authorization
47
+ # @param password [String] authorized user's password
48
+ def set_auth(user, password)
49
+ @user = user
50
+ @password = password
40
51
  end
41
52
 
42
- ##
43
- # Modifies the authentication for the rest client
53
+ # Modifies the headers by merging new headers with current headers.
44
54
  #
45
- # * +user+ = authorized username
46
- # * +password+ = password for the user
47
- def set_auth(user, password)
48
- new_url = "#{@uri.scheme}://#{user}:#{password}@#{@uri.host}:#{@uri.port}#{@uri.path}"
49
- @uri = URI.parse(new_url)
55
+ # @param headers [Hash] new headers to merge with current headers
56
+ def set_headers(headers)
57
+ @headers = @headers.merge(headers)
50
58
  end
51
59
 
52
- ##
53
60
  # Basic web services Get command
54
61
  #
55
- # * +url+ = url to send the command to
56
- # * +headers+ = header parameters including authorization and Content-Type
57
- # The response from the rest server is returned
58
- def get(headers = {})
59
- return request(@uri, Net::HTTP::Get.new(@uri.request_uri), headers)
62
+ # @param path [String] Path of service to send the command to
63
+ # @param headers [String] header parameters including authorization and Content-Type
64
+ # @return The response from the rest server is returned
65
+ def get(headers = {}, path = '')
66
+ uri = URI.parse("#{@url}#{path}")
67
+ return request(uri, Net::HTTP::Get.new(uri.request_uri), nil, headers)
60
68
  end
61
-
62
- ##
69
+
63
70
  # Basic web services Post command
64
71
  #
65
- # * +url+ = url to send the command to
66
- # * +body+ = data to put in the body
67
- # * +headers+ = header parameters including authorization and Content-Type
68
- # The response from the rest server is returned
69
- def post(body = nil, headers = {})
70
- return request(@uri, Net::HTTP::Post.new(@uri.request_uri), body, headers)
72
+ # @param path [String] Path of service to send the command to
73
+ # @param body [Any type of string] Data to put in the body such as json or xml
74
+ # @param headers [Hash] header parameters including authorization and Content-Type
75
+ # @return The response from the rest server is returned
76
+ def post(body = nil, headers = {}, path = '')
77
+ uri = URI.parse("#{@url}#{path}")
78
+ return request(uri, Net::HTTP::Post.new(uri.request_uri), body, headers)
71
79
  end
72
-
73
- ##
80
+
74
81
  # Basic web services Delete command
75
82
  #
76
- # * +url+ = url to send the command to
77
- # * +headers+ = header parameters including authorization and Content-Type
78
- # The response from the rest server is returned
79
- def delete(headers = {})
80
- return request(@uri, Net::HTTP::Delete.new(@uri.request_uri), headers)
83
+ # @param path [String] Path of service to send the command to
84
+ # @param headers [Hash] header parameters including authorization and Content-Type
85
+ # @return The response from the rest server is returned
86
+ def delete(headers = {}, path = '')
87
+ uri = URI.parse("#{@url}#{path}")
88
+ return request(uri, Net::HTTP::Delete.new(uri.request_uri), headers)
81
89
  end
82
-
83
- ##
90
+
84
91
  # Basic web services Put command
85
92
  #
86
- # * +url+ = url to send the command to
87
- # * +body+ = data to put in the body
88
- # * +headers+ = header parameters including authorization and Content-Type
89
- # The response from the rest server is returned
90
- def put(body = nil, headers = {})
91
- return request(@uri, Net::HTTP::Put.new(@uri.request_uri), body, headers)
93
+ # @param path [String] Path of service to send the command to
94
+ # @param body [String] data to put in the body
95
+ # @param headers [Hash] header parameters including authorization and Content-Type
96
+ # @return Response from the rest server is returned
97
+ def put(body = nil, headers = {}, path = '')
98
+ uri = URI.parse("#{@url}#{path}")
99
+ return request(uri, Net::HTTP::Put.new(uri.request_uri), body, headers)
92
100
  end
93
101
 
94
- ##
102
+ # Get the code from the last call
103
+ #
104
+ # @return the http code such as 200, 301, 403, 404, or 500
105
+ def get_code
106
+ @wsresponse.code
107
+ end
108
+
109
+ # Set verbose on or off
110
+ #
111
+ # @param on [Boolean] True=verbose, False=Not verbose
112
+ def set_verbose(on = true)
113
+ @verbose = on
114
+ end
115
+
116
+ private
117
+
95
118
  # Sending the web services command
96
119
  #
97
- # * +url+ = url to send the command to
98
- # * +req+ = command to send as Net::HTTP:<command> class
99
- # * +body+ = data to put in the body
100
- # * +headers+ = header parameters including authorization and Content-Type
101
- # The response from the rest server is returned
102
- def request(uri, request, body = nil, headers)
120
+ # @param uri [URI] Uri to send the command to
121
+ # @param request [HTTP Request] command to send as Net::HTTP:<command> class; get, post, put, delete.
122
+ # @param body [String] data to put in the body
123
+ # @param headers [Hash] header parameters including authorization and Content-Type
124
+ # @return The response from the rest server is returned
125
+ def request(uri, request, body = nil, headers = {})
103
126
  @headers.merge(headers).each { |key, value| request[key] = value }
127
+ request.basic_auth(@user, @password) unless @user.nil?
104
128
  request.body = body unless body.nil?
105
- http = Net::HTTP.new(@uri.host, @uri.port)
106
- http.use_ssl = true if @uri.scheme == 'https'
129
+ http = Net::HTTP.new(uri.host, uri.port)
130
+ http.use_ssl = true if uri.scheme == 'https'
131
+ print_request(request, http, uri)
107
132
  begin
108
133
  @wsresponse = http.request(request)
109
- # puts print_last_response
134
+ print_response(@wsresponse)
110
135
  return @wsresponse
111
136
  rescue Timeout::Error => e
112
137
  raise e.message
113
138
  end
114
139
  end
115
140
 
116
- def get_code
117
- @wsresponse.code
118
- end
119
-
120
- ##
141
+ # Print the Request
142
+ def print_request(request, http, uri)
143
+ if @verbose
144
+ puts ">> REQUEST"
145
+ puts "> URL: #{uri.scheme}://#{uri.host}:#{uri.port}#{uri.path}"
146
+ puts "> Headers: "
147
+ request.each { |key, value| puts " > #{key}: #{value}" }
148
+ # puts "> Basic_Auth #{request.basic_auth}"
149
+ puts "> BODY = "
150
+ if request.body.nil?
151
+ puts "[Empty]"
152
+ elsif request['Content-Type']=='application/json'
153
+ jj JSON(request.body)
154
+ elsif request['Content-Type']=='text/csv'
155
+ puts request.body
156
+ elsif request['Content-Type']=='application/xml'
157
+ puts pretty_xml(request.body)
158
+ else
159
+ puts request.body
160
+ end
161
+ end
162
+ end
163
+
121
164
  # Pretty print the web services last response
122
- #
123
- # The nice looking output is returned as a string
124
- def print_last_response
125
- output = "CODE = #{@wsresponse.code}\n"
126
- output << "MESSAGE = #{@wsresponse.message}\n"
127
- @wsresponse.each { |key, value| output << "#{key} = #{value}\n"}
128
- begin
129
- output << "BODY = "
130
- output << "#{@wsresponse.body}"
131
- rescue
132
- output << "[Empty]"
133
- end
134
- return output
135
- end
136
-
165
+ def print_response(response)
166
+ if @verbose
167
+ puts "<< RESPONSE"
168
+ puts " < CODE = #{response.code}"
169
+ puts " < MESSAGE = #{response.message}"
170
+ response.each { |key, value| puts " < #{key} = #{value}\n"}
171
+ puts " < BODY = "
172
+ if response.header['Content-Type']=='application/json'
173
+ jj JSON(response.body)
174
+ elsif response.header['Content-Type']=='text/csv'
175
+ puts response.body
176
+ elsif response.header['Content-Type']=='application/xml'
177
+ puts pretty_xml(body)
178
+ elsif response.body.nil?
179
+ puts "[Empty]"
180
+ else
181
+ puts "#{response.body}\n<"
182
+ end
183
+ end
184
+ end
185
+
186
+ def pretty_xml(xml)
187
+ doc = Nokogiri.XML(xml) do |config|
188
+ config.default_xml.noblanks
189
+ end
190
+ return doc.to_xml(:indent => 2)
191
+ end
137
192
  end
138
193
  end
data/rest_baby.gemspec CHANGED
@@ -18,12 +18,12 @@ Gem::Specification.new do |spec|
18
18
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
19
  spec.require_paths = ["lib"]
20
20
 
21
+ spec.add_dependency "nokogiri"
22
+ spec.add_dependency "json", ">= 1.8.0"
21
23
  spec.add_development_dependency "bundler", "~> 1.3"
22
24
  spec.add_development_dependency "rake"
23
25
  spec.add_development_dependency "cucumber", ">= 1.3.5"
24
26
  spec.add_development_dependency "fig_newton", ">= 0.9"
25
- spec.add_development_dependency "json", ">= 1.8.0"
26
- spec.add_development_dependency "pinch_hitter", ">= 0.3"
27
27
  spec.add_development_dependency "rspec", ">= 2.14.1"
28
28
  spec.add_development_dependency "webmock", ">= 1.13.0"
29
29
 
metadata CHANGED
@@ -1,99 +1,99 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rest_baby
3
3
  version: !ruby/object:Gem::Version
4
- version: '0.2'
4
+ version: '0.3'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dave McNulla
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-08-10 00:00:00.000000000 Z
11
+ date: 2014-02-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: bundler
14
+ name: nokogiri
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - ~>
17
+ - - '>='
18
18
  - !ruby/object:Gem::Version
19
- version: '1.3'
20
- type: :development
19
+ version: '0'
20
+ type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - ~>
24
+ - - '>='
25
25
  - !ruby/object:Gem::Version
26
- version: '1.3'
26
+ version: '0'
27
27
  - !ruby/object:Gem::Dependency
28
- name: rake
28
+ name: json
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
31
  - - '>='
32
32
  - !ruby/object:Gem::Version
33
- version: '0'
34
- type: :development
33
+ version: 1.8.0
34
+ type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - '>='
39
39
  - !ruby/object:Gem::Version
40
- version: '0'
40
+ version: 1.8.0
41
41
  - !ruby/object:Gem::Dependency
42
- name: cucumber
42
+ name: bundler
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - '>='
45
+ - - ~>
46
46
  - !ruby/object:Gem::Version
47
- version: 1.3.5
47
+ version: '1.3'
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - '>='
52
+ - - ~>
53
53
  - !ruby/object:Gem::Version
54
- version: 1.3.5
54
+ version: '1.3'
55
55
  - !ruby/object:Gem::Dependency
56
- name: fig_newton
56
+ name: rake
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
59
  - - '>='
60
60
  - !ruby/object:Gem::Version
61
- version: '0.9'
61
+ version: '0'
62
62
  type: :development
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
66
  - - '>='
67
67
  - !ruby/object:Gem::Version
68
- version: '0.9'
68
+ version: '0'
69
69
  - !ruby/object:Gem::Dependency
70
- name: json
70
+ name: cucumber
71
71
  requirement: !ruby/object:Gem::Requirement
72
72
  requirements:
73
73
  - - '>='
74
74
  - !ruby/object:Gem::Version
75
- version: 1.8.0
75
+ version: 1.3.5
76
76
  type: :development
77
77
  prerelease: false
78
78
  version_requirements: !ruby/object:Gem::Requirement
79
79
  requirements:
80
80
  - - '>='
81
81
  - !ruby/object:Gem::Version
82
- version: 1.8.0
82
+ version: 1.3.5
83
83
  - !ruby/object:Gem::Dependency
84
- name: pinch_hitter
84
+ name: fig_newton
85
85
  requirement: !ruby/object:Gem::Requirement
86
86
  requirements:
87
87
  - - '>='
88
88
  - !ruby/object:Gem::Version
89
- version: '0.3'
89
+ version: '0.9'
90
90
  type: :development
91
91
  prerelease: false
92
92
  version_requirements: !ruby/object:Gem::Requirement
93
93
  requirements:
94
94
  - - '>='
95
95
  - !ruby/object:Gem::Version
96
- version: '0.3'
96
+ version: '0.9'
97
97
  - !ruby/object:Gem::Dependency
98
98
  name: rspec
99
99
  requirement: !ruby/object:Gem::Requirement
@@ -137,7 +137,6 @@ files:
137
137
  - config/environments/mock.yml
138
138
  - cucumber.yml
139
139
  - doc/MockRestService.html
140
- - doc/PinchRestService.html
141
140
  - doc/RestBaby.html
142
141
  - doc/RestBaby/Client.html
143
142
  - doc/_index.html
@@ -175,6 +174,7 @@ files:
175
174
  - doc/requirements/tags/post.html
176
175
  - doc/requirements/tags/print.html
177
176
  - doc/requirements/tags/put.html
177
+ - doc/requirements/tags/wip.html
178
178
  - doc/step_list.html
179
179
  - doc/stepdefinition_list.html
180
180
  - doc/tag_list.html
@@ -186,7 +186,6 @@ files:
186
186
  - features/step_definitions/rest_client_steps.rb
187
187
  - features/support/env.rb
188
188
  - features/support/mock_rest_service.rb
189
- - features/support/pinch_rest_service.rb
190
189
  - features/support/utility.rb
191
190
  - lib/rest_baby.rb
192
191
  - lib/rest_baby/version.rb
@@ -211,7 +210,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
211
210
  version: '0'
212
211
  requirements: []
213
212
  rubyforge_project:
214
- rubygems_version: 2.0.3
213
+ rubygems_version: 2.1.11
215
214
  signing_key:
216
215
  specification_version: 4
217
216
  summary: This is a small rest client developed for use in testing rest services.
@@ -223,6 +222,5 @@ test_files:
223
222
  - features/step_definitions/rest_client_steps.rb
224
223
  - features/support/env.rb
225
224
  - features/support/mock_rest_service.rb
226
- - features/support/pinch_rest_service.rb
227
225
  - features/support/utility.rb
228
226
  has_rdoc: