apiculture 0.0.15 → 0.0.16

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: aef2587d9cd055ecd731cc11364e2660dcf95e9c
4
- data.tar.gz: 9b4820367da19028395a2d9928e1520808d10fad
3
+ metadata.gz: fddefec311625b3874fbcf1f2359f17d9393ae03
4
+ data.tar.gz: 2006215622d1839c0316d1bd1c790b65b08e6fb5
5
5
  SHA512:
6
- metadata.gz: cb167f833039971ee16271a0c48d911fd11eeb3aeddfc130bdd31773b63f60e670d4901f0b21cd6ad4357bd499e78243a58c5c07532b4a36685e56e5c0156216
7
- data.tar.gz: 49b05ae437ad28f58d132534b2b7b07c5b768e60cc60e53b9ab5fd85c4fabccf2f9bcc49c8cb5c7867daa2de1b940a7f21d00debccd3c19b315781966f792f76
6
+ metadata.gz: 3933f460a2b5b2cd1ea3187d8b4023851efc82ad5b555b55decb0922751f198336998f59d16262ca522f2ef097c26e6978a063be122b842bf1ecd6977b7bacff
7
+ data.tar.gz: b8f2c0ed089a32b45b5edeb98aea9fe1110b07a92f3df5aa0497c24ccb61e602fcfc547d1b4a0d011a631ca5b871c3ab198569dee3ec04b6b78538392e1ce1fd
data/.travis.yml CHANGED
@@ -1,5 +1,6 @@
1
1
  rvm:
2
2
  - 2.1.5
3
3
  - 2.2.2
4
+ - 2.3.0
4
5
  sudo: false
5
6
  cache: bundler
data/apiculture.gemspec CHANGED
@@ -2,16 +2,16 @@
2
2
  # DO NOT EDIT THIS FILE DIRECTLY
3
3
  # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
4
  # -*- encoding: utf-8 -*-
5
- # stub: apiculture 0.0.15 ruby lib
5
+ # stub: apiculture 0.0.16 ruby lib
6
6
 
7
7
  Gem::Specification.new do |s|
8
8
  s.name = "apiculture"
9
- s.version = "0.0.15"
9
+ s.version = "0.0.16"
10
10
 
11
11
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
12
12
  s.require_paths = ["lib"]
13
13
  s.authors = ["Julik Tarkhanov", "WeTransfer"]
14
- s.date = "2016-06-06"
14
+ s.date = "2016-09-27"
15
15
  s.description = "A toolkit for building REST APIs on top of Sinatra"
16
16
  s.email = "me@julik.nl"
17
17
  s.extra_rdoc_files = [
@@ -43,7 +43,7 @@ Gem::Specification.new do |s|
43
43
  ]
44
44
  s.homepage = "https://github.com/WeTransfer/apiculture"
45
45
  s.licenses = ["MIT"]
46
- s.rubygems_version = "2.2.2"
46
+ s.rubygems_version = "2.5.1"
47
47
  s.summary = "Sweet API sauce on top of Sintra"
48
48
 
49
49
  if s.respond_to? :specification_version then
data/lib/apiculture.rb CHANGED
@@ -23,14 +23,14 @@ module Apiculture
23
23
  }
24
24
 
25
25
  AC_CHECK_TYPE_PROC = ->(param, value) {
26
- value.is_a?(param.ruby_type) or raise ParameterTypeMismatch.new(param, value.class)
26
+ param.matchable === value or raise ParameterTypeMismatch.new(param, value.class)
27
27
  }
28
28
 
29
29
  AC_PERMIT_PROC = ->(maybe_strong_params, param_name) {
30
30
  maybe_strong_params.permit(param_name) if maybe_strong_params.respond_to?(:permit)
31
31
  }
32
32
 
33
- class Parameter < Struct.new(:name, :description, :required, :ruby_type, :cast_proc_or_method)
33
+ class Parameter < Struct.new(:name, :description, :required, :matchable, :cast_proc_or_method)
34
34
  # Return Strings since Sinatra prefers string keys for params{}
35
35
  def name_as_string; name.to_s; end
36
36
  end
@@ -103,24 +103,24 @@ module Apiculture
103
103
  end
104
104
 
105
105
  # Add an optional parameter for the API call
106
- def param(name, description, ruby_type, cast: IDENTITY_PROC)
106
+ def param(name, description, matchable, cast: IDENTITY_PROC)
107
107
  @apiculture_action_definition ||= ActionDefinition.new
108
- @apiculture_action_definition.parameters << Parameter.new(name, description, required=false, ruby_type, cast)
108
+ @apiculture_action_definition.parameters << Parameter.new(name, description, required=false, matchable, cast)
109
109
  end
