sliver 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 606c72d36976f690024b04140c2c47b4d537ad19
4
+ data.tar.gz: 692fbe6fa8ffb13ddcc4a5bd0d0598ede82302ca
5
+ SHA512:
6
+ metadata.gz: b319b0442c56ca705818f2248d1a8aa18fd06e019826dfedf887246e60d745f1f76776ca84840fd593b6b8783bc8962cfa483d18bc9c0ceaeebbc3d09cffec0e
7
+ data.tar.gz: 210a95f4674840b127ac01e4d05b37d70434749cefc7dd1bf38656748683692b0a0107f7c777b21fa8233529bb74232844588b4a187ef5446cfacc505909664a
data/.gitignore ADDED
@@ -0,0 +1,22 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ language: ruby
2
+ script: bundle exec rspec spec
3
+ cache: bundler
4
+ rvm:
5
+ - 2.1
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Pat Allan
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,103 @@
1
+ # Sliver
2
+
3
+ A super simple, extendable Rack API.
4
+
5
+ ## Installation
6
+
7
+ Add it to your Gemfile like any other gem, or install it manually.
8
+
9
+ ```ruby
10
+ gem 'sliver', '~> 0.0.1'
11
+ ```
12
+
13
+ ## Usage
14
+
15
+ Create a new API (a Rack app, of course), and specify paths with corresponding
16
+ responses. Responses can be anything that responds to `call` with a single
17
+ argument (the request environment) and returns a standard Rack response (an
18
+ array with three items: status code, headers, and body).
19
+
20
+ So, a response can be as simple as a lambda/proc, or it can be a complex class.
21
+ If you want to deal with classes, you can mix in `Sliver::Action` to take
22
+ advantage of some helper methods (and it already stores environment via
23
+ `attr_reader`), and it returns a `Sliver::Response` class which is translated to
24
+ the standard Rack response. Each instance of a class that mixes in
25
+ `Sliver::Action` is handling a specific API request.
26
+
27
+ ```ruby
28
+ app = Sliver::API.new do |api|
29
+ #Endpoints can be namespaced by a path - for example, a version.
30
+ api.path = '/v1'
31
+
32
+ # GET /v1/
33
+ api.connect :get, '/', lambda { |environment|
34
+ [200, {}, ['How dare the Premier ignore my invitations?']]
35
+ }
36
+
37
+ # PUT /v1/change
38
+ api.connect :put, '/change', ChangeAction
39
+ end
40
+
41
+ class ChangeAction
42
+ include Sliver::Action
43
+
44
+ def call
45
+ # Change the status:
46
+ response.status = 404
47
+
48
+ # Add to the response headers:
49
+ response.headers['Content-Type'] = 'text/plain'
50
+
51
+ # Add a response body - let's provide an array, like Rack expects:
52
+ response.body = [
53
+ "How dare the Premier ignore my invitations?",
54
+ "He'll have to go",
55
+ "So, to the bunch he luncheons with",
56
+ "It's second on my list of things to do"
57
+ ]
58
+
59
+ # Access the request environment:
60
+ self.environment
61
+
62
+ # Access to a Rack::Request object built from that environment:
63
+ self.request
64
+ end
65
+ end
66
+ ```
67
+
68
+ If you want all responses to API requests to share some behaviour - say, for
69
+ example, you are always returning JSON - then you can create your own base class
70
+ for this purpose:
71
+
72
+ ```ruby
73
+ class JSONAction
74
+ include Sliver::Action
75
+
76
+ def call
77
+ response.headers['Content-Type'] = 'application/json'
78
+ response.body = [JSON.generate(response.body)]
79
+ end
80
+ end
81
+
82
+ class ChangeAction < JSONAction
83
+ def call
84
+ response.status = 200
85
+ response.body = {'status' => 'OK'}
86
+
87
+ super
88
+ end
89
+ end
90
+ ```
91
+
92
+ ## Contributing
93
+
94
+ 1. Fork it ( https://github.com/pat/sliver/fork )
95
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
96
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
97
+ 4. Push to the branch (`git push origin my-new-feature`)
98
+ 5. Create a new Pull Request
99
+
100
+ ## Licence
101
+
102
+ Copyright (c) 2014, Sliver is developed and maintained by Pat Allan, and is
103
+ released under the open MIT Licence.
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require 'bundler/gem_tasks'
@@ -0,0 +1,22 @@
1
+ module Sliver::Action
2
+ def self.included(base)
3
+ base.extend Sliver::Action::ClassMethods
4
+ end
5
+
6
+ module ClassMethods
7
+ def call(environment)
8
+ response = Sliver::Response.new
9
+ new(environment, response).call
10
+
11
+ response.to_a
12
+ end
13
+ end
14
+
15
+ def initialize(environment, response)
16
+ @environment, @response = environment, response
17
+ end
18
+
19
+ private
20
+
21
+ attr_reader :environment, :response
22
+ end
data/lib/sliver/api.rb ADDED
@@ -0,0 +1,29 @@
1
+ class Sliver::API
2
+ attr_accessor :path
3
+
4
+ def initialize(&block)
5
+ @endpoints = {}
6
+ @path = ''
7
+
8
+ block.call self
9
+ end
10
+
11
+ def call(environment)
12
+ method = environment['REQUEST_METHOD']
13
+ path_info = environment['PATH_INFO'].gsub(/\A#{path}/, '')
14
+ endpoint = endpoints[method][path_info]
15
+
16
+ endpoint ? endpoint.call(environment) : [404, {}, ['Not Found']]
17
+ end
18
+
19
+ def connect(method, path, action)
20
+ method = method.to_s.upcase
21
+
22
+ endpoints[method] ||= {}
23
+ endpoints[method][path] = action
24
+ end
25
+
26
+ private
27
+
28
+ attr_reader :endpoints
29
+ end
@@ -0,0 +1,11 @@
1
+ class Sliver::Response
2
+ attr_accessor :status, :headers, :body
3
+
4
+ def initialize
5
+ @headers = {}
6
+ end
7
+
8
+ def to_a
9
+ [status, headers, body]
10
+ end
11
+ end
data/lib/sliver.rb ADDED
@@ -0,0 +1,7 @@
1
+ module Sliver
2
+ #
3
+ end
4
+
5
+ require 'sliver/action'
6
+ require 'sliver/api'
7
+ require 'sliver/response'
data/sliver.gemspec ADDED
@@ -0,0 +1,21 @@
1
+ # coding: utf-8
2
+ Gem::Specification.new do |spec|
3
+ spec.name = 'sliver'
4
+ spec.version = '0.0.1'
5
+ spec.authors = ['Pat Allan']
6
+ spec.email = ['pat@freelancing-gods.com']
7
+ spec.summary = %q{Lightweight, simple Rack APIs}
8
+ spec.description = %q{A super simple, object-focused extendable Rack API.}
9
+ spec.homepage = 'https://github.com/pat/sliver'
10
+ spec.license = 'MIT'
11
+
12
+ spec.files = `git ls-files -z`.split("\x0")
13
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
14
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
15
+ spec.require_paths = ['lib']
16
+
17
+ spec.add_runtime_dependency 'rack', '>= 1.5.2'
18
+
19
+ spec.add_development_dependency 'rack-test', '>= 0.6.2'
20
+ spec.add_development_dependency 'rspec', '>= 3.0.0.beta2'
21
+ end
@@ -0,0 +1,43 @@
1
+ require 'spec_helper'
2
+
3
+ class GetAction
4
+ include Sliver::Action
5
+
6
+ def call
7
+ response.status = 200
8
+ response.headers = {'Content-Type' => 'text/plain'}
9
+ response.body = ['foo']
10
+ end
11
+ end
12
+
13
+ class EchoAction
14
+ include Sliver::Action
15
+
16
+ def call
17
+ response.status = 200
18
+ response.body = environment['rack.input'].read
19
+ end
20
+ end
21
+
22
+ describe 'Class-based Sliver API' do
23
+ include Rack::Test::Methods
24
+
25
+ let(:app) { Sliver::API.new do |api|
26
+ api.connect :get, '/', GetAction
27
+ api.connect :put, '/echo', EchoAction
28
+ end }
29
+
30
+ it 'constructs responses' do
31
+ get '/'
32
+
33
+ expect(last_response.status).to eq(200)
34
+ expect(last_response.headers['Content-Type']).to eq('text/plain')
35
+ expect(last_response.body).to eq('foo')
36
+ end
37
+
38
+ it 'allows use of environment' do
39
+ put '/echo', 'baz'
40
+
41
+ expect(last_response.body).to eq('baz')
42
+ end
43
+ end
@@ -0,0 +1,64 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'Basic Sliver API' do
4
+ include Rack::Test::Methods
5
+
6
+ let(:app) { Sliver::API.new do |api|
7
+ api.connect :get, '/', lambda { |environment|
8
+ [200, {'Content-Type' => 'text/plain'}, ['foo']]
9
+ }
10
+
11
+ api.connect :get, '/bar', lambda { |environment|
12
+ [200, {'Content-Type' => 'text/plain'}, ['baz']]
13
+ }
14
+
15
+ api.connect :post, '/', lambda { |environment|
16
+ [200, {'Content-Type' => 'text/plain'}, ['qux']]
17
+ }
18
+ end }
19
+
20
+ it 'responds to GET requests' do
21
+ get '/'
22
+
23
+ expect(last_response.status).to eq(200)
24
+ expect(last_response.headers['Content-Type']).to eq('text/plain')
25
+ expect(last_response.body).to eq('foo')
26
+ end
27
+
28
+ it 'delegates to the appropriate endpoint' do
29
+ get '/bar'
30
+
31
+ expect(last_response.body).to eq('baz')
32
+ end
33
+
34
+ it 'responds to POST requests' do
35
+ post '/'
36
+
37
+ expect(last_response.body).to eq('qux')
38
+ end
39
+
40
+ it 'responds to unknown endpoints with a 404' do
41
+ get '/missing'
42
+
43
+ expect(last_response.status).to eq(404)
44
+ expect(last_response.body).to eq('Not Found')
45
+ end
46
+ end
47
+
48
+ describe 'Basic lambda API with a path prefix' do
49
+ include Rack::Test::Methods
50
+
51
+ let(:app) { Sliver::API.new do |api|
52
+ api.path = '/v1'
53
+
54
+ api.connect :get, '/', lambda { |environment|
55
+ [200, {'Content-Type' => 'text/plain'}, ['foo']]
56
+ }
57
+ end }
58
+
59
+ it 'responds to GET requests' do
60
+ get '/v1/'
61
+
62
+ expect(last_response.body).to eq('foo')
63
+ end
64
+ end
@@ -0,0 +1,4 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+
4
+ Bundler.require :default, :development
metadata ADDED
@@ -0,0 +1,104 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sliver
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Pat Allan
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-05-03 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rack
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 1.5.2
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 1.5.2
27
+ - !ruby/object:Gem::Dependency
28
+ name: rack-test
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 0.6.2
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: 0.6.2
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.0.beta2
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.0.beta2
55
+ description: A super simple, object-focused extendable Rack API.
56
+ email:
57
+ - pat@freelancing-gods.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".travis.yml"
64
+ - Gemfile
65
+ - LICENSE.txt
66
+ - README.md
67
+ - Rakefile
68
+ - lib/sliver.rb
69
+ - lib/sliver/action.rb
70
+ - lib/sliver/api.rb
71
+ - lib/sliver/response.rb
72
+ - sliver.gemspec
73
+ - spec/acceptance/class_api_spec.rb
74
+ - spec/acceptance/lambda_api_spec.rb
75
+ - spec/spec_helper.rb
76
+ homepage: https://github.com/pat/sliver
77
+ licenses:
78
+ - MIT
79
+ metadata: {}
80
+ post_install_message:
81
+ rdoc_options: []
82
+ require_paths:
83
+ - lib
84
+ required_ruby_version: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ required_rubygems_version: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ requirements: []
95
+ rubyforge_project:
96
+ rubygems_version: 2.2.2
97
+ signing_key:
98
+ specification_version: 4
99
+ summary: Lightweight, simple Rack APIs
100
+ test_files:
101
+ - spec/acceptance/class_api_spec.rb
102
+ - spec/acceptance/lambda_api_spec.rb
103
+ - spec/spec_helper.rb
104
+ has_rdoc: