ruby-development-toolbox 1.3.2 → 1.4.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
  SHA1:
3
- metadata.gz: 910138d99d7862d7ce2725e50334afc5bbc51565
4
- data.tar.gz: 6fe90a88dd79fb5e49ec65b9ffca2836eb3c811d
3
+ metadata.gz: 5617c949afadb92dd7a3acb8c5505e5eb02c8a52
4
+ data.tar.gz: 29d5d156a1733866e01b40000c3ea4516c4d31ea
5
5
  SHA512:
6
- metadata.gz: c2a6894e4d066e35a9fb3963e2b8b03d9f00c7ed79da652f4e3090760d9165776dd39623263dd46619e1588bd823899a04c50214fd48aebdf1c6f1a8a53c2b32
7
- data.tar.gz: f91363877803840ded559ef939f2d88298c1ce65e81ceaee7152796ed7c7206e64342f9ab13c2e2e7a0960297bf065410732e8cf0783a6f0091502bafb04008d
6
+ metadata.gz: 192122294fbc818dd832ad27cd653b17e723a076a1971bace2bccb520fbb9f70834415a3137bd2b53d67ed96b44468a8357282d32112291739d62f286bfddb0a
7
+ data.tar.gz: 8ddbed2b82ccea569b2373364e33010dae8d6c6732af39a6f68564e0d6fb6ef6b6118620360eed032e13bcab04ed95c3a7bd28c4caddb7266923f26b31e1800c
data/Gemfile CHANGED
@@ -9,7 +9,10 @@ group :development do
9
9
  gem "shoulda", ">= 0"
10
10
  gem "rdoc", "~> 3.12"
11
11
  gem "bundler", "~> 1.0"
12
- gem "jeweler", "~> 2.0.1"
12
+
13
+ gem "minitest", "~> 4.0"
14
+ gem "jeweler", "= 2.0.1"
15
+
13
16
  gem "simplecov", ">= 0"
14
17
  gem "yard", ">= 0.8.7.3"
15
18
  end
@@ -76,7 +76,8 @@ PLATFORMS
76
76
 
77
77
  DEPENDENCIES
78
78
  bundler (~> 1.0)
79
- jeweler (~> 2.0.1)
79
+ jeweler (= 2.0.1)
80
+ minitest (~> 4.0)
80
81
  rdoc (~> 3.12)
81
82
  shoulda
82
83
  simplecov
data/README.md CHANGED
@@ -38,6 +38,66 @@ This is to support operations like the following:
38
38
 
