sliver 0.0.1 → 0.0.2

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 606c72d36976f690024b04140c2c47b4d537ad19
4
- data.tar.gz: 692fbe6fa8ffb13ddcc4a5bd0d0598ede82302ca
3
+ metadata.gz: 20b809608f86609cc795b28a114f3c812dec65e8
4
+ data.tar.gz: 4bf292bffbc5d19991d0fa7e29d2ced1005b6732
5
5
  SHA512:
6
- metadata.gz: b319b0442c56ca705818f2248d1a8aa18fd06e019826dfedf887246e60d745f1f76776ca84840fd593b6b8783bc8962cfa483d18bc9c0ceaeebbc3d09cffec0e
7
- data.tar.gz: 210a95f4674840b127ac01e4d05b37d70434749cefc7dd1bf38656748683692b0a0107f7c777b21fa8233529bb74232844588b4a187ef5446cfacc505909664a
6
+ metadata.gz: 6f33a1f81c5222b877ef334d98f386abc7d1c64bc47d8e97587e533ebfcaefaf7ae0ebb774434ac305b8049d624522121a08c154eb893cb8de13432b3fd7cc96
7
+ data.tar.gz: c2496802b022d43a351ef992c38188f61f2f170e8ad52abe1c4f3e562a6012c332f2521fd76c29085ea418f884ce2f449cd4d1b22827b262d1c877cc521cad53
data/README.md CHANGED
@@ -2,12 +2,16 @@
2
2
 
3
3
  A super simple, extendable Rack API.
4
4
 
