safettp 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
+ SHA1:
3
+ metadata.gz: eb5c3ce9fecd9c628c388c8eb29d12e7c0bc4f28
4
+ data.tar.gz: 6c8de0e8e0899c098f56c74e34ded33fde6d4278
5
+ SHA512:
6
+ metadata.gz: b164ee8f62159db74d766a821e197a52956db771f35c43cc9455a70a04a4e20b3b20f10ce22dc7d8692ad941d9e98fc39b4a1efdd9903d332b3888e46bdca9a8
7
+ data.tar.gz: bdb722df61dbb289186c256ae75bcc8d2fe63dc543bc2e65e5247686abcfcf9cb27c8b8ef1082f43612827eff1f6baab392971b5d0de706076545612971222c9
data/.gitignore ADDED
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+
11
+ # rspec failure tracking
12
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.0
5
+ before_install: gem install bundler -v 1.15.4
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in safettp.gemspec
6
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Emric
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,61 @@
1
+ # Safettp
2
+
3
+ Safettp is another HTTP library. It encourages your requests to always handle
4
+ the failure state, and does so in a straightforward and easy way.
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ ```ruby
11
+ gem 'safettp'
12
+ ```
13
+
14
+ And then execute:
15
+
16
+ $ bundle
17
+
18
+ Or install it yourself as:
19
+
20
+ $ gem install safettp
21
+
22
+ ## Synopsis
23
+ ```ruby
24
+ class MyHttpClient
25
+ include Safettp::Client
26
+
27
+ configure do |config|
28
+ config.base_url = 'https://httpbin.org'
29
+ config.default_options = { headers: { Accept: 'application/json' } }
30
+ end
31
+
32
+ def do_post(payload, &block)
33
+ post('/post', payload, &block)
34
+ end
35
+ end
36
+
37
+ MyHttpClient.do_post({ body: 'my_body' }) do |result|
38
+ result.on_success do |response|
39
+ puts response.parsed_body
40
+ end
41
+
42
+ result.on_failure do |response|
43
+ puts 'Request failed :c'
44
+ end
45
+ end
46
+ ```
47
+
48
+ ## Development
49
+
50
+ 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.
51
+
52
+ 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).
53
+
54
+ ## Contributing
55
+
56
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/safettp.
57
+
58
+ ## License
59
+
60
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
61
+
data/Rakefile ADDED
@@ -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
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "safettp"
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
@@ -0,0 +1,11 @@
1
+ class Safettp::BasicAuthenticator
2
+ attr_reader :options
3
+
4
+ def initialize(options)
5
+ @options = options
6
+ end
7
+
8
+ def set(request)
9
+ request.basic_auth(options[:username], options[:password])
10
+ end
11
+ end
@@ -0,0 +1,10 @@
1
+ class Safettp::Client::Configuration
2
+ attr_accessor :base_url
3
+ attr_accessor :default_options
4
+
5
+ def initialize
6
+ @default_options = {
7
+ headers: Safettp::HTTPOptions::DEFAULT_HEADERS
8
+ }
9
+ end
10
+ end
@@ -0,0 +1,62 @@
1
+ module Safettp::Client
2
+ attr_reader :base_url, :options_hash
3
+
4
+ def initialize(base_url = self.class.config.base_url,
5
+ options_hash = self.class.config.default_options)
6
+ @base_url = base_url
7
+ @options_hash = options_hash
8
+ end
9
+
10
+ def perform(*args, &block)
11
+ response = perform_without_guard(*args)
12
+ guard = Safettp::Guard.new(response)
13
+ yield(guard)
14
+ guard.evaluate!
15
+ end
16
+
17
+ def perform_without_guard(method, uri_suffix = '/', options = {})
18
+ url = "#{base_url}#{uri_suffix}"
19
+ Safettp::Request.new(url, options_hash.merge(options))
20
+ .perform(method)
21
+ end
22
+
23
+ module ClassMethods
24
+ attr_accessor :config
25
+
26
+ def instance_from_default_options
27
+ new(config.base_url, config.default_options)
28
+ end
29
+
30
+ def method_missing(method, *args, &block)
31
+ return super unless respond_to_missing?(method, *args, &block)
32
+ instance_from_default_options.public_send(method, *args, &block)
33
+ end
34
+
35
+ def respond_to_missing?(method, *args, &block)
36
+ instance_from_default_options.respond_to?(method)
37
+ end
38
+
39
+ def configure
40
+ self.config ||= Safettp::Client::Configuration.new
41
+ yield(config)
42
+ end
43
+ end
44
+
45
+ def self.included(base)
46
+ base.extend(ClassMethods)
47
+
48
+ %i[get delete].each do |method|
49
+ define_method(method) do
50
+ |uri_suffix, query = {}, options = { query: query }, &block|
51
+ perform(method, uri_suffix, options, &block)
52
+ end
53
+ end
54
+
55
+ %i[post put patch].each do |method|
56
+ define_method(method) do
57
+ |uri_suffix, body = {}, options = { body: body }, &block|
58
+ perform(method, uri_suffix, options, &block)
59
+ end
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,2 @@
1
+ class Safettp::Error < StandardError
2
+ end
@@ -0,0 +1,40 @@
1
+ class Safettp::Guard
2
+ class Safettp::Guard::StatesNotCovered < Safettp::Error
3
+ end
4
+
5
+ attr_reader :covered, :response
6
+
7
+ def initialize(response)
8
+ @response = response
9
+ @covered = []
10
+ end
11
+
12
+ def evaluate!
13
+ return response if safe?
14
+ raise Safettp::Guard::StatesNotCovered
15
+ end
16
+
17
+ def cover(state, &block)
18
+ @covered << state
19
+ yield(response) if response_is?(state)
20
+ end
21
+
22
+ def safe?
23
+ covered.include?(:success) &&
24
+ covered.include?(:failure)
25
+ end
26
+
27
+ def on_success(&block)
28
+ cover(:success, &block)
29
+ end
30
+
31
+ def on_failure(&block)
32
+ cover(:failure, &block)
33
+ end
34
+
35
+ private
36
+
37
+ def response_is?(state)
38
+ response.public_send("#{state}?")
39
+ end
40
+ end
@@ -0,0 +1,39 @@
1
+ class Safettp::HTTPOptions
2
+ AUTHENTICATORS = {
3
+ none: Safettp::NoneAuthenticator,
4
+ basic: Safettp::BasicAuthenticator
5
+ }.freeze
6
+
7
+ DEFAULT_HEADERS = {
8
+ Accept: 'application/json',
9
+ 'Content-Type': 'application/json'
10
+ }.freeze
11
+
12
+ attr_reader :options_hash
13
+
14
+ def initialize(options_hash = {})
15
+ @options_hash = options_hash
16
+ end
17
+
18
+ def headers
19
+ options_hash.fetch(:headers, DEFAULT_HEADERS)
20
+ end
21
+
22
+ def parser
23
+ options_hash.fetch(:parser, Safettp::Parsers::JSON)
24
+ end
25
+
26
+ def query
27
+ URI.encode_www_form(options_hash.fetch(:query, {}))
28
+ end
29
+
30
+ def body
31
+ options_hash.fetch(:body, "")
32
+ end
33
+
34
+ def authorization
35
+ authorization_options = options_hash.fetch(:authorization, { type: :none })
36
+ AUTHENTICATORS.fetch(authorization_options[:type], Safettp::NoneAuthenticator)
37
+ .new(authorization_options)
38
+ end
39
+ end
@@ -0,0 +1,11 @@
1
+ class Safettp::NoneAuthenticator
2
+ attr_reader :options
3
+
4
+ def initialize(options)
5
+ @options = options
6
+ end
7
+
8
+ def set(request)
9
+ request
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ class Safettp::Parsers::JSON
2
+ class << self
3
+ def encode(raw)
4
+ JSON.dump(raw)
5
+ end
6
+
7
+ def decode(raw)
8
+ JSON.parse(raw)
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,2 @@
1
+ module Safettp::Parsers
2
+ end
@@ -0,0 +1,45 @@
1
+ class Safettp::Request::Net
2
+ attr_reader :verb, :uri, :options
3
+
4
+ def initialize(verb, uri, options)
5
+ @verb = verb
6
+ @uri = uri
7
+ @options = options
8
+ end
9
+
10
+ def perform
11
+ http.request(request)
12
+ end
13
+
14
+ def http
15
+ ::Net::HTTP.new(uri.host, uri.port).tap do |obj|
16
+ obj.use_ssl = uri.scheme == 'https'
17
+ obj.verify_mode = OpenSSL::SSL::VERIFY_PEER
18
+ end
19
+ end
20
+
21
+ def request
22
+ klass = Kernel.const_get("Net::HTTP::#{verb.capitalize}")
23
+ klass.new(uri).tap do |request|
24
+ set_headers(request)
25
+ set_body(request)
26
+ set_authorization(request)
27
+ end
28
+ end
29
+
30
+ private
31
+
32
+ def set_authorization(request)
33
+ options.authorization.set(request)
34
+ end
35
+
36
+ def set_body(request)
37
+ request.body = options.parser.encode(options.body)
38
+ end
39
+
40
+ def set_headers(request)
41
+ options.headers.each do |header, value|
42
+ request.add_field(header.to_s, value)
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,14 @@
1
+ class Safettp::Request
2
+ attr_reader :uri, :options
3
+
4
+ def initialize(uri, options = {})
5
+ @uri = URI(uri)
6
+ @options = Safettp::HTTPOptions.new(options)
7
+ @uri.query = @options.query
8
+ end
9
+
10
+ def perform(method)
11
+ net = Safettp::Request::Net.new(method, uri, options)
12
+ Safettp::Response.new(net.perform, options.parser)
13
+ end
14
+ end
@@ -0,0 +1,20 @@
1
+ class Safettp::Response
2
+ attr_reader :http_response, :parser
3
+
4
+ def initialize(http_response, parser = Safettp::Parsers::JSON)
5
+ @http_response = http_response
6
+ @parser = parser
7
+ end
8
+
9
+ def success?
10
+ @http_response.kind_of? Net::HTTPSuccess
11
+ end
12
+
13
+ def failure?
14
+ !success?
15
+ end
16
+
17
+ def parsed_body
18
+ parser.decode(http_response.body)
19
+ end
20
+ end
@@ -0,0 +1,3 @@
1
+ module Safettp
2
+ VERSION = "0.1.0"
3
+ end
data/lib/safettp.rb ADDED
@@ -0,0 +1,19 @@
1
+ require 'safettp/version'
2
+ require 'safettp/request'
3
+ require 'safettp/response'
4
+ require 'safettp/request/net'
5
+ require 'safettp/basic_authenticator'
6
+ require 'safettp/none_authenticator'
7
+ require 'safettp/http_options'
8
+ require 'safettp/parsers'
9
+ require 'safettp/parsers/json'
10
+ require 'safettp/client'
11
+ require 'safettp/client/configuration'
12
+ require 'safettp/error'
13
+ require 'safettp/guard'
14
+ require 'json'
15
+ require 'net/http'
16
+ require 'net/https'
17
+
18
+ module Safettp
19
+ end
data/safettp.gemspec ADDED
@@ -0,0 +1,35 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "safettp/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "safettp"
8
+ spec.version = Safettp::VERSION
9
+ spec.authors = ["Emric"]
10
+ spec.email = ["emric.mansson@gmail.com"]
11
+
12
+ spec.summary = %q{Simple HTTP library with guarded requests.}
13
+ spec.license = "MIT"
14
+
15
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
16
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
17
+ if spec.respond_to?(:metadata)
18
+ spec.metadata["allowed_push_host"] = "https://rubygems.org"
19
+ else
20
+ raise "RubyGems 2.0 or newer is required to protect against " \
21
+ "public gem pushes."
22
+ end
23
+
24
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
25
+ f.match(%r{^(test|spec|features)/})
26
+ end
27
+ spec.bindir = "exe"
28
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
29
+ spec.require_paths = ["lib"]
30
+
31
+ spec.add_development_dependency "bundler", "~> 1.15"
32
+ spec.add_development_dependency "rake", "~> 10.0"
33
+ spec.add_development_dependency "rspec", "~> 3.0"
34
+ spec.add_development_dependency "webmock"
35
+ end
metadata ADDED
@@ -0,0 +1,125 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: safettp
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Emric
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-08-30 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.15'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.15'
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: webmock
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
+ description:
70
+ email:
71
+ - emric.mansson@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".rspec"
78
+ - ".travis.yml"
79
+ - Gemfile
80
+ - LICENSE.txt
81
+ - README.md
82
+ - Rakefile
83
+ - bin/console
84
+ - bin/setup
85
+ - lib/safettp.rb
86
+ - lib/safettp/basic_authenticator.rb
87
+ - lib/safettp/client.rb
88
+ - lib/safettp/client/configuration.rb
89
+ - lib/safettp/error.rb
90
+ - lib/safettp/guard.rb
91
+ - lib/safettp/http_options.rb
92
+ - lib/safettp/none_authenticator.rb
93
+ - lib/safettp/parsers.rb
94
+ - lib/safettp/parsers/json.rb
95
+ - lib/safettp/request.rb
96
+ - lib/safettp/request/net.rb
97
+ - lib/safettp/response.rb
98
+ - lib/safettp/version.rb
99
+ - safettp.gemspec
100
+ homepage:
101
+ licenses:
102
+ - MIT
103
+ metadata:
104
+ allowed_push_host: https://rubygems.org
105
+ post_install_message:
106
+ rdoc_options: []
107
+ require_paths:
108
+ - lib
109
+ required_ruby_version: !ruby/object:Gem::Requirement
110
+ requirements:
111
+ - - ">="
112
+ - !ruby/object:Gem::Version
113
+ version: '0'
114
+ required_rubygems_version: !ruby/object:Gem::Requirement
115
+ requirements:
116
+ - - ">="
117
+ - !ruby/object:Gem::Version
118
+ version: '0'
119
+ requirements: []
120
+ rubyforge_project:
121
+ rubygems_version: 2.5.1
122
+ signing_key:
123
+ specification_version: 4
124
+ summary: Simple HTTP library with guarded requests.
125
+ test_files: []