sliver 0.2.4 → 0.2.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: fe4e83efcb34646cfb507ccc74056fb402265f77
4
- data.tar.gz: f52c128bb56392cd35d97940845c17e1f23fa1cc
2
+ SHA256:
3
+ metadata.gz: c1cd2079096546ca4d779a38c9df4e9986f8e51aded1efd90ee11afad1ccbadf
4
+ data.tar.gz: 282216521e7da8aaf4343c4b3a93d68930e1e53283564a96fbf626a73ca8387d
5
5
  SHA512:
6
- metadata.gz: e9b69f9c741d49387772a373cf6db8eab0dc39fcd9f49df34aa21b86cd54e344434c3d1e2b28f612cdc835fc2035643f73d9fd4fb5784924a551a5ed04a64832
7
- data.tar.gz: bb79a247fddbe76eacc5cb0b07a6fe9ed681cd9ad96a318ccc6ab70fc2e2aa4f21e4c95f07de5a9e08ecc2118571b9685d9eff437c303e151fa31787a5ae0e26
6
+ metadata.gz: f17cbc600f41de13f3e73944412e3ec62b30a8601d9d3d6e985aa2ba8e6a8f558f1ba3130e1790835906cb3fb1facb056be630550f153bb9352df900b9f985ec
7
+ data.tar.gz: 581ee12c280ac3a4749b180232ce106a10893627eb00dd05ae5176acc5f46ba064f9d4f02154f6b9884fa8f2c12abf43d42a6c61d126ac897341707cae012c47
data/README.md CHANGED
@@ -3,7 +3,7 @@
3
3
  A super simple, extendable Rack API.
4
4
 
