spine-http 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +36 -0
- data/.rspec +3 -0
- data/CHANGELOG.md +14 -0
- data/Gemfile +4 -0
- data/LICENSE +12 -0
- data/README.md +39 -0
- data/Rakefile +1 -0
- data/lib/spine/http.rb +12 -0
- data/lib/spine/http/request.rb +89 -0
- data/lib/spine/http/response.rb +25 -0
- data/lib/spine/http/version.rb +5 -0
- data/spine-http.gemspec +24 -0
- metadata +98 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 421fcefdc3f33a25d710f33becdd7c2ea960b15e
|
4
|
+
data.tar.gz: f583442e5a5e4d02959782063dc41c0148ead237
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 30a4bdd727d6617ff6975c451e95cff910635167cfa845f1682564143f393755354ad40695bcf3b63a843058ef0d6c59d83e7b98c6b8fd3be9f59d9d667d7087
|
7
|
+
data.tar.gz: 6a10013c7a37e6d279863b63f62e11cb5e9b7479ebb6aff4c0e6d0d87add34765dba4b9c8a90a8a955ff48a3d5b183f116e84cbbb738cf759381c29049b49058
|
data/.gitignore
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
*.gem
|
2
|
+
*.rbc
|
3
|
+
/.config
|
4
|
+
/coverage/
|
5
|
+
/InstalledFiles
|
6
|
+
/pkg/
|
7
|
+
/spec/reports/
|
8
|
+
/test/tmp/
|
9
|
+
/test/version_tmp/
|
10
|
+
/tmp/
|
11
|
+
|
12
|
+
## Specific to RubyMotion:
|
13
|
+
.dat*
|
14
|
+
.repl_history
|
15
|
+
build/
|
16
|
+
|
17
|
+
## Documentation cache and generated files:
|
18
|
+
/.yardoc/
|
19
|
+
/_yardoc/
|
20
|
+
/doc/
|
21
|
+
/rdoc/
|
22
|
+
|
23
|
+
## Environment normalisation:
|
24
|
+
/bin/
|
25
|
+
/.bundle/
|
26
|
+
/vendor/bundle
|
27
|
+
/lib/bundler/man/
|
28
|
+
|
29
|
+
# for a library or gem, you might want to ignore these files since the code is
|
30
|
+
# intended to run in multiple environments; otherwise, check them in:
|
31
|
+
# Gemfile.lock
|
32
|
+
# .ruby-version
|
33
|
+
# .ruby-gemset
|
34
|
+
|
35
|
+
# unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
|
36
|
+
.rvmrc
|
data/.rspec
ADDED
data/CHANGELOG.md
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
Copyright (c) 2015, TOGGL LLC
|
2
|
+
All rights reserved.
|
3
|
+
|
4
|
+
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
|
5
|
+
|
6
|
+
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
|
7
|
+
|
8
|
+
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
|
9
|
+
|
10
|
+
3. Neither the name of the TOGGL LLC nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
|
11
|
+
|
12
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
data/README.md
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
# Spine::Http
|
2
|
+
|
3
|
+
[![Gem Version](https://badge.fury.io/rb/spine-http.svg)](http://badge.fury.io/rb/spine-http)
|
4
|
+
[![Dependency Status](https://gemnasium.com/rspine/http.svg)](https://gemnasium.com/rspine/http)
|
5
|
+
[![Code Climate](https://codeclimate.com/github/rspine/http/badges/gpa.svg)](https://codeclimate.com/github/rspine/http)
|
6
|
+
|
7
|
+
Wrapper for `Net::Http` to make requests easier.
|
8
|
+
|
9
|
+
## Installation
|
10
|
+
|
11
|
+
To install it, add the gem to your Gemfile:
|
12
|
+
|
13
|
+
```ruby
|
14
|
+
gem 'spine-http'
|
15
|
+
```
|
16
|
+
|
17
|
+
Then run `bundle`. If you're not using Bundler, just `gem install spine-http`.
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
```ruby
|
22
|
+
response = Spine::Http::Request.new('https://api.example.com/me')
|
23
|
+
.header('Authorization', 'Bearer 123')
|
24
|
+
.query(include_examples: true)
|
25
|
+
.get
|
26
|
+
|
27
|
+
response.body(Spine::ContentTypes::Json)
|
28
|
+
# => { id: 1, name: 'Myself' }
|
29
|
+
|
30
|
+
response = Spine::Http::Request.new('https://api.example.com/me')
|
31
|
+
.header('Authorization', 'Bearer 123')
|
32
|
+
.payload(name: 'Me')
|
33
|
+
.post
|
34
|
+
|
35
|
+
response.code
|
36
|
+
# => 200
|
37
|
+
response.is?(Net::HTTPSuccess)
|
38
|
+
# => true
|
39
|
+
```
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/lib/spine/http.rb
ADDED
@@ -0,0 +1,89 @@
|
|
1
|
+
require 'net/http'
|
2
|
+
require 'spine/content_types'
|
3
|
+
|
4
|
+
module Spine
|
5
|
+
module Http
|
6
|
+
class Request
|
7
|
+
attr_reader :uri, :headers
|
8
|
+
|
9
|
+
def initialize(uri)
|
10
|
+
@uri = URI.parse(uri)
|
11
|
+
@headers = {}
|
12
|
+
@payload = ''
|
13
|
+
end
|
14
|
+
|
15
|
+
def query(parameters)
|
16
|
+
uri.query = URI.encode_www_form(parameters)
|
17
|
+
self
|
18
|
+
end
|
19
|
+
|
20
|
+
def header(key, value)
|
21
|
+
headers[key] = value
|
22
|
+
self
|
23
|
+
end
|
24
|
+
|
25
|
+
def payload(data, content_type = ContentTypes::Plain)
|
26
|
+
@payload = content_type.dump(data)
|
27
|
+
self
|
28
|
+
end
|
29
|
+
|
30
|
+
def get
|
31
|
+
execute(Net::HTTP::Get.new(uri.request_uri))
|
32
|
+
end
|
33
|
+
|
34
|
+
def post
|
35
|
+
request = Net::HTTP::Post.new(uri.request_uri)
|
36
|
+
add_payload(request)
|
37
|
+
execute(request)
|
38
|
+
end
|
39
|
+
|
40
|
+
def patch
|
41
|
+
request = Net::HTTP::Patch.new(uri.request_uri)
|
42
|
+
add_payload(request)
|
43
|
+
execute(request)
|
44
|
+
end
|
45
|
+
|
46
|
+
def put
|
47
|
+
request = Net::HTTP::Put.new(uri.request_uri)
|
48
|
+
add_payload(request)
|
49
|
+
execute(request)
|
50
|
+
end
|
51
|
+
|
52
|
+
def delete
|
53
|
+
request = Net::HTTP::Delete.new(uri.request_uri)
|
54
|
+
execute(request)
|
55
|
+
end
|
56
|
+
|
57
|
+
private
|
58
|
+
|
59
|
+
def add_payload(request)
|
60
|
+
if @payload.is_a?(Hash)
|
61
|
+
request.set_form_data(@payload)
|
62
|
+
else
|
63
|
+
request.body = @payload
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
def add_headers(request)
|
68
|
+
headers.each do |key, value|
|
69
|
+
request[key] = value
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
def execute(request)
|
74
|
+
add_headers(request)
|
75
|
+
Response.new(http.request(request))
|
76
|
+
end
|
77
|
+
|
78
|
+
def http
|
79
|
+
@http ||= build_connection
|
80
|
+
end
|
81
|
+
|
82
|
+
def build_connection
|
83
|
+
http = Net::HTTP.new(uri.host, uri.port)
|
84
|
+
http.use_ssl = (uri.scheme == 'https')
|
85
|
+
http
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
89
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
require 'spine/content_types'
|
2
|
+
|
3
|
+
module Spine
|
4
|
+
module Http
|
5
|
+
class Response
|
6
|
+
attr_reader :raw
|
7
|
+
|
8
|
+
def initialize(raw)
|
9
|
+
@raw = raw
|
10
|
+
end
|
11
|
+
|
12
|
+
def code
|
13
|
+
raw.code
|
14
|
+
end
|
15
|
+
|
16
|
+
def is?(status)
|
17
|
+
raw.is_a?(status)
|
18
|
+
end
|
19
|
+
|
20
|
+
def body(content_type = ContentTypes::Plain)
|
21
|
+
content_type.load(raw.body)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
data/spine-http.gemspec
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
lib = File.expand_path('../lib', __FILE__)
|
2
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
3
|
+
require 'spine/http/version'
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = "spine-http"
|
7
|
+
spec.version = Spine::Http::VERSION
|
8
|
+
spec.authors = ["TOGGL LLC"]
|
9
|
+
spec.email = ["support@toggl.com"]
|
10
|
+
spec.summary = 'Wrapper for Net::HTTP requests.'
|
11
|
+
spec.description = ''
|
12
|
+
spec.homepage = 'https://gitHttp.com/rspine/http'
|
13
|
+
spec.license = 'BSD-3-Clause'
|
14
|
+
|
15
|
+
spec.files = `git ls-files -z`.split("\x0")
|
16
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
17
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
18
|
+
spec.require_paths = ["lib"]
|
19
|
+
|
20
|
+
spec.add_dependency 'spine-content_types', "~> 0.1"
|
21
|
+
|
22
|
+
spec.add_development_dependency 'bundler', "~> 1.7"
|
23
|
+
spec.add_development_dependency 'rake', "~> 10.0"
|
24
|
+
end
|
metadata
ADDED
@@ -0,0 +1,98 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: spine-http
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.3.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- TOGGL LLC
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-05-21 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: spine-content_types
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0.1'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0.1'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.7'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.7'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '10.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '10.0'
|
55
|
+
description: ''
|
56
|
+
email:
|
57
|
+
- support@toggl.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- ".gitignore"
|
63
|
+
- ".rspec"
|
64
|
+
- CHANGELOG.md
|
65
|
+
- Gemfile
|
66
|
+
- LICENSE
|
67
|
+
- README.md
|
68
|
+
- Rakefile
|
69
|
+
- lib/spine/http.rb
|
70
|
+
- lib/spine/http/request.rb
|
71
|
+
- lib/spine/http/response.rb
|
72
|
+
- lib/spine/http/version.rb
|
73
|
+
- spine-http.gemspec
|
74
|
+
homepage: https://gitHttp.com/rspine/http
|
75
|
+
licenses:
|
76
|
+
- BSD-3-Clause
|
77
|
+
metadata: {}
|
78
|
+
post_install_message:
|
79
|
+
rdoc_options: []
|
80
|
+
require_paths:
|
81
|
+
- lib
|
82
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
83
|
+
requirements:
|
84
|
+
- - ">="
|
85
|
+
- !ruby/object:Gem::Version
|
86
|
+
version: '0'
|
87
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
88
|
+
requirements:
|
89
|
+
- - ">="
|
90
|
+
- !ruby/object:Gem::Version
|
91
|
+
version: '0'
|
92
|
+
requirements: []
|
93
|
+
rubyforge_project:
|
94
|
+
rubygems_version: 2.4.5
|
95
|
+
signing_key:
|
96
|
+
specification_version: 4
|
97
|
+
summary: Wrapper for Net::HTTP requests.
|
98
|
+
test_files: []
|