curlyrest 0.1.0

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: c362bdedaa3523b085c1c85a566d1b4cbaefbe84
4
+ data.tar.gz: 1156b2fa806226824c715ea4368a5a100fa6e385
5
+ SHA512:
6
+ metadata.gz: ec71c9ffebea0ffc2285e40832773bc6f03483ffb4ff1210a9573f55a467ea62ead8cd1895a8a15c8abaff826a7eedf5c7168862b90499bd78baab39988165d0
7
+ data.tar.gz: abb74de66b5520301ce42516f5daa0ca154173fda78eeefe8c1c68a6243c25284693a3482cab25ed91fb5341f333aa01ccb13c9ecd330d2669831da692aa5816
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ .byebug_history
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in curlyrest.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Keith Williams
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,41 @@
1
+ # Curlyrest
2
+
3
+ Welcome to curlyrest. In years of testing RESTful APIs, it was regularly beneficial to be able to substitute execution via curl for a request which normally would have been executed with RestClient. This might have been because observation of the exact request wasn't easy, or because RestClient had some unexplained restriction. Having a tool that could execute the request via curl and optionally expose the output, allowed observing the exact failing request. It was also possible to pass the curl request to a colleague without having them deal with environment, ruby, or data complications to be able to replicate a failure.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'curlyrest'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install curlyrest
20
+
21
+ ## Usage
22
+
23
+ Simply replace your use of require 'rest-client' with require 'curlyrest', and optionally add header :params of :use_curl with true or 'debug' which will cause the request to be executed with curl and response parsed to be compatable with rest-client.
24
+
25
+ ## Development
26
+
27
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
28
+
29
+ ## Limitations
30
+
31
+ Curlyrest works with basic requests, including responses with content-encoding: 'gzip'. I would not be suprised to find some more complicated requests that are not supported at this time.
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/keithrw54/curlyrest.
36
+
37
+
38
+ ## License
39
+
40
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
41
+
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'curlyrest/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "curlyrest"
8
+ spec.version = Curlyrest::VERSION
9
+ spec.authors = ["Keith Williams"]
10
+ spec.email = ["keithrw@comcast.net"]
11
+
12
+ spec.summary = 'gem extending rest-client, allowing use/debug of curl for request'
13
+ spec.description = 'gem extending rest-client, allowing use/debug of curl for request'
14
+ spec.homepage = "https://github.com/keithrw54/curlyrest.git"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_development_dependency "bundler", "~> 1.11"
23
+ spec.add_development_dependency "rake", "~> 10.0"
24
+ spec.add_development_dependency "rspec", "~> 3.0"
25
+ spec.add_development_dependency "rest-client", "~> 2.0"
26
+ spec.add_development_dependency "byebug", "~> 2.0"
27
+ end
@@ -0,0 +1,91 @@
1
+ require "curlyrest/version"
2
+ require 'rest-client'
3
+ require 'byebug'
4
+
5
+ module Curlyrest
6
+ class CurlResponse
7
+ include Net::HTTPHeader
8
+ attr_reader :code, :http_version, :message, :headers
9
+ attr_accessor :body, :inflate
10
+ def initialize(http_version, status, message)
11
+ @message = message
12
+ @http_version = http_version
13
+ @code = status
14
+ @body = ''
15
+ @inflate = Zlib::Inflate.new(32 + Zlib::MAX_WBITS)
16
+ initialize_http_header nil
17
+ end
18
+ def body=(value)
19
+ @body = value
20
+ end
21
+ def unzip_body(gzip)
22
+ @body = @inflate.inflate(gzip)
23
+ end
24
+ end
25
+
26
+ def parse_response(r)
27
+ t = :http_status
28
+ n = nil
29
+ body = ''
30
+ r.each_line do |l|
31
+ case t
32
+ when :http_status
33
+ rem = /^HTTP\/(\d+\.\d+) (\d+) (.+)$/.match(l)
34
+ n = CurlResponse.new(rem[1], rem[2], rem[3].chop)
35
+ t = :headers
36
+ when :headers
37
+ if /^[\r\n]+$/.match(l)
38
+ t = :body
39
+ else
40
+ rem = /^([\w-]+):\s(.*)/.match(l.chop)
41
+ n[rem[1]] = rem[2]
42
+ end
43
+ when :body
44
+ body << l
45
+ end
46
+ end
47
+ if n.to_hash['content-encoding'].include?('gzip')
48
+ n.unzip_body(body)
49
+ else
50
+ n.body = (body)
51
+ end
52
+ n
53
+ end
54
+
55
+ def curl_headers(headers)
56
+ ret_headers = ' '
57
+ headers.each{|k,v| ret_headers << "-H '#{k}: \"#{v}\"' "}
58
+ ret_headers
59
+ end
60
+
61
+ def transmit(uri, method, processed_headers, payload, &block)
62
+ curlyrest_option = processed_headers.delete('Use-Curl')
63
+ curl_line = "curl -isS -X #{method.upcase} #{curl_headers(processed_headers)}'#{uri}'"
64
+ puts curl_line if curlyrest_option == 'debug'
65
+ r = `#{curl_line}`
66
+ puts r if curlyrest_option == 'debug'
67
+ parse_response(r)
68
+ end
69
+ end
70
+
71
+ include Curlyrest
72
+
73
+ module RestClient
74
+ class Request
75
+ def execute & block
76
+ # With 2.0.0+, net/http accepts URI objects in requests and handles wrapping
77
+ # IPv6 addresses in [] for use in the Host request header.
78
+ case processed_headers['Use-Curl']
79
+ when 'debug', 'true'
80
+ r = Curlyrest.transmit(uri, method, processed_headers, payload, &block)
81
+ RestClient::Response.create(r.body, r, self)
82
+ else
83
+ transmit uri,
84
+ net_http_request_class(method).new(uri, processed_headers),
85
+ payload, & block
86
+ end
87
+ ensure
88
+ payload.close if payload
89
+ end
90
+ end
91
+ end
@@ -0,0 +1,3 @@
1
+ module Curlyrest
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,123 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: curlyrest
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Keith Williams
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-04-17 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.11'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.11'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rest-client
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '2.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '2.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: byebug
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '2.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '2.0'
83
+ description: gem extending rest-client, allowing use/debug of curl for request
84
+ email:
85
+ - keithrw@comcast.net
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - ".rspec"
92
+ - Gemfile
93
+ - LICENSE.txt
94
+ - README.md
95
+ - Rakefile
96
+ - curlyrest.gemspec
97
+ - lib/curlyrest.rb
98
+ - lib/curlyrest/version.rb
99
+ homepage: https://github.com/keithrw54/curlyrest.git
100
+ licenses:
101
+ - MIT
102
+ metadata: {}
103
+ post_install_message:
104
+ rdoc_options: []
105
+ require_paths:
106
+ - lib
107
+ required_ruby_version: !ruby/object:Gem::Requirement
108
+ requirements:
109
+ - - ">="
110
+ - !ruby/object:Gem::Version
111
+ version: '0'
112
+ required_rubygems_version: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - ">="
115
+ - !ruby/object:Gem::Version
116
+ version: '0'
117
+ requirements: []
118
+ rubyforge_project:
119
+ rubygems_version: 2.5.2.1
120
+ signing_key:
121
+ specification_version: 4
122
+ summary: gem extending rest-client, allowing use/debug of curl for request
123
+ test_files: []