network-client 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 30beca79e944d3f7f1d9a042d502593a24cd28dc
4
+ data.tar.gz: 43176302a9f186c38ee982e962085812c324e749
5
+ SHA512:
6
+ metadata.gz: 1e92a959db3b863d6d196551a6f3497fe0db2903c643354b059009d6cbb25a7996a2531f7af2b4c2fd502286b5546f64883b29ad63b2d2f4ae659e0018cc565f
7
+ data.tar.gz: 0c6d55ca68c2c5cfe8a577c2d730ab0b414776930908df0b9a7982d816f6020a88c9178fcf3c84da4b011dc4cfcfa600e7e6a9d25cd2576f2ce3ef3cfb7da80a
@@ -0,0 +1,55 @@
1
+ # Created by https://www.gitignore.io/api/ruby
2
+
3
+ ### Ruby ###
4
+ *.gem
5
+ *.rbc
6
+ /.config
7
+ /coverage/
8
+ /InstalledFiles
9
+ /pkg/
10
+ /spec/reports/
11
+ /spec/examples.txt
12
+ /test/tmp/
13
+ /test/version_tmp/
14
+ /tmp/
15
+
16
+ # Used by dotenv library to load environment variables.
17
+ # .env
18
+
19
+ ## Specific to RubyMotion:
20
+ .dat*
21
+ .repl_history
22
+ build/
23
+ *.bridgesupport
24
+ build-iPhoneOS/
25
+ build-iPhoneSimulator/
26
+
27
+ ## Specific to RubyMotion (use of CocoaPods):
28
+ #
29
+ # We recommend against adding the Pods directory to your .gitignore. However
30
+ # you should judge for yourself, the pros and cons are mentioned at:
31
+ # https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control
32
+ #
33
+ # vendor/Pods/
34
+
35
+ ## Documentation cache and generated files:
36
+ /.yardoc/
37
+ /_yardoc/
38
+ /doc/
39
+ /rdoc/
40
+
41
+ ## Environment normalization:
42
+ /.bundle/
43
+ /vendor/bundle
44
+ /lib/bundler/man/
45
+
46
+ # for a library or gem, you might want to ignore these files since the code is
47
+ # intended to run in multiple environments; otherwise, check them in:
48
+ # Gemfile.lock
49
+ # .ruby-version
50
+ # .ruby-gemset
51
+
52
+ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
53
+ .rvmrc
54
+
55
+ # End of https://www.gitignore.io/api/ruby
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --color
2
+ --order random
3
+ --require spec_helper
@@ -0,0 +1 @@
1
+ network-client
@@ -0,0 +1 @@
1
+ 2.3.1
@@ -0,0 +1,10 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.2.0
5
+ - 2.2.4
6
+ - 2.3.1
7
+ - 2.4.0
8
+ - 2.4.1
9
+ cache: bundler
10
+ before_install: gem install bundler
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify dependencies in network-client.gemspec
4
+ gemspec
@@ -0,0 +1,60 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ network-client (1.0.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ activesupport (5.1.1)
10
+ concurrent-ruby (~> 1.0, >= 1.0.2)
11
+ i18n (~> 0.7)
12
+ minitest (~> 5.1)
13
+ tzinfo (~> 1.1)
14
+ codeclimate-test-reporter (1.0.8)
15
+ simplecov (<= 0.13)
16
+ concurrent-ruby (1.0.5)
17
+ diff-lcs (1.3)
18
+ docile (1.1.5)
19
+ factory_girl (4.8.0)
20
+ activesupport (>= 3.0.0)
21
+ i18n (0.8.1)
22
+ json (2.1.0)
23
+ minitest (5.10.2)
24
+ rake (10.5.0)
25
+ rspec (3.6.0)
26
+ rspec-core (~> 3.6.0)
27
+ rspec-expectations (~> 3.6.0)
28
+ rspec-mocks (~> 3.6.0)
29
+ rspec-core (3.6.0)
30
+ rspec-support (~> 3.6.0)
31
+ rspec-expectations (3.6.0)
32
+ diff-lcs (>= 1.2.0, < 2.0)
33
+ rspec-support (~> 3.6.0)
34
+ rspec-mocks (3.6.0)
35
+ diff-lcs (>= 1.2.0, < 2.0)
36
+ rspec-support (~> 3.6.0)
37
+ rspec-support (3.6.0)
38
+ simplecov (0.13.0)
39
+ docile (~> 1.1.0)
40
+ json (>= 1.8, < 3)
41
+ simplecov-html (~> 0.10.0)
42
+ simplecov-html (0.10.0)
43
+ thread_safe (0.3.6)
44
+ tzinfo (1.2.3)
45
+ thread_safe (~> 0.1)
46
+
47
+ PLATFORMS
48
+ ruby
49
+
50
+ DEPENDENCIES
51
+ bundler (~> 1.14)
52
+ codeclimate-test-reporter (~> 1.0.0)
53
+ factory_girl (~> 4.5)
54
+ network-client!
55
+ rake (~> 10.0)
56
+ rspec (~> 3.6)
57
+ simplecov
58
+
59
+ BUNDLED WITH
60
+ 1.14.6
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Abdullah Barrak (abarrak)
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,37 @@
1
+ # Network Client
2
+ [![Gem Version](https://badge.fury.io/rb/network-client.svg)](https://rubygems.org/gems/network-client)
3
+ [![Build Status](https://travis-ci.org/abarrak/network-client.svg?branch=master)](https://travis-ci.org/abarrak/network-client)
4
+
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ ```ruby
11
+ gem 'network-client'
12
+ ```
13
+
14
+ And then execute:
15
+
16
+ ```sh
17
+ $ bundle
18
+ ```
19
+
20
+ Or install it yourself as:
21
+
22
+ ```sh
23
+ $ gem install network-client
24
+ ```
25
+
26
+ ## Usage
27
+
28
+
29
+
30
+ ## Contributing
31
+
32
+ Bug reports and pull requests are very much appreciated at [Github](https://github.com/abarrak/network-client).
33
+
34
+
35
+ ## License
36
+ [MIT](http://opensource.org/licenses/MIT).
37
+
@@ -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,2 @@
1
+ require_relative "network-client/core"
2
+ require_relative "network-client/version"
@@ -0,0 +1,128 @@
1
+ require 'net/http'
2
+ require 'json'
3
+ require 'securerandom'
4
+ require 'erb'
5
+ require 'logger'
6
+
7
+
8
+ module NetworkClient
9
+ # This class is simple HTTP client that meant to be initialized configured with a single URI.
10
+ # Subsequent calls should target endpoints/paths of that URI.
11
+ #
12
+ # Return values of its rest-like methods is Struct holding two values for the code and response
13
+ # body parsed as JSON.
14
+ #
15
+ class Client
16
+
17
+ HTTP_VERBS = {
18
+ :get => Net::HTTP::Get,
19
+ :post => Net::HTTP::Post,
20
+ :put => Net::HTTP::Put,
21
+ :delete => Net::HTTP::Delete
22
+ }
23
+
24
+ def initialize(endpoint:, tries: 1)
25
+ @uri = URI.parse(endpoint)
26
+ @tries = tries
27
+ set_http_client
28
+ set_logger
29
+ set_response_struct
30
+ end
31
+
32
+ def get(path, params = {})
33
+ request_json :get, path, params
34
+ end
35
+
36
+ def post(path, params = {})
37
+ request_json :post, path, params
38
+ end
39
+
40
+ def put(path, params = {})
41
+ request_json :put, path, params
42
+ end
43
+
44
+ def delete(path, params = {})
45
+ request_json :delete, path, params
46
+ end
47
+
48
+ private
49
+
50
+ def set_http_client
51
+ @http = Net::HTTP.new(@uri.host, @uri.port)
52
+ @http.use_ssl = true
53
+ @http.verify_mode = OpenSSL::SSL::VERIFY_NONE
54
+ end
55
+
56
+ def set_logger
57
+ @logger ||= begin
58
+ if block_given?
59
+ yield
60
+ else
61
+ logger = Logger.new(STDOUT)
62
+ logger.level = Logger::DEBUG
63
+ logger
64
+ end
65
+ end
66
+ end
67
+
68
+ def set_response_struct
69
+ @response_struct = Struct.new(:code, :body)
70
+ end
71
+
72
+ def request_json(http_method, path, params)
73
+ response = request(http_method, path, params)
74
+ body = JSON.parse(response.body)
75
+
76
+ @response_struct.new(:code => response.code, :body => body)
77
+
78
+ rescue JSON::ParserError => error
79
+ @logger.error "parsing response body as json failed.\n Details: \n #{error.message}"
80
+ response
81
+ end
82
+
83
+ def request(http_method, path, params = {})
84
+ case http_method
85
+ when :get
86
+ full_path = encode_path_params(path, params)
87
+ request = HTTP_VERBS[http_method].new(full_path)
88
+ else
89
+ request = HTTP_VERBS[http_method].new(path)
90
+ request.set_form_data(params)
91
+ end
92
+
93
+ response = http_request(request)
94
+ case code = response.code
95
+ when Net::HTTPSuccess
96
+ true
97
+ else
98
+ @logger.error "example endpoint responded with a non-success #{code} code."
99
+ end
100
+
101
+ response
102
+ end
103
+
104
+ def http_request(request)
105
+ begin
106
+ tries_count ||= @tries
107
+ response = @http.request(request)
108
+ rescue Errno::ECONNREFUSED, Net::ReadTimeout, Net::OpenTimeout => error
109
+ @logger.warn(error)
110
+ retry unless (tries_count -= 1).zero?
111
+ else
112
+ response
113
+ end
114
+ end
115
+
116
+ def errors_to_recover_by_retry
117
+ end
118
+
119
+ def errors_to_recover_by_propogate
120
+ end
121
+
122
+ def encode_path_params(path, params)
123
+ return path if params.empty?
124
+ encoded = URI.encode_www_form(params)
125
+ [path, encoded].join("?")
126
+ end
127
+ end
128
+ end
@@ -0,0 +1,3 @@
1
+ module NetworkClient
2
+ VERSION = "1.0.0"
3
+ end
@@ -0,0 +1,33 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+
5
+ require 'network-client/version'
6
+ require 'date'
7
+
8
+ Gem::Specification.new do |spec|
9
+ spec.name = "network-client"
10
+ spec.version = NetworkClient::VERSION
11
+ spec.date = Date.today.to_s
12
+ spec.authors = ["Abdullah Barrak (abarrak)"]
13
+ spec.email = ["abdullah@abarrak.com"]
14
+
15
+ spec.summary = "A thin resilient wrapper around ruby's Net::HTTP."
16
+ spec.description = "network-client gem is almost a drop-in thin layer around Net::HTTP classes \
17
+ with simple error handling and retry functionality implemented."
18
+ spec.homepage = "https://github.com/abarrak/network-client"
19
+ spec.license = "MIT"
20
+
21
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
22
+ f.match(%r{^(test|spec|features)/})
23
+ end
24
+
25
+ spec.require_paths = %w(lib)
26
+
27
+ spec.add_development_dependency "bundler", "~> 1.14"
28
+ spec.add_development_dependency "rake", "~> 10.0"
29
+ spec.add_development_dependency "rspec", "~> 3.6"
30
+ spec.add_development_dependency "factory_girl", "~> 4.5"
31
+ spec.add_development_dependency "simplecov", '~> 0'
32
+ spec.add_development_dependency 'codeclimate-test-reporter', '~> 1.0', '>= 1.0.0'
33
+ end
metadata ADDED
@@ -0,0 +1,149 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: network-client
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Abdullah Barrak (abarrak)
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-05-14 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.14'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.14'
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.6'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.6'
55
+ - !ruby/object:Gem::Dependency
56
+ name: factory_girl
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '4.5'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '4.5'
69
+ - !ruby/object:Gem::Dependency
70
+ name: simplecov
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: codeclimate-test-reporter
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '1.0'
90
+ - - ">="
91
+ - !ruby/object:Gem::Version
92
+ version: 1.0.0
93
+ type: :development
94
+ prerelease: false
95
+ version_requirements: !ruby/object:Gem::Requirement
96
+ requirements:
97
+ - - "~>"
98
+ - !ruby/object:Gem::Version
99
+ version: '1.0'
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ version: 1.0.0
103
+ description: network-client gem is almost a drop-in thin layer around Net::HTTP classes with
104
+ simple error handling and retry functionality implemented.
105
+ email:
106
+ - abdullah@abarrak.com
107
+ executables: []
108
+ extensions: []
109
+ extra_rdoc_files: []
110
+ files:
111
+ - ".gitignore"
112
+ - ".rspec"
113
+ - ".ruby-gemset"
114
+ - ".ruby-version"
115
+ - ".travis.yml"
116
+ - Gemfile
117
+ - Gemfile.lock
118
+ - LICENSE
119
+ - README.md
120
+ - Rakefile
121
+ - lib/network-client.rb
122
+ - lib/network-client/core.rb
123
+ - lib/network-client/version.rb
124
+ - network-client.gemspec
125
+ homepage: https://github.com/abarrak/network-client
126
+ licenses:
127
+ - MIT
128
+ metadata: {}
129
+ post_install_message:
130
+ rdoc_options: []
131
+ require_paths:
132
+ - lib
133
+ required_ruby_version: !ruby/object:Gem::Requirement
134
+ requirements:
135
+ - - ">="
136
+ - !ruby/object:Gem::Version
137
+ version: '0'
138
+ required_rubygems_version: !ruby/object:Gem::Requirement
139
+ requirements:
140
+ - - ">="
141
+ - !ruby/object:Gem::Version
142
+ version: '0'
143
+ requirements: []
144
+ rubyforge_project:
145
+ rubygems_version: 2.6.11
146
+ signing_key:
147
+ specification_version: 4
148
+ summary: A thin resilient wrapper around ruby's Net::HTTP.
149
+ test_files: []