5
+ [![Build Status](https://travis-ci.org/pat/sliver.svg?branch=master)](https://travis-ci.org/pat/sliver)
6
+
7
+ Early days of development, so things may change dramatically. Or not. Who knows.
8
+
5
9
  ## Installation
6
10
 
7
11
  Add it to your Gemfile like any other gem, or install it manually.
8
12
 
9
13
  ```ruby
10
- gem 'sliver', '~> 0.0.1'
14
+ gem 'sliver', '~> 0.0.2'
11
15
  ```
12
16
 
13
17
  ## Usage
@@ -52,7 +56,7 @@ class ChangeAction
52
56
  response.body = [
53
57
  "How dare the Premier ignore my invitations?",
54
58
  "He'll have to go",
55
- "So, to the bunch he luncheons with",
59
+ "So too the bunch he luncheons with",
56
60
  "It's second on my list of things to do"
57
61
  ]
58
62
 
data/lib/sliver/action.rb CHANGED
@@ -19,4 +19,8 @@ module Sliver::Action
19
19
  private
20
20
 
21
21
  attr_reader :environment, :response
22
+
23
+ def request
24
+ @request ||= Rack::Request.new environment
25
+ end
22
26
  end
data/lib/sliver/api.rb CHANGED
@@ -1,8 +1,10 @@
1
1
  class Sliver::API
2
+ NOT_FOUND = lambda { |environment| [404, {}, ['Not Found']] }
3
+
2
4
  attr_accessor :path
3
5
 
4
6
  def initialize(&block)
5
- @endpoints = {}
7
+ @endpoints = Hash.new { |hash, key| hash[key] = Sliver::Endpoints.new }
6
8
  @path = ''
7
9
 
8
10
  block.call self
@@ -11,16 +13,15 @@ class Sliver::API
11
13
  def call(environment)
12
14
  method = environment['REQUEST_METHOD']
13
15
  path_info = environment['PATH_INFO'].gsub(/\A#{path}/, '')
14
- endpoint = endpoints[method][path_info]
16
+ endpoint = endpoints[method].find(path_info) || NOT_FOUND
15
17
 
16
- endpoint ? endpoint.call(environment) : [404, {}, ['Not Found']]
18
+ endpoint.call environment
17
19
  end
18
20
 
19
21
  def connect(method, path, action)
20
22
  method = method.to_s.upcase
21
23
 
22
- endpoints[method] ||= {}
23
- endpoints[method][path] = action
24
+ endpoints[method].append path, action
24
25
  end
25
26
 
26
27
  private
@@ -0,0 +1,21 @@
1
+ class Sliver::Endpoints
2
+ def initialize
3
+ @paths = {}
4
+ end
5
+
6
+ def append(path, action)
7
+ paths[path] = action
8
+ end
9
+
10
+ def find(path)
11
+ key = paths.keys.detect { |key|
12
+ key.is_a?(String) ? (key == path) : path[/\A#{key}\z/]
13
+ }
14
+
15
+ key && paths[key]
16
+ end
17
+
18
+ private
19
+
20
+ attr_reader :paths
21
+ end
data/lib/sliver.rb CHANGED
@@ -4,4 +4,5 @@ end
4
4
 
5
5
  require 'sliver/action'
6
6
  require 'sliver/api'
7
+ require 'sliver/endpoints'
7
8
  require 'sliver/response'
data/sliver.gemspec CHANGED
@@ -1,7 +1,7 @@
1
1
  # coding: utf-8
2
2
  Gem::Specification.new do |spec|
3
3
  spec.name = 'sliver'
4
- spec.version = '0.0.1'
4
+ spec.version = '0.0.2'
5
5
  spec.authors = ['Pat Allan']
6
6
  spec.email = ['pat@freelancing-gods.com']
7
7
  spec.summary = %q{Lightweight, simple Rack APIs}
@@ -19,12 +19,22 @@ class EchoAction
19
19
  end
20
20
  end
21
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
+
22
31
  describe 'Class-based Sliver API' do
23
32
  include Rack::Test::Methods
24
33
 
25
34
  let(:app) { Sliver::API.new do |api|
26
- api.connect :get, '/', GetAction
27
- api.connect :put, '/echo', EchoAction
35
+ api.connect :get, '/', GetAction
36
+ api.connect :put, '/echo', EchoAction
37
+ api.connect :get, '/addition', AdditionAction
28
38
  end }
29
39
 
30
40
  it 'constructs responses' do
@@ -40,4 +50,10 @@ describe 'Class-based Sliver API' do
40
50
 
41
51
  expect(last_response.body).to eq('baz')
42
52
  end
53
+
54
+ it 'allows use of request' do
55
+ get '/addition', 'a' => '5', 'b' => '3'
56
+
57
+ expect(last_response.body).to eq('8')
58
+ end
43
59
  end
@@ -15,6 +15,10 @@ describe 'Basic Sliver API' do
15
15
  api.connect :post, '/', lambda { |environment|
16
16
  [200, {'Content-Type' => 'text/plain'}, ['qux']]
17
17
  }
18
+
19
+ api.connect :delete, %r{/remove/\d+}, lambda { |environment|
20
+ [200, {}, ['removed']]
21
+ }
18
22
  end }
19
23
 
20
24
  it 'responds to GET requests' do
@@ -43,6 +47,13 @@ describe 'Basic Sliver API' do
43
47
  expect(last_response.status).to eq(404)
44
48
  expect(last_response.body).to eq('Not Found')
45
49
  end
50
+
51
+ it 'matches against regular expressions' do
52
+ delete '/remove/141'
53
+
54
+ expect(last_response.status).to eq(200)
55
+ expect(last_response.body).to eq('removed')
56
+ end
46
57
  end
47
58
 
48
59
  describe 'Basic lambda API with a path prefix' do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sliver
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Pat Allan
@@ -68,6 +68,7 @@ files:
68
68
  - lib/sliver.rb
69
69
  - lib/sliver/action.rb
70
70
  - lib/sliver/api.rb
71
+ - lib/sliver/endpoints.rb
71
72
  - lib/sliver/response.rb
72
73
  - sliver.gemspec
73
74
  - spec/acceptance/class_api_spec.rb