async-rest 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: 980a446f72bdda0d1c784191fcb21212584d96bd7ea7f677559789d2dc982668
4
+ data.tar.gz: edfd0ce4873aea371c007a40ab982aa677f891f199ddf14f9fe0a75f8853668b
5
+ SHA512:
6
+ metadata.gz: 3c34905a2dc51ddf5068adb1107b04c7c7e43c4530d6fac4469c888610303921677346eeccfe5d772d3647343c7b04494fa698ef5cff4ddb460bf0c7c9b97f50
7
+ data.tar.gz: 3647e473eb28edc8f229b5be613e0a068496ac704e617159afd34103405015214df2687a93752f62ef4bfde9b7f970f3fbf57b92d5ca21756220f4f6e500d383
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,3 @@
1
+ --format documentation
2
+ --warnings
3
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,20 @@
1
+ language: ruby
2
+ cache: bundler
3
+
4
+ before_script:
5
+ - gem update --system
6
+ - gem install bundler
7
+
8
+ matrix:
9
+ include:
10
+ - rvm: 2.3
11
+ - rvm: 2.4
12
+ - rvm: 2.5
13
+ - rvm: jruby-head
14
+ env: JRUBY_OPTS="--debug -X+O"
15
+ - rvm: ruby-head
16
+ - rvm: rbx-3
17
+ allow_failures:
18
+ - rvm: ruby-head
19
+ - rvm: jruby-head
20
+ - rvm: rbx-3
data/Gemfile ADDED
@@ -0,0 +1,13 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in async-io.gemspec
4
+ gemspec
5
+
6
+ group :development do
7
+ gem 'pry'
8
+ end
9
+
10
+ group :test do
11
+ gem 'simplecov'
12
+ gem 'coveralls', require: false
13
+ end
data/README.md ADDED
@@ -0,0 +1,63 @@
1
+ # Async::REST
2
+
3
+ An asynchronous client and server implementation of RESTful interfaces.
4
+
5
+ [![Build Status](https://secure.travis-ci.org/socketry/async-rest.svg)](http://travis-ci.org/socketry/async-rest)
6
+ [![Code Climate](https://codeclimate.com/github/socketry/async-rest.svg)](https://codeclimate.com/github/socketry/async-rest)
7
+ [![Coverage Status](https://coveralls.io/repos/socketry/async-rest/badge.svg)](https://coveralls.io/r/socketry/async-rest)
8
+
9
+ [async]: https://github.com/socketry/async
10
+ [async-io]: https://github.com/socketry/async-io
11
+ [falcon]: https://github.com/socketry/falcon
12
+
13
+ ## Installation
14
+
15
+ Add this line to your application's Gemfile:
16
+
17
+ ```ruby
18
+ gem 'async-rest'
19
+ ```
20
+
21
+ And then execute:
22
+
23
+ $ bundle
24
+
25
+ Or install it yourself as:
26
+
27
+ $ gem install async-rest
28
+
29
+ ## Usage
30
+
31
+ ...
32
+
33
+ ## Contributing
34
+
35
+ 1. Fork it
36
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
37
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
38
+ 4. Push to the branch (`git push origin my-new-feature`)
39
+ 5. Create new Pull Request
40
+
41
+ ## License
42
+
43
+ Released under the MIT license.
44
+
45
+ Copyright, 2015, by [Samuel G. D. Williams](http://www.codeotaku.com/samuel-williams).
46
+
47
+ Permission is hereby granted, free of charge, to any person obtaining a copy
48
+ of this software and associated documentation files (the "Software"), to deal
49
+ in the Software without restriction, including without limitation the rights
50
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
51
+ copies of the Software, and to permit persons to whom the Software is
52
+ furnished to do so, subject to the following conditions:
53
+
54
+ The above copyright notice and this permission notice shall be included in
55
+ all copies or substantial portions of the Software.
56
+
57
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
58
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
59
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
60
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
61
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
62
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
63
+ THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:test)
5
+
6
+ task :default => :test
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ require_relative 'lib/async/rest/version'
3
+
4
+ Gem::Specification.new do |spec|
5
+ spec.name = "async-rest"
6
+ spec.version = Async::REST::VERSION
7
+ spec.authors = ["Samuel Williams"]
8
+ spec.email = ["samuel.williams@oriontransfer.co.nz"]
9
+
10
+ spec.summary = "A library for RESTful clients and servers."
11
+ spec.homepage = "https://github.com/socketry/async-rest"
12
+
13
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
14
+ f.match(%r{^(test|spec|features)/})
15
+ end
16
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
+ spec.require_paths = ["lib"]
18
+
19
+ spec.add_dependency("async-http", "~> 0.18.0")
20
+
21
+ spec.add_development_dependency "async-rspec", "~> 1.1"
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.3"
24
+ spec.add_development_dependency "rspec", "~> 3.6"
25
+ spec.add_development_dependency "rake"
26
+ end
@@ -0,0 +1,141 @@
1
+ # Copyright, 2017, by Samuel G. D. Williams. <http://www.codeotaku.com>
2
+ #
3
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ # of this software and associated documentation files (the "Software"), to deal
5
+ # in the Software without restriction, including without limitation the rights
6
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ # copies of the Software, and to permit persons to whom the Software is
8
+ # furnished to do so, subject to the following conditions:
9
+ #
10
+ # The above copyright notice and this permission notice shall be included in
11
+ # all copies or substantial portions of the Software.
12
+ #
13
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ # THE SOFTWARE.
20
+
21
+ module Async
22
+ module REST
23
+ # A relative reference, excluding any authority.
24
+ class Reference
25
+ def initialize(path, query_string, fragment, parameters)
26
+ @path = path
27
+ @query_string = query_string
28
+ @fragment = fragment
29
+ @parameters = parameters
30
+ end
31
+
32
+ # Generate a reference from a path and user parameters. The path may contain a `#fragment` or `?query=parameters`.
33
+ def self.parse(path = '/', parameters = nil)
34
+ base, fragment = path.split('#', 2)
35
+ path, query_string = base.split('?', 2)
36
+
37
+ self.new(path, query_string, fragment, parameters)
38
+ end
39
+
40
+ # The path component, e.g. /foo/bar/index.html
41
+ attr :path
42
+
43
+ # The un-parsed query string, e.g. 'x=10&y=20'
44
+ attr :query_string
45
+
46
+ # A fragment, the part after the '#'
47
+ attr :fragment
48
+
49
+ # User supplied parameters that will be appended to the query part.
50
+ attr :parameters
51
+
52
+ def append(buffer)
53
+ if @query_string
54
+ buffer << escape_path(@path) << '?' << query_string
55
+ buffer << '&' << query_parameters if @parameters
56
+ else
57
+ buffer << escape_path(@path)
58
+ buffer << '?' << query_parameters if @parameters
59
+ end
60
+
61
+ if @fragment
62
+ buffer << '#' << escape(@fragment)
63
+ end
64
+
65
+ return buffer
66
+ end
67
+
68
+ def to_str
69
+ append(String.new)
70
+ end
71
+
72
+ alias to_s to_str
73
+
74
+ def + path
75
+ self.dup(path)
76
+ end
77
+
78
+ def [] parameters
79
+ self.dup(nil, parameters)
80
+ end
81
+
82
+ def dup(path = nil, parameters = nil)
83
+ if parameters and @parameters
84
+ parameters = @parameters.merge(parameters)
85
+ else
86
+ parameters = @parameters
87
+ end
88
+
89
+ if path
90
+ path = @path + '/' + path
91
+ else
92
+ path = @path
93
+ end
94
+
95
+ self.class.new(path, @query_string, @fragment, parameters)
96
+ end
97
+
98
+ private
99
+
100
+ # According to https://tools.ietf.org/html/rfc3986#section-3.3, we escape non-pchar.
101
+ NON_PCHAR = /([^a-zA-Z0-9_\-\.~!$&'()*+,;=:@\/]+)/.freeze
102
+
103
+ def escape_path(path)
104
+ encoding = path.encoding
105
+ path.b.gsub(NON_PCHAR) do |m|
106
+ '%' + m.unpack('H2' * m.bytesize).join('%').upcase
107
+ end.force_encoding(encoding)
108
+ end
109
+
110
+ # Escapes a generic string, using percent encoding.
111
+ def escape(string)
112
+ encoding = string.encoding
113
+ string.b.gsub(/([^a-zA-Z0-9_.\-]+)/) do |m|
114
+ '%' + m.unpack('H2' * m.bytesize).join('%').upcase
115
+ end.force_encoding(encoding)
116
+ end
117
+
118
+ def query_parameters
119
+ build_nested_query(@parameters)
120
+ end
121
+
122
+ def build_nested_query(value, prefix = nil)
123
+ case value
124
+ when Array
125
+ value.map { |v|
126
+ build_nested_query(v, "#{prefix}[]")
127
+ }.join("&")
128
+ when Hash
129
+ value.map { |k, v|
130
+ build_nested_query(v, prefix ? "#{prefix}[#{escape(k.to_s)}]" : escape(k.to_s))
131
+ }.reject(&:empty?).join('&')
132
+ when nil
133
+ prefix
134
+ else
135
+ raise ArgumentError, "value must be a Hash" if prefix.nil?
136
+ "#{prefix}=#{escape(value.to_s)}"
137
+ end
138
+ end
139
+ end
140
+ end
141
+ end
@@ -0,0 +1,125 @@
1
+ # Copyright, 2017, by Samuel G. D. Williams. <http://www.codeotaku.com>
2
+ #
3
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ # of this software and associated documentation files (the "Software"), to deal
5
+ # in the Software without restriction, including without limitation the rights
6
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ # copies of the Software, and to permit persons to whom the Software is
8
+ # furnished to do so, subject to the following conditions:
9
+ #
10
+ # The above copyright notice and this permission notice shall be included in
11
+ # all copies or substantial portions of the Software.
12
+ #
13
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ # THE SOFTWARE.
20
+
21
+ require_relative 'reference'
22
+
23
+ require 'async/http/client'
24
+ require 'json'
25
+
26
+ module Async
27
+ module REST
28
+ class JSONBody
29
+ def initialize(body)
30
+ @body = body
31
+ end
32
+
33
+ def close
34
+ @body = @body.close
35
+
36
+ return self
37
+ end
38
+
39
+ def join
40
+ JSON.parse(@body.join, symbolize_names: true)
41
+ end
42
+
43
+ def self.dump(payload)
44
+ JSON.dump(payload)
45
+ end
46
+
47
+ def finished?
48
+ @body.finished?
49
+ end
50
+ end
51
+
52
+ class Resource
53
+ def initialize(client, reference = Reference.parse, headers = {}, max_redirects: 10)
54
+ @client = client
55
+ @reference = reference
56
+ @headers = headers
57
+
58
+ @max_redirects = max_redirects
59
+ end
60
+
61
+ def [] path
62
+ self.class.new(@client, @reference.nest(path), @headers, max_redirects: @max_redirects)
63
+ end
64
+
65
+ def with(**headers)
66
+ self.class.new(@client, @reference, @headers.merge(headers), max_redirects: @max_redirects)
67
+ end
68
+
69
+ def wrapper_for(content_type)
70
+ if content_type == 'application/json'
71
+ return JSONBody
72
+ end
73
+ end
74
+
75
+ def prepare_body(payload)
76
+ return [] if payload.nil?
77
+
78
+ content_type = @headers['content-type']
79
+
80
+ if wrapper = wrapper_for(content_type)
81
+ return wrapper.dump(payload)
82
+ else
83
+ raise ArgumentError.new("Unsure how to convert payload to #{content_type}!")
84
+ end
85
+ end
86
+
87
+ def process_response(response)
88
+ content_type = response.headers['content-type']
89
+
90
+ if wrapper = wrapper_for(content_type)
91
+ response.body = wrapper.new(response.body)
92
+ end
93
+
94
+ return response
95
+ end
96
+
97
+ HTTP::Client::VERBS.each do |verb|
98
+ define_method(verb.downcase) do |payload = nil, **parameters, &block|
99
+ reference = @reference.dup(nil, parameters)
100
+
101
+ response = self.request(verb, reference.to_str, @headers, prepare_body(payload)) do |response|
102
+ process_response(response)
103
+ end
104
+
105
+ return response
106
+ end
107
+ end
108
+
109
+ def request(verb, location, *args)
110
+ @max_redirects.times do
111
+ @client.request(verb, location, *args) do |response|
112
+ if response.redirection?
113
+ verb = 'GET' unless response.preserve_method?
114
+ location = response.headers['location']
115
+ else
116
+ return yield response
117
+ end
118
+ end
119
+ end
120
+
121
+ raise ArgumentError.new("Too many redirections!")
122
+ end
123
+ end
124
+ end
125
+ end
@@ -0,0 +1,25 @@
1
+ # Copyright, 2017, by Samuel G. D. Williams. <http://www.codeotaku.com>
2
+ #
3
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ # of this software and associated documentation files (the "Software"), to deal
5
+ # in the Software without restriction, including without limitation the rights
6
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ # copies of the Software, and to permit persons to whom the Software is
8
+ # furnished to do so, subject to the following conditions:
9
+ #
10
+ # The above copyright notice and this permission notice shall be included in
11
+ # all copies or substantial portions of the Software.
12
+ #
13
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ # THE SOFTWARE.
20
+
21
+ module Async
22
+ module REST
23
+ VERSION = "0.1.0"
24
+ end
25
+ end
data/lib/async/rest.rb ADDED
@@ -0,0 +1,21 @@
1
+ # Copyright, 2017, by Samuel G. D. Williams. <http://www.codeotaku.com>
2
+ #
3
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ # of this software and associated documentation files (the "Software"), to deal
5
+ # in the Software without restriction, including without limitation the rights
6
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ # copies of the Software, and to permit persons to whom the Software is
8
+ # furnished to do so, subject to the following conditions:
9
+ #
10
+ # The above copyright notice and this permission notice shall be included in
11
+ # all copies or substantial portions of the Software.
12
+ #
13
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ # THE SOFTWARE.
20
+
21
+ require "async/rest/version"
metadata ADDED
@@ -0,0 +1,124 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: async-rest
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Samuel Williams
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-04-13 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: async-http
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.18.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 0.18.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: async-rspec
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.1'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.1'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.3'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.3'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.6'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.6'
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
+ description:
84
+ email:
85
+ - samuel.williams@oriontransfer.co.nz
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - ".rspec"
92
+ - ".travis.yml"
93
+ - Gemfile
94
+ - README.md
95
+ - Rakefile
96
+ - async-rest.gemspec
97
+ - lib/async/rest.rb
98
+ - lib/async/rest/reference.rb
99
+ - lib/async/rest/resource.rb
100
+ - lib/async/rest/version.rb
101
+ homepage: https://github.com/socketry/async-rest
102
+ licenses: []
103
+ metadata: {}
104
+ post_install_message:
105
+ rdoc_options: []
106
+ require_paths:
107
+ - lib
108
+ required_ruby_version: !ruby/object:Gem::Requirement
109
+ requirements:
110
+ - - ">="
111
+ - !ruby/object:Gem::Version
112
+ version: '0'
113
+ required_rubygems_version: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ requirements: []
119
+ rubyforge_project:
120
+ rubygems_version: 2.7.6
121
+ signing_key:
122
+ specification_version: 4
123
+ summary: A library for RESTful clients and servers.
124
+ test_files: []