39
39
  The projects homepage can be found [here](https://github.com/gradeawarrior/ruby-development-toolbox). You can also refer to the [Rubydoc YARD Server](http://rubydoc.info/github/gradeawarrior/ruby-development-toolbox/frames)
40
40
 
41
+ In summary, the following modules are available currently in this gem:
42
+
43
+ ### 1. toolbox/array
44
+
45
+ This is to implement equivalence check for arrays. In the context of arrays, we use =~ as a means of checking if array1 is contained in array2; or if object is contained in array1
46
+
47
+ ### 2. toolbox/boolean
48
+
49
+ The main application of the Boolean module is to support reading boolean values from a String (e.g. while reading a configuration value) and having the ability to convert it back to a boolean true/false for easier evaluation in your Ruby code
50
+
51
+ ### 3. toolbox/gem_specification
52
+
53
+ Extends the functionality of a Gem::Specification to be able to retrieve the latest version of gems currently on your system.
54
+
55
+ ### 4. toolbox/hash_diff
56
+
57
+ Extends the functionality of a Hash to be able to perform (i) diff and (ii) similarity operations. For implementation details, see the Hash class for the extended functions
58
+
59
+ ### 5. toolbox/http
60
+
61
+ A simple testing client based on the Perl version found in: [QA-Perl-Lib.git](https://github.com/gradeawarrior/QA-Perl-Lib/blob/master/QA/Test/WebService.pm)
62
+
63
+ An example usage would be the following:
64
+
65
+ require 'toolbox/http'
66
+
67
+ response = Toolbox::Http.request(:method => 'GET', :url => 'http://www.google.com')
68
+ if response.code == 200.to_s
69
+ puts "Yay! It worked!"
70
+ else
71
+ puts "Boo! Something broke!" unless response.code == 200.to_s
72
+ end
73
+
74
+ ### 6. toolbox/integer
75
+
76
+ Extends the functionality of String to support checking if a String can be converted to an Integer
77
+
78
+ require 'toolbox/integer'
79
+
80
+ ['-1', '0', '1', '1.0', 'one', '1 too many'].each do |test|
81
+ puts "This can be converted to an Integer" if test.is_i?
82
+ puts "This cannot be converted to an Integer" unless test.is_i?
83
+ end
84
+
85
+ ### 7. toolbox/json
86
+
87
+ Extends the functionality of String to support pretty_printing json. This is really a natural extension of the basic String type to support JSON.pretty_generate(string).
88
+
89
+ ### 8. toolbox/uuid
90
+
91
+ A generic UUID class:
92
+
93
+ $ irb
94
+ require 'toolbox/uuid'
95
+ => true
96
+ UUID.generate
97
+ => "0a391631-22ba-40ea-af2e-65a64de4a42b"
98
+ UUID.generate
99
+ => "092516ba-a2f4-45cf-9e1d-c5e63342aaa4"
100
+
41
101
  # Development
42
102
 
43
103
  The project is built by [jeweler](https://github.com/technicalpickles/jeweler). See the project's page for more details about how to manage this gem. However, I will list out quick guidance on a typical release.
data/Rakefile CHANGED
@@ -2,6 +2,9 @@
2
2
 
3
3
  require 'rubygems'
4
4
  require 'bundler'
5
+ require 'rake'
6
+ require 'jeweler'
7
+
5
8
  begin
6
9
  Bundler.setup(:default, :development)
7
10
  rescue Bundler::BundlerError => e
@@ -9,9 +12,7 @@ rescue Bundler::BundlerError => e
9
12
  $stderr.puts "Run `bundle install` to install missing gems"
10
13
  exit e.status_code
11
14
  end
12
- require 'rake'
13
15
 
14
- require 'jeweler'
15
16
  Jeweler::Tasks.new do |gem|
16
17
  # gem is a Gem::Specification... see http://guides.rubygems.org/specification-reference/ for more options
17
18
  gem.name = "ruby-development-toolbox"
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.3.2
1
+ 1.4.0
@@ -23,17 +23,17 @@ module Toolbox
23
23
  # we use =~ as a means of checking if array1 is contained in array2; or if
24
24
  # object is contained in array1
25
25
  #
26
- # sample_array = [1, 2, 3]
27
- # puts sample_array =~ 3 ## This prints true
28
- # puts sample_array =~ 4 ## This prints false
26
+ # sample_array = [1, 2, 3]
27
+ # puts sample_array =~ 3 ## This prints true
28
+ # puts sample_array =~ 4 ## This prints false
29
29
  #
30
30
  # The other way is the following:
31
31
  #
32
- # sample_array1 = [1, 2, 3]
33
- # sample_array2 = [1, 2, 3, 4]
34
- #
35
- # puts sample_array2 =~ sample_array1 ## This prints true, because everything in array1 is in array2
36
- # puts sample_array1 =~ sample_array2 ## This prints false, because not everything in array2 is in array1
32
+ # sample_array1 = [1, 2, 3]
33
+ # sample_array2 = [1, 2, 3, 4]
34
+ #
35
+ # puts sample_array2 =~ sample_array1 ## This prints true, because everything in array1 is in array2
36
+ # puts sample_array1 =~ sample_array2 ## This prints false, because not everything in array2 is in array1
37
37
  #
38
38
  module Array
39
39
  end
@@ -0,0 +1,208 @@
1
+ require 'net/http'
2
+ require 'toolbox/boolean'
3
+ require 'toolbox/json'
4
+ require 'uri'
5
+
6
+ begin
7
+ $SSL_AVAILABLE = true
8
+ require 'net/https'
9
+ rescue Exception
10
+ $SSL_AVAILABLE = false
11
+ end
12
+
13
+ module Toolbox
14
+
15
+ ##
16
+ # A simple testing client based on the Perl version found in:
17
+ #
18
+ # https://github.com/gradeawarrior/QA-Perl-Lib/blob/master/QA/Test/WebService.pm
19
+ #
20
+ # An example usage would be the following:
21
+ #
22
+ # require 'toolbox/http'
23
+ #
24
+ # response = Toolbox::Http.request(:method => 'GET', :url => 'http://www.google.com')
25
+ # if response.code == 200.to_s
26
+ # puts "Yay! It worked!"
27
+ # else
28
+ # puts "Boo! Something broke!" unless response.code == 200.to_s
29
+ # end
30
+ #
31
+ class Http
32
+
33
+ ##
34
+ # Generate and display curl request
35
+ #
36
+ # == Parameters
37
+ # uri : The URI you are performing the request on
38
+ # request : The Net::HTTP Request object
39
+ #
40
+ # == Returns
41
+ # curl_request : String representing the curl request
42
+ #
43
+ def self.generate_curl_request(uri, request)
44
+ curl_request_str = "curl -v -X #{request.method} \"#{uri.scheme}://#{uri.host}:#{uri.port}#{request.path}\""
45
+
46
+ # Generate headers
47
+ request.each_header do |name, value|
48
+ curl_request_str += " -H '#{name}: #{value}'" if (name !~ /user-agent/i && name !~ /accept/i)
49
+ end
50
+
51
+ curl_request_str += " -k" if (uri.scheme.to_s == "https")
52
+ curl_request_str += " -d 'YOUR_REQUEST_BODY'" if state.nil? && state.get(:curl_file).nil?
53
+ curl_request_str += " -d @#{state.get(:curl_file)}" if !state.nil? && !state.get(:curl_file).nil?
54
+ curl_request_str
55
+ end
56
+
57
+ ##
58
+ # Show details about request
59
+ #
60
+ # == Parameters
61
+ # uri : The URI you are performing the request on
62
+ # request : The Net::HTTP Request object
63
+ # disable_print_body : Disables printing body
64
+ #
65
+ def self.show_req(uri, request, disable_print_body=false)
66
+
67
+ ## Print verbose output from RESTest
68
+ puts "$ #{generate_curl_request(uri, request)}"
69
+ puts '---[ Request ]----------------------------------------------------'
70
+ puts "URI: #{uri.scheme}://#{uri.host}:#{uri.port}"
71
+ puts "#{request.method} #{request.path}"
72
+
73
+ request.each_header { |name, value| puts "#{name}: #{value}" }
74
+ if disable_print_body
75
+ puts "\n<!-- Disabled print body for Content-Type: #{request.content_type}-->\n"
76
+ else
77
+ puts "\n#{request.body}\n"
78
+ end
79
+ puts '------------------------------------------------------------------'
80
+
81
+ end
82
+
83
+ ##
84
+ # Show details about response received
85
+ #
86
+ # == Parameters
87
+ # response : The Net::HTTPResponse object
88
+ # disable_print_body : Disables printing body
89
+ #
90
+ def self.show_response(response, disable_print_body=false)
91
+
92
+ puts '---[ Response ]---------------------------------------------------'
93
+ if response
94
+ puts "#{response.code} #{response.message}"
95
+ if (response.code.to_i > 0)
96
+ response.each_header { |name, value| puts "#{name}: #{value}" }
97
+ end
98
+ if disable_print_body
99
+ puts "\n<!-- Disabled print body for Content-Type: #{request.content_type}-->\n"
100
+ else
101
+ puts "\n#{response.body}\n"
102
+ end
103
+ else
104
+ puts 'response object is nil!'
105
+ end
106
+ puts '------------------------------------------------------------------'
107
+
108
+ end
109
+
110
+ ##
111
+ # A simple testing client based on the Perl version found in:
112
+ #
113
+ # https://github.com/gradeawarrior/QA-Perl-Lib/blob/master/QA/Test/WebService.pm
114
+ #
115
+ # == Parameters
116
+ # :method => (Required) Sets the request method to perform. The valid methods are GET, POST, PUT, & DELETE
117
+ # :url => (Required) This should be set to the exact service API (e.g. 'http://www.google.com'). Port,
118
+ # path, and query parameters should also be specified. NOTE: If you specified the
119
+ # :request option, then the URI path and query parameters will be configured there.
120
+ # :request => (Optional) Default: nil - You can optionally setup your Net::HTTP::[Get|Post|Put|Delete]
121
+ # operation here. Other request could also be specified here if
122
+ # not supported via :method parameter.
123
+ # :timeout => (Optional) Default: 60 - This is the default request timeout
124
+ # :basic_auth => (Optional) Default: nil - This should be a hash containing 'user' and 'password' keys
125
+ # :headers => (Optional) Default: {} - A Hash containing all the Request headers that need to be set
126
+ # :request_body => (Optional) Default: nil - The request body for PUT and POST requests
127
+ # :content_type => (Optional) Default: nil - Sets the 'Content-Type' header
128
+ # :debug => (Optional) Default: false - Enables debug output
129
+ # :disable_print_body => (Optional) Default: false - This will hide the request/response body output
130
+ #
131
+ # == Returns
132
+ #
133
+ # A Net::HTTPResponse object is returned. This is the standard object returned from Net::HTTP
134
+ #
135
+ # == Throws (Exceptions)
136
+ #
137
+ # Any of the standard Net::HTTP::Exceptions could be returned if there is an error connecting
138
+ # to the downstream server.
139
+ #
140
+ # Additionally, The method could return:
141
+ # - ArgumentError
142
+ # - URI parse error
143
+ #
144
+ def self.request(*args)
145
+
146
+ args = args.nil? || args.empty? ? {} : args.pop
147
+ args = {} unless args.is_a?(::Hash)
148
+ raise ArgumentError, 'method is required!' if args[:method].nil? && args[:request].nil?
149
+ raise ArgumentError, 'url is required!' if args[:url].nil?
150
+
151
+ ## Options and defaults
152
+ request = args[:request]
153
+ args[:method] = request.method if request
154
+ args[:timeout] = args[:timeout] ? args[:timeout] : 60
155
+ args[:headers] = args[:headers] ? args[:headers] : {}
156
+ args[:headers]['Content-Type'] = args[:content_type] if args[:content_type]
157
+ args[:debug] = args[:debug].nil? ? false : args[:debug].to_bool
158
+ args[:disable_print_body] = args[:disable_print_body].nil? ? false : args[:disable_print_body].to_bool
159
+
160
+ puts "Requesting: #{args[:method]} #{uri.to_s}" if args[:debug]
161
+
162
+ ## Set the request based on the URL
163
+ if args[:url]
164
+ uri = args[:url].is_a?(::URI) ? args[:url] : URI(args[:url])
165
+ request_path = "#{uri.path}#{uri.query || ''}"
166
+ request_path = request_path.empty? ? '/' : request_path
167
+
168
+ if args[:method]
169
+ request = case args[:method]
170
+ when 'GET'
171
+ Net::HTTP::Get.new(request_path, args[:headers])
172
+ when 'POST'
173
+ req = Net::HTTP::Post.new(request_path, args[:headers])
174
+ req.body = args[:request_body] if args[:request_body]
175
+ req
176
+ when 'PUT'
177
+ req = Net::HTTP::Put.new(request_path, args[:headers])
178
+ req.body = args[:request_body] if args[:request_body]
179
+ req
180
+ when 'DELETE'
181
+ Net::HTTP::Delete.new(request_path, args[:headers])
182
+ else
183
+ raise ArgumentError, "Invalid method #{args[:method]}!"
184
+ end
185
+ end
186
+ end
187
+
188
+ ## Set Auth
189
+ req.basic_auth(args[:basic_auth]['user'], args[:basic_auth]['password']) if args[:basic_auth]
190
+
191
+ http = Net::HTTP.new(uri.host, uri.port)
192
+ http.read_timeout = args[:timeout]
193
+
194
+ if uri.scheme == 'https'
195
+ raise RuntimeError, 'SSL required but not available!' unless $SSL_AVAILABLE
196
+ http.use_ssl = true
197
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE
198
+ end
199
+
200
+ show_req(uri, request, state) if args[:debug]
201
+ response = http.request(request)
202
+ show_response(response) if args[:debug]
203
+ response
204
+
205
+ end
206
+
207
+ end
208
+ end
@@ -2,16 +2,16 @@
2
2
  # DO NOT EDIT THIS FILE DIRECTLY
3
3
  # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
4
  # -*- encoding: utf-8 -*-
5
- # stub: ruby-development-toolbox 1.3.2 ruby lib
5
+ # stub: ruby-development-toolbox 1.4.0 ruby lib
6
6
 
7
7
  Gem::Specification.new do |s|
8
8
  s.name = "ruby-development-toolbox"
9
- s.version = "1.3.2"
9
+ s.version = "1.4.0"
10
10
 
11
11
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
12
12
  s.require_paths = ["lib"]
13
13
  s.authors = ["Peter Salas"]
14
- s.date = "2014-12-17"
14
+ s.date = "2015-02-04"
15
15
  s.description = "A collection of useful utilities and libraries for Ruby development (not Rails)"
16
16
  s.email = "psalas+github@gmail.com"
17
17
  s.extra_rdoc_files = [
@@ -33,6 +33,7 @@ Gem::Specification.new do |s|
33
33
  "lib/toolbox/boolean.rb",
34
34
  "lib/toolbox/gem_specification.rb",
35
35
  "lib/toolbox/hash_diff.rb",
36
+ "lib/toolbox/http.rb",
36
37
  "lib/toolbox/integer.rb",
37
38
  "lib/toolbox/json.rb",
38
39
  "lib/toolbox/uuid.rb",
@@ -42,11 +43,12 @@ Gem::Specification.new do |s|
42
43
  "test/representations/person.rb",
43
44
  "test/test_toolbox-integer.rb",
44
45
  "test/test_toolbox_array.rb",
46
+ "test/test_toolbox_http.rb",
45
47
  "test/test_toolbox_json.rb"
46
48
  ]
47
49
  s.homepage = "http://github.com/gradeawarrior/ruby-development-toolbox"
48
50
  s.licenses = ["MIT"]
49
- s.rubygems_version = "2.2.2"
51
+ s.rubygems_version = "2.4.3"
50
52
  s.summary = "Useful Ruby Development Toolbox"
51
53
 
52
54
  if s.respond_to? :specification_version then
@@ -56,14 +58,16 @@ Gem::Specification.new do |s|
56
58
  s.add_development_dependency(%q<shoulda>, [">= 0"])
57
59
  s.add_development_dependency(%q<rdoc>, ["~> 3.12"])
58
60
  s.add_development_dependency(%q<bundler>, ["~> 1.0"])
59
- s.add_development_dependency(%q<jeweler>, ["~> 2.0.1"])
61
+ s.add_development_dependency(%q<minitest>, ["~> 4.0"])
62
+ s.add_development_dependency(%q<jeweler>, ["= 2.0.1"])
60
63
  s.add_development_dependency(%q<simplecov>, [">= 0"])
61
64
  s.add_development_dependency(%q<yard>, [">= 0.8.7.3"])
62
65
  else
63
66
  s.add_dependency(%q<shoulda>, [">= 0"])
64
67
  s.add_dependency(%q<rdoc>, ["~> 3.12"])
65
68
  s.add_dependency(%q<bundler>, ["~> 1.0"])
66
- s.add_dependency(%q<jeweler>, ["~> 2.0.1"])
69
+ s.add_dependency(%q<minitest>, ["~> 4.0"])
70
+ s.add_dependency(%q<jeweler>, ["= 2.0.1"])
67
71
  s.add_dependency(%q<simplecov>, [">= 0"])
68
72
  s.add_dependency(%q<yard>, [">= 0.8.7.3"])
69
73
  end
@@ -71,7 +75,8 @@ Gem::Specification.new do |s|
71
75
  s.add_dependency(%q<shoulda>, [">= 0"])
72
76
  s.add_dependency(%q<rdoc>, ["~> 3.12"])
73
77
  s.add_dependency(%q<bundler>, ["~> 1.0"])
74
- s.add_dependency(%q<jeweler>, ["~> 2.0.1"])
78
+ s.add_dependency(%q<minitest>, ["~> 4.0"])
79
+ s.add_dependency(%q<jeweler>, ["= 2.0.1"])
75
80
  s.add_dependency(%q<simplecov>, [">= 0"])
76
81
  s.add_dependency(%q<yard>, [">= 0.8.7.3"])
77
82
  end
@@ -0,0 +1,18 @@
1
+ require 'helper'
2
+ require 'toolbox/http'
3
+ require 'net/http'
4
+
5
+ class TestToolboxHttp < Test::Unit::TestCase
6
+
7
+ should 'be able to make a simple request to google' do
8
+ response = Toolbox::Http.request(:method => 'GET', :url => 'http://www.google.com')
9
+ assert_equal(200.to_s, response.code)
10
+ end
11
+
12
+ should 'be able to make a simple request to google using Net::Http::Get Request' do
13
+ req = Net::HTTP::Get.new("/", {})
14
+ response = Toolbox::Http.request(:url => 'http://www.google.com', :request => req)
15
+ assert_equal(200.to_s, response.code)
16
+ end
17
+
18
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby-development-toolbox
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.2
4
+ version: 1.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Peter Salas
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-12-17 00:00:00.000000000 Z
11
+ date: 2015-02-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: shoulda
@@ -53,17 +53,31 @@ dependencies:
53
53
  - !ruby/object:Gem::Version
54
54
  version: '1.0'
55
55
  - !ruby/object:Gem::Dependency
56
- name: jeweler
56
+ name: minitest
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
59
  - - ~>
60
60
  - !ruby/object:Gem::Version
61
- version: 2.0.1
61
+ version: '4.0'
62
62
  type: :development
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
66
  - - ~>
67
+ - !ruby/object:Gem::Version
68
+ version: '4.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: jeweler
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '='
74
+ - !ruby/object:Gem::Version
75
+ version: 2.0.1
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '='
67
81
  - !ruby/object:Gem::Version
68
82
  version: 2.0.1
69
83
  - !ruby/object:Gem::Dependency
@@ -117,6 +131,7 @@ files:
117
131
  - lib/toolbox/boolean.rb
118
132
  - lib/toolbox/gem_specification.rb
119
133
  - lib/toolbox/hash_diff.rb
134
+ - lib/toolbox/http.rb
120
135
  - lib/toolbox/integer.rb
121
136
  - lib/toolbox/json.rb
122
137
  - lib/toolbox/uuid.rb
@@ -126,6 +141,7 @@ files:
126
141
  - test/representations/person.rb
127
142
  - test/test_toolbox-integer.rb
128
143
  - test/test_toolbox_array.rb
144
+ - test/test_toolbox_http.rb
129
145
  - test/test_toolbox_json.rb
130
146
  homepage: http://github.com/gradeawarrior/ruby-development-toolbox
131
147
  licenses:
@@ -147,7 +163,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
147
163
  version: '0'
148
164
  requirements: []
149
165
  rubyforge_project:
150
- rubygems_version: 2.2.2
166
+ rubygems_version: 2.4.3
151
167
  signing_key:
152
168
  specification_version: 4
153
169
  summary: Useful Ruby Development Toolbox