ezclient 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: a950272f16611080c183b2873c85c2b78d23d36c9d226d900b511f720b7647ba
4
+ data.tar.gz: a52ca73d8ff2abc0de9d8811c608394d52f537275951f4cbad5d7d52ceee3710
5
+ SHA512:
6
+ metadata.gz: d3e3bab868b24417979497778c55e08efaa861b155e45f174847749cebeff75b895a97b6e6d3a8c22fd04eb7cadedd92f4cb2cf5dee60378854f558e2678cc95
7
+ data.tar.gz: 68fc9ff7949547044d9aaf28ed11ec7e0477e5cbdf533e0bcfd7efc0b1894232f1d19c8c5c60dbc22c0523f8bc601a2619b48ceb7d608dfea943783014f96ca3
data/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ .rspec_status
10
+ Gemfile.lock
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,6 @@
1
+ inherit_gem:
2
+ rubocop-config-umbrellio: lib/rubocop.yml
3
+
4
+ AllCops:
5
+ DisplayCopNames: true
6
+ TargetRubyVersion: 2.5
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.5
5
+ - 2.4.2
6
+ - 2.5.0
7
+ before_install: gem install bundler -v 1.16.1
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+ gemspec
5
+
6
+ gem "http", github: "httprb/http"
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Yuri Smirnov
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.
data/README.md ADDED
@@ -0,0 +1,39 @@
1
+ # EzClient
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/ezclient`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'ezclient'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install ezclient
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ 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).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/umbrellio/ezclient.
36
+
37
+ ## License
38
+
39
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task default: :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "ezclient"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/ezclient.gemspec ADDED
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ lib = File.expand_path("../lib", __FILE__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require "ezclient/version"
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = "ezclient"
9
+ spec.version = EzClient::VERSION
10
+ spec.authors = ["Yuri Smirnov"]
11
+ spec.email = ["tycooon@yandex.ru"]
12
+
13
+ spec.summary = "An HTTP gem wrapper for easy persistent connections and more."
14
+ spec.homepage = "https://github.org/umbrellio/ezclient"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^spec/}) }
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.add_runtime_dependency "http", ">= 4.0.0dev"
21
+
22
+ spec.add_development_dependency "bundler"
23
+ spec.add_development_dependency "coveralls"
24
+ spec.add_development_dependency "pry"
25
+ spec.add_development_dependency "rake"
26
+ spec.add_development_dependency "rspec"
27
+ spec.add_development_dependency "rubocop-config-umbrellio"
28
+ spec.add_development_dependency "simplecov"
29
+ spec.add_development_dependency "webmock"
30
+ end
@@ -0,0 +1,67 @@
1
+ # frozen_string_literal: true
2
+
3
+ class EzClient::Request
4
+ DEFAULT_TIMEOUT = 15
5
+
6
+ attr_accessor :verb, :url, :options
7
+
8
+ def initialize(verb, url, options)
9
+ self.verb = verb.to_s.upcase
10
+ self.url = url
11
+ self.options = options
12
+ end
13
+
14
+ def perform
15
+ http_response = http_client.perform(http_request, http_client.default_options)
16
+
17
+ EzClient::Response.new(http_response).tap do |response|
18
+ on_complete.call(self, response, options[:metadata])
19
+ end
20
+ rescue => error
21
+ on_error.call(self, error, options[:metadata])
22
+ raise error
23
+ end
24
+
25
+ def api_auth!(*args)
26
+ # raise "ApiAuth gem is not loaded" unless defined?(ApiAuth)
27
+ ApiAuth.sign!(http_request, *args)
28
+ self
29
+ end
30
+
31
+ def body
32
+ http_request.body.source.rewind if http_request.body.source.respond_to?(:rewind)
33
+ body = +""
34
+ http_request.body.each { |chunk| body << chunk }
35
+ body
36
+ end
37
+
38
+ def headers
39
+ http_request.headers.to_h
40
+ end
41
+
42
+ private
43
+
44
+ def http_client
45
+ options.fetch(:client).timeout(timeout)
46
+ end
47
+
48
+ def timeout
49
+ options.fetch(:timeout, DEFAULT_TIMEOUT).to_f
50
+ end
51
+
52
+ def http_request
53
+ @http_request ||= http_client.build_request(verb, url, http_options)
54
+ end
55
+
56
+ def http_options
57
+ options.slice(:params, :form, :json, :body, :headers)
58
+ end
59
+
60
+ def on_complete
61
+ options[:on_complete] || proc {}
62
+ end
63
+
64
+ def on_error
65
+ options[:on_error] || proc {}
66
+ end
67
+ end
@@ -0,0 +1,42 @@
1
+ # frozen_string_literal: true
2
+
3
+ class EzClient::Response
4
+ attr_accessor :http_response
5
+
6
+ def initialize(http_response)
7
+ self.http_response = http_response
8
+ end
9
+
10
+ def body
11
+ http_response.body.to_s
12
+ end
13
+
14
+ def headers
15
+ http_response.headers
16
+ end
17
+
18
+ def code
19
+ http_response.code
20
+ end
21
+ alias status code
22
+
23
+ def ok?
24
+ code.between?(200, 299)
25
+ end
26
+
27
+ def redirect?
28
+ code.between?(300, 399)
29
+ end
30
+
31
+ def client_error?
32
+ code.between?(400, 499)
33
+ end
34
+
35
+ def server_error?
36
+ code.between?(500, 599)
37
+ end
38
+
39
+ def error?
40
+ client_error? || server_error?
41
+ end
42
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ class EzClient
4
+ VERSION = "0.1.0"
5
+ end
data/lib/ezclient.rb ADDED
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "http"
4
+ require "ezclient/version"
5
+ require "ezclient/request"
6
+ require "ezclient/response"
7
+
8
+ class EzClient
9
+ def initialize(on_complete: nil, on_error: nil)
10
+ self.clients = {}
11
+ self.on_complete = on_complete
12
+ self.on_error = on_error
13
+ end
14
+
15
+ def request(verb, url, **options)
16
+ keep_alive_timeout = options.delete(:keep_alive)
17
+
18
+ if keep_alive_timeout
19
+ client = persistent_client_for(url, timeout: keep_alive_timeout)
20
+ else
21
+ client = HTTP::Client.new
22
+ end
23
+
24
+ Request.new(verb, url, client: client, on_complete: on_complete, on_error: on_error, **options)
25
+ end
26
+
27
+ private
28
+
29
+ attr_accessor :clients, :on_complete, :on_error
30
+
31
+ def persistent_client_for(url, timeout: 600)
32
+ uri = HTTP::URI.parse(url)
33
+ clients[uri.origin] ||= HTTP.persistent(uri.origin, timeout: timeout)
34
+ end
35
+ end
metadata ADDED
@@ -0,0 +1,185 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ezclient
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Yuri Smirnov
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-02-14 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: http
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 4.0.0dev
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 4.0.0dev
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: coveralls
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: pry
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rspec
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rubocop-config-umbrellio
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: simplecov
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: webmock
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ description:
140
+ email:
141
+ - tycooon@yandex.ru
142
+ executables: []
143
+ extensions: []
144
+ extra_rdoc_files: []
145
+ files:
146
+ - ".gitignore"
147
+ - ".rspec"
148
+ - ".rubocop.yml"
149
+ - ".travis.yml"
150
+ - Gemfile
151
+ - LICENSE.txt
152
+ - README.md
153
+ - Rakefile
154
+ - bin/console
155
+ - bin/setup
156
+ - ezclient.gemspec
157
+ - lib/ezclient.rb
158
+ - lib/ezclient/request.rb
159
+ - lib/ezclient/response.rb
160
+ - lib/ezclient/version.rb
161
+ homepage: https://github.org/umbrellio/ezclient
162
+ licenses:
163
+ - MIT
164
+ metadata: {}
165
+ post_install_message:
166
+ rdoc_options: []
167
+ require_paths:
168
+ - lib
169
+ required_ruby_version: !ruby/object:Gem::Requirement
170
+ requirements:
171
+ - - ">="
172
+ - !ruby/object:Gem::Version
173
+ version: '0'
174
+ required_rubygems_version: !ruby/object:Gem::Requirement
175
+ requirements:
176
+ - - ">="
177
+ - !ruby/object:Gem::Version
178
+ version: '0'
179
+ requirements: []
180
+ rubyforge_project:
181
+ rubygems_version: 2.7.4
182
+ signing_key:
183
+ specification_version: 4
184
+ summary: An HTTP gem wrapper for easy persistent connections and more.
185
+ test_files: []