110
110
 
111
111
  # Add a requred parameter for the API call
112
- def required_param(name, description, ruby_type, cast: IDENTITY_PROC)
112
+ def required_param(name, description, matchable, cast: IDENTITY_PROC)
113
113
  @apiculture_action_definition ||= ActionDefinition.new
114
- @apiculture_action_definition.parameters << Parameter.new(name, description, required=true, ruby_type, cast)
114
+ @apiculture_action_definition.parameters << Parameter.new(name, description, required=true, matchable, cast)
115
115
  end
116
116
 
117
117
  # Describe a parameter that has to be included in the URL of the API call.
118
118
  # Route parameters are always required, and all the parameters specified
119
119
  # using +route_param+ should also be included in the path given for the route
120
120
  # definition
121
- def route_param(name, description, ruby_type = String, cast: IDENTITY_PROC)
121
+ def route_param(name, description, matchable = String, cast: IDENTITY_PROC)
122
122
  @apiculture_action_definition ||= ActionDefinition.new
123
- @apiculture_action_definition.route_parameters << RouteParameter.new(name, description, required=false, ruby_type, cast)
123
+ @apiculture_action_definition.route_parameters << RouteParameter.new(name, description, required=false, matchable, cast)
124
124
  end
125
125
 
126
126
  # Add a possible response, specifying the code and the JSON Response by example.
@@ -147,9 +147,9 @@ module Apiculture
147
147
  # Gets raised when a parameter is supplied and has a wrong type
148
148
  class ParameterTypeMismatch < ValidationError
149
149
  def initialize(ac_parameter, received_ruby_type)
150
- parameter_name, expected_type = ac_parameter.name, ac_parameter.ruby_type
150
+ parameter_name, expected_type = ac_parameter.name, ac_parameter.matchable
151
151
  received_type = received_ruby_type
152
- super "Received #{received_type}, expected #{expected_type} for :#{parameter_name}"
152
+ super "Received #{received_type}, expected #{expected_type.inspect} for :#{parameter_name}"
153
153
  end
154
154
  end
155
155
 
@@ -132,7 +132,7 @@ class Apiculture::MethodDocumentation
132
132
  html.tr do
133
133
  html.td { html.tt(param.name.to_s) }
134
134
  html.td(param.required ? 'Yes' : 'No')
135
- html.td(param.ruby_type.to_s)
135
+ html.td(param.matchable.inspect)
136
136
  html.td(param.description.to_s)
137
137
  end
138
138
  end
@@ -1,3 +1,3 @@
1
1
  module Apiculture
2
- VERSION = '0.0.15'
2
+ VERSION = '0.0.16'
3
3
  end
@@ -149,7 +149,33 @@ describe "Apiculture" do
149
149
  post '/thing', {number: '123'}
150
150
  }.to raise_error('Received String, expected Integer for :number')
151
151
  end
152
-
152
+
153
+ it 'supports an arbitrary object with === as a type specifier for a parameter' do
154
+ custom_matcher = Class.new do
155
+ def ===(value)
156
+ value == "Magic word"
157
+ end
158
+ end.new
159
+
160
+ @app_class = Class.new(Sinatra::Base) do
161
+ settings.show_exceptions = false
162
+ settings.raise_errors = true
163
+ extend Apiculture
164
+
165
+ required_param :pretty_please, "Only a magic word will do", custom_matcher
166
+ api_method :post, '/thing' do
167
+ 'Ohai!'
168
+ end
169
+ end
170
+
171
+ post '/thing', {pretty_please: 'Magic word'}
172
+ expect(last_response).to be_ok
173
+
174
+ expect {
175
+ post '/thing', {pretty_please: 'not the magic word you are looking for'}
176
+ }.to raise_error(Apiculture::ParameterTypeMismatch)
177
+ end
178
+
153
179
  it 'suppresses parameters that are not defined in the action definition' do
154
180
  @app_class = Class.new(Sinatra::Base) do
155
181
  settings.show_exceptions = false
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: apiculture
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.15
4
+ version: 0.0.16
5
5
  platform: ruby
6
6
  authors:
7
7
  - Julik Tarkhanov
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2016-06-06 00:00:00.000000000 Z
12
+ date: 2016-09-27 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: sinatra
@@ -220,7 +220,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
220
220
  version: '0'
221
221
  requirements: []
222
222
  rubyforge_project:
223
- rubygems_version: 2.2.2
223
+ rubygems_version: 2.5.1
224
224
  signing_key:
225
225
  specification_version: 4
226
226
  summary: Sweet API sauce on top of Sintra