5
5
  [![Build Status](https://travis-ci.org/pat/sliver.svg?branch=master)](https://travis-ci.org/pat/sliver)
6
- [![Code Climate](https://codeclimate.com/github/pat/sliver.png)](https://codeclimate.com/github/pat/sliver)
6
+ [![Code Climate](https://codeclimate.com/github/pat/sliver.svg)](https://codeclimate.com/github/pat/sliver)
7
7
  [![Gem Version](https://badge.fury.io/rb/sliver.svg)](http://badge.fury.io/rb/sliver)
8
8
 
9
9
  ## Why?
@@ -20,7 +20,7 @@ Ruby doesn't lack for web frameworks, especially ones focused on APIs, but Slive
20
20
  Add it to your Gemfile like any other gem, or install it manually.
21
21
 
22
22
  ```ruby
23
- gem 'sliver', '~> 0.2.4'
23
+ gem 'sliver', '~> 0.2.5'
24
24
  ```
25
25
 
26
26
  ## Usage
data/lib/sliver/action.rb CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Sliver::Action
2
4
  def self.included(base)
3
5
  base.extend Sliver::Action::ClassMethods
@@ -18,7 +20,8 @@ module Sliver::Action
18
20
  end
19
21
 
20
22
  def initialize(environment, response)
21
- @environment, @response = environment, response
23
+ @environment = environment
24
+ @response = response
22
25
  end
23
26
 
24
27
  def request
data/lib/sliver/api.rb CHANGED
@@ -1,6 +1,6 @@
1
- class Sliver::API
2
- NOT_FOUND = [404, {'X-Cascade' => 'pass'}, ['Not Found']].freeze
1
+ # frozen_string_literal: true
3
2
 
3
+ class Sliver::API
4
4
  def initialize(&block)
5
5
  @endpoints = Sliver::Endpoints.new
6
6
 
@@ -10,7 +10,7 @@ class Sliver::API
10
10
  def call(environment)
11
11
  endpoint = endpoints.find environment
12
12
 
13
- endpoint.nil? ? NOT_FOUND : invoke(endpoint, environment)
13
+ endpoint.nil? ? not_found : invoke(endpoint, environment)
14
14
  end
15
15
 
16
16
  def invoke(endpoint, environment)
@@ -24,4 +24,8 @@ class Sliver::API
24
24
  private
25
25
 
26
26
  attr_reader :endpoints
27
+
28
+ def not_found
29
+ [404, {"X-Cascade" => "pass"}, ["Not Found"]]
30
+ end
27
31
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Sliver::Endpoints
2
4
  def initialize
3
5
  @paths = {}
@@ -8,11 +10,11 @@ class Sliver::Endpoints
8
10
  end
9
11
 
10
12
  def find(environment)
11
- key = paths.keys.detect { |key| key.matches?(environment) }
12
- return nil unless key
13
+ path = paths.keys.detect { |key| key.matches?(environment) }
14
+ return nil unless path
13
15
 
14
- environment[Sliver::PATH_KEY] = key
15
- paths[key]
16
+ environment[Sliver::PATH_KEY] = path
17
+ paths[path]
16
18
  end
17
19
 
18
20
  private
data/lib/sliver/hook.rb CHANGED
@@ -1,10 +1,13 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Sliver::Hook
2
4
  def self.call(action, response)
3
5
  new(action, response).call
4
6
  end
5
7
 
6
8
  def initialize(action, response)
7
- @action, @response = action, response
9
+ @action = action
10
+ @response = response
8
11
  end
9
12
 
10
13
  private
data/lib/sliver/path.rb CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Sliver::Path
2
4
  METHOD_KEY = "REQUEST_METHOD".freeze
3
5
  PATH_KEY = "PATH_INFO".freeze
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Sliver::Response
2
4
  attr_accessor :status, :headers, :body
3
5
 
data/lib/sliver/runner.rb CHANGED
@@ -1,6 +1,9 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Sliver::Runner
2
4
  def initialize(klass, environment)
3
- @klass, @environment = klass, environment
5
+ @klass = klass
6
+ @environment = environment
4
7
 
5
8
  @guarded = false
6
9
  end
data/lib/sliver.rb CHANGED
@@ -1,11 +1,13 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Sliver
2
4
  PATH_KEY = "sliver.path".freeze
3
5
  end
4
6
 
5
- require 'sliver/action'
6
- require 'sliver/api'
7
- require 'sliver/endpoints'
8
- require 'sliver/hook'
9
- require 'sliver/path'
10
- require 'sliver/response'
11
- require 'sliver/runner'
7
+ require "sliver/action"
8
+ require "sliver/api"
9
+ require "sliver/endpoints"
10
+ require "sliver/hook"
11
+ require "sliver/path"
12
+ require "sliver/response"
13
+ require "sliver/runner"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sliver
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.4
4
+ version: 0.2.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Pat Allan
8
- autorequire:
9
- bindir: bin
8
+ autorequire:
9
+ bindir: exe
10
10
  cert_chain: []
11
- date: 2017-06-02 00:00:00.000000000 Z
11
+ date: 2024-03-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rack
@@ -38,6 +38,20 @@ dependencies:
38
38
  - - ">="
39
39
  - !ruby/object:Gem::Version
40
40
  version: 0.6.2
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
41
55
  - !ruby/object:Gem::Dependency
42
56
  name: rspec
43
57
  requirement: !ruby/object:Gem::Requirement
@@ -52,6 +66,34 @@ dependencies:
52
66
  - - ">="
53
67
  - !ruby/object:Gem::Version
54
68
  version: 3.6.0
69
+ - !ruby/object:Gem::Dependency
70
+ name: rubocop
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: 0.78.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.78.0
83
+ - !ruby/object:Gem::Dependency
84
+ name: rubocop-performance
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '1.5'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '1.5'
55
97
  description: A super simple, object-focused extendable Rack API.
56
98
  email:
57
99
  - pat@freelancing-gods.com
@@ -59,12 +101,8 @@ executables: []
59
101
  extensions: []
60
102
  extra_rdoc_files: []
61
103
  files:
62
- - ".gitignore"
63
- - ".travis.yml"
64
- - Gemfile
65
104
  - LICENSE.txt
66
105
  - README.md
67
- - Rakefile
68
106
  - lib/sliver.rb
69
107
  - lib/sliver/action.rb
70
108
  - lib/sliver/api.rb
@@ -73,15 +111,14 @@ files:
73
111
  - lib/sliver/path.rb
74
112
  - lib/sliver/response.rb
75
113
  - lib/sliver/runner.rb
76
- - sliver.gemspec
77
- - spec/acceptance/class_api_spec.rb
78
- - spec/acceptance/lambda_api_spec.rb
79
- - spec/spec_helper.rb
80
114
  homepage: https://github.com/pat/sliver
81
115
  licenses:
82
116
  - MIT
83
- metadata: {}
84
- post_install_message:
117
+ metadata:
118
+ homepage_uri: https://github.com/pat/sliver
119
+ source_code_uri: https://github.com/pat/sliver
120
+ rubygems_mfa_required: 'true'
121
+ post_install_message:
85
122
  rdoc_options: []
86
123
  require_paths:
87
124
  - lib
@@ -96,12 +133,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
96
133
  - !ruby/object:Gem::Version
97
134
  version: '0'
98
135
  requirements: []
99
- rubyforge_project:
100
- rubygems_version: 2.6.11
101
- signing_key:
136
+ rubygems_version: 3.4.19
137
+ signing_key:
102
138
  specification_version: 4
103
139
  summary: Lightweight, simple Rack APIs
104
- test_files:
105
- - spec/acceptance/class_api_spec.rb
106
- - spec/acceptance/lambda_api_spec.rb
107
- - spec/spec_helper.rb
140
+ test_files: []
data/.gitignore DELETED
@@ -1,22 +0,0 @@
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 DELETED
@@ -1,9 +0,0 @@
1
- language: ruby
2
- before_script:
3
- - ((ruby --version | grep -q "2.4.1") && export RUBYOPT="--enable-frozen-string-literal") || true
4
- script: bundle exec rspec spec
5
- cache: bundler
6
- rvm:
7
- - 2.2
8
- - 2.3.4
9
- - 2.4.1
data/Gemfile DELETED
@@ -1,13 +0,0 @@
1
- source 'https://rubygems.org'
2
-
3
- gemspec
4
-
5
- %w[
6
- rspec
7
- rspec-core
8
- rspec-expectations
9
- rspec-mocks
10
- rspec-support
11
- ].each do |library|
12
- gem library, :git => "https://github.com/rspec/#{library}.git"
13
- end
data/Rakefile DELETED
@@ -1 +0,0 @@
1
- require 'bundler/gem_tasks'
data/sliver.gemspec DELETED
@@ -1,21 +0,0 @@
1
- # coding: utf-8
2
- Gem::Specification.new do |spec|
3
- spec.name = 'sliver'
4
- spec.version = '0.2.4'
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.6.0'
21
- end
@@ -1,209 +0,0 @@
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
- class AdditionAction
23
- include Sliver::Action
24
-
25
- def call
26
- response.status = 200
27
- response.body = [(request.params['a'].to_i + request.params['b'].to_i)]
28
- end
29
- end
30
-
31
- class SkippedAction
32
- include Sliver::Action
33
-
34
- def skip?
35
- response.status = 400
36
- response.body = ['Invalid']
37
- end
38
-
39
- def call
40
- response.status = 200
41
- response.body = ['Success']
42
- end
43
- end
44
-
45
- class MyParamGuard < Sliver::Hook
46
- def continue?
47
- action.request.params['hello'] == 'world'
48
- end
49
-
50
- def respond
51
- response.status = 404
52
- response.body = ['Not Found']
53
- end
54
- end
55
-
56
- class GuardedAction
57
- include Sliver::Action
58
-
59
- def self.guards
60
- [MyParamGuard]
61
- end
62
-
63
- def call
64
- response.status = 200
65
- response.body = ['Welcome']
66
- end
67
- end
68
-
69
- class UnguardedAction < GuardedAction
70
- def self.guards
71
- super - [MyParamGuard]
72
- end
73
- end
74
-
75
- class IdAction
76
- include Sliver::Action
77
-
78
- def call
79
- response.status = 200
80
- response.body = [path_params['id']]
81
- end
82
- end
83
-
84
- class MultiPathPartAction
85
- include Sliver::Action
86
-
87
- def call
88
- response.status = 200
89
- response.body = ["#{path_params['first']}:#{path_params['second']}"]
90
- end
91
- end
92
-
93
- class JsonProcessor < Sliver::Hook
94
- def call
95
- response.headers['Content-Type'] = 'application/json'
96
- end
97
- end
98
-
99
- class ProcessedAction
100
- include Sliver::Action
101
-
102
- def self.processors
103
- [JsonProcessor]
104
- end
105
-
106
- def call
107
- response.status = 200
108
- response.body = ['[]']
109
- end
110
- end
111
-
112
- describe 'Class-based Sliver API' do
113
- include Rack::Test::Methods
114
-
115
- let(:app) { Sliver::API.new do |api|
116
- api.connect :get, '/', GetAction
117
- api.connect :put, '/echo', EchoAction
118
- api.connect :get, '/addition', AdditionAction
119
- api.connect :get, '/skip', SkippedAction
120
- api.connect :get, '/guard', GuardedAction
121
- api.connect :get, '/unguard', UnguardedAction
122
- api.connect :get, '/my/:id', IdAction
123
- api.connect :get, '/my/:first/:second', MultiPathPartAction
124
- api.connect :get, '/processed', ProcessedAction
125
- end }
126
-
127
- it 'constructs responses' do
128
- get '/'
129
-
130
- expect(last_response.status).to eq(200)
131
- expect(last_response.headers['Content-Type']).to eq('text/plain')
132
- expect(last_response.body).to eq('foo')
133
- end
134
-
135
- it 'allows use of environment' do
136
- put '/echo', 'baz'
137
-
138
- expect(last_response.body).to eq('baz')
139
- end
140
-
141
- it 'allows use of request' do
142
- get '/addition', 'a' => '5', 'b' => '3'
143
-
144
- expect(last_response.body).to eq('8')
145
- end
146
-
147
- it 'allows standard responses to be skipped' do
148
- get '/skip'
149
-
150
- expect(last_response.status).to eq(400)
151
- expect(last_response.body).to eq('Invalid')
152
- end
153
-
154
- it 'blocks guarded actions if they cannot continue' do
155
- get '/guard'
156
-
157
- expect(last_response.status).to eq(404)
158
- expect(last_response.body).to eq('Not Found')
159
- end
160
-
161
- it 'accepts guarded actions that meet criteria' do
162
- get '/guard', 'hello' => 'world'
163
-
164
- expect(last_response.status).to eq(200)
165
- expect(last_response.body).to eq('Welcome')
166
- end
167
-
168
- it 'respects subclass guard changes' do
169
- get '/unguard'
170
-
171
- expect(last_response.status).to eq(200)
172
- expect(last_response.body).to eq('Welcome')
173
- end
174
-
175
- it 'handles path parameter markers' do
176
- get '/my/10'
177
-
178
- expect(last_response.status).to eq(200)
179
- expect(last_response.body).to eq('10')
180
- end
181
-
182
- it 'handles path parameters with full stops' do
183
- get '/my/10.1'
184
-
185
- expect(last_response.status).to eq(200)
186
- expect(last_response.body).to eq('10.1')
187
- end
188
-
189
- it 'handles path parameters with pluses' do
190
- get '/my/10+1'
191
-
192
- expect(last_response.status).to eq(200)
193
- expect(last_response.body).to eq('10+1')
194
- end
195
-
196
- it 'handles multiple path parameter markers' do
197
- get '/my/10/foo'
198
-
199
- expect(last_response.status).to eq(200)
200
- expect(last_response.body).to eq('10:foo')
201
- end
202
-
203
- it 'handles processors' do
204
- get '/processed'
205
-
206
- expect(last_response.status).to eq(200)
207
- expect(last_response.headers['Content-Type']).to eq('application/json')
208
- end
209
- end
@@ -1,64 +0,0 @@
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
-
19
- api.connect :delete, %r{/remove/\d+}, lambda { |environment|
20
- [200, {}, ['removed']]
21
- }
22
- end }
23
-
24
- it 'responds to GET requests' do
25
- get '/'
26
-
27
- expect(last_response.status).to eq(200)
28
- expect(last_response.headers['Content-Type']).to eq('text/plain')
29
- expect(last_response.body).to eq('foo')
30
- end
31
-
32
- it 'matches empty paths as /' do
33
- get ''
34
-
35
- expect(last_response.body).to eq('foo')
36
- end
37
-
38
- it 'delegates to the appropriate endpoint' do
39
- get '/bar'
40
-
41
- expect(last_response.body).to eq('baz')
42
- end
43
-
44
- it 'responds to POST requests' do
45
- post '/'
46
-
47
- expect(last_response.body).to eq('qux')
48
- end
49
-
50
- it 'responds to unknown endpoints with a 404' do
51
- get '/missing'
52
-
53
- expect(last_response.status).to eq(404)
54
- expect(last_response.body).to eq('Not Found')
55
- expect(last_response.headers['X-Cascade']).to eq('pass')
56
- end
57
-
58
- it 'matches against regular expressions' do
59
- delete '/remove/141'
60
-
61
- expect(last_response.status).to eq(200)
62
- expect(last_response.body).to eq('removed')
63
- end
64
- end
data/spec/spec_helper.rb DELETED
@@ -1,4 +0,0 @@
1
- require 'rubygems'
2
- require 'bundler'
3
-
4
- Bundler.require :default, :development