haveapi 0.11.1 → 0.12.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (37) hide show
  1. checksums.yaml +5 -5
  2. data/CHANGELOG +2 -0
  3. data/Gemfile +2 -0
  4. data/haveapi.gemspec +5 -5
  5. data/lib/haveapi/action.rb +6 -5
  6. data/lib/haveapi/actions/default.rb +3 -0
  7. data/lib/haveapi/authentication/basic/provider.rb +2 -0
  8. data/lib/haveapi/authentication/token/provider.rb +2 -0
  9. data/lib/haveapi/authentication/token/resources.rb +3 -0
  10. data/lib/haveapi/client_examples/curl.rb +1 -0
  11. data/lib/haveapi/client_examples/fs_client.rb +2 -0
  12. data/lib/haveapi/client_examples/http.rb +1 -0
  13. data/lib/haveapi/client_examples/js_client.rb +2 -0
  14. data/lib/haveapi/client_examples/php_client.rb +2 -0
  15. data/lib/haveapi/client_examples/ruby_cli.rb +1 -0
  16. data/lib/haveapi/client_examples/ruby_client.rb +1 -0
  17. data/lib/haveapi/example.rb +56 -3
  18. data/lib/haveapi/example_list.rb +28 -0
  19. data/lib/haveapi/extensions/action_exceptions.rb +2 -0
  20. data/lib/haveapi/extensions/exception_mailer.rb +1 -0
  21. data/lib/haveapi/model_adapters/active_record.rb +2 -0
  22. data/lib/haveapi/model_adapters/hash.rb +2 -0
  23. data/lib/haveapi/output_formatters/json.rb +2 -0
  24. data/lib/haveapi/resource.rb +2 -0
  25. data/lib/haveapi/resources/action_state.rb +2 -0
  26. data/lib/haveapi/server.rb +7 -1
  27. data/lib/haveapi/validators/acceptance.rb +2 -0
  28. data/lib/haveapi/validators/confirmation.rb +2 -0
  29. data/lib/haveapi/validators/custom.rb +2 -0
  30. data/lib/haveapi/validators/exclusion.rb +2 -0
  31. data/lib/haveapi/validators/format.rb +2 -0
  32. data/lib/haveapi/validators/inclusion.rb +2 -0
  33. data/lib/haveapi/validators/length.rb +2 -0
  34. data/lib/haveapi/validators/numericality.rb +7 -5
  35. data/lib/haveapi/validators/presence.rb +2 -0
  36. data/lib/haveapi/version.rb +1 -1
  37. metadata +12 -11
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 2910cb58bad5471ca8abb70e980ab138519bfc21
4
- data.tar.gz: 70283cec7cbbe98d68de0078dd6f2c43149dabcd
2
+ SHA256:
3
+ metadata.gz: cdbc87bdee886f2d6c0063cb3cb4ce06bb42431508ba88ae9de7375432c642bb
4
+ data.tar.gz: ca75c1bbaed366ef28c8605d6c1f80c204066cdb7dc09a2e44e4e0a08bb0b52b
5
5
  SHA512:
6
- metadata.gz: 2b2d482ded9ef0c1bf44f74245a170d068eb51d298d2eadcbf85d9f4853d305c98788e581f2de37567586e5612383cfe5350cd4aab37827db75ae2cd02e5ef03
7
- data.tar.gz: 7a5b2316291159eaca2ff05871213e2153cd0732b339699d26c00b1dd525d99129965457c682c61beedae0cc9661512de13a09f8168847b7bee29783c141a8bb
6
+ metadata.gz: c41b7a81996a2cde3e59b813e9d59d0494338f17f1a67319da23c9aa801c4e79459af8d62c5ac1f76796efd9168c4640e397cc25385e2bcd673ef53a931593c2
7
+ data.tar.gz: b8a884d61fbe5a2b683f557de99d1c0c6514e16db2666c010e0e9a110a1c41ed0787cee151dbdf1ab939c94d19cde183c390bc4a98aa6fd5d781880fb82c910d
data/CHANGELOG CHANGED
@@ -1,3 +1,5 @@
1
+ * See ../../CHANGELOG.md for newer versions
2
+
1
3
  * Wed Mar 14 2018 - version 0.11.1
2
4
  - Downgrade require_all to v1.5
3
5
 
data/Gemfile CHANGED
@@ -1,6 +1,8 @@
1
1
  source 'https://rubygems.org'
2
2
  gemspec
3
3
 
4
+ gem 'haveapi-client', path: '../../clients/ruby'
5
+
4
6
  group :test do
5
7
  gem 'rspec'
6
8
  gem 'rack-test'
data/haveapi.gemspec CHANGED
@@ -5,7 +5,7 @@ require 'haveapi/version'
5
5
  Gem::Specification.new do |s|
6
6
  s.name = 'haveapi'
7
7
  s.version = HaveAPI::VERSION
8
- s.date = '2018-03-14'
8
+ s.date = '2017-11-27'
9
9
  s.summary =
10
10
  s.description = 'Framework for creating self-describing APIs'
11
11
  s.authors = 'Jakub Skokan'
@@ -15,15 +15,15 @@ Gem::Specification.new do |s|
15
15
 
16
16
  s.required_ruby_version = '>= 2.0.0'
17
17
 
18
- s.add_runtime_dependency 'require_all', '~> 1.5.0'
18
+ s.add_runtime_dependency 'require_all', '~> 2.0.0'
19
19
  s.add_runtime_dependency 'json'
20
20
  s.add_runtime_dependency 'activesupport', '>= 4.0'
21
- s.add_runtime_dependency 'sinatra', '~> 1.4'
22
- s.add_runtime_dependency 'tilt', '~> 1.4'
21
+ s.add_runtime_dependency 'sinatra', '~> 2.0.5'
22
+ s.add_runtime_dependency 'tilt', '~> 2.0.9'
23
23
  s.add_runtime_dependency 'redcarpet', '~> 3.4'
24
24
  s.add_runtime_dependency 'rake'
25
25
  s.add_runtime_dependency 'github-markdown'
26
26
  s.add_runtime_dependency 'nesty', '~> 1.0'
27
- s.add_runtime_dependency 'haveapi-client', '~> 0.10.0'
27
+ s.add_runtime_dependency 'haveapi-client', '~> 0.12.0'
28
28
  s.add_runtime_dependency 'mail'
29
29
  end
@@ -1,3 +1,7 @@
1
+ require 'haveapi/common'
2
+ require 'haveapi/hooks'
3
+ require 'haveapi/metadata'
4
+
1
5
  module HaveAPI
2
6
  class Action < Common
3
7
  obj_type :action
@@ -167,7 +171,7 @@ module HaveAPI
167
171
  end
168
172
 
169
173
  def example(title = '', &block)
170
- @examples ||= []
174
+ @examples ||= ExampleList.new
171
175
  e = Example.new(title)
172
176
  e.instance_eval(&block)
173
177
  @examples << e
@@ -209,7 +213,7 @@ module HaveAPI
209
213
  input: @input ? @input.describe(context) : {parameters: {}},
210
214
  output: @output ? @output.describe(context) : {parameters: {}},
211
215
  meta: @meta ? @meta.merge(@meta) { |_, v| v && v.describe(context) } : nil,
212
- examples: @examples ? @examples.map { |e| e.describe } : [],
216
+ examples: @examples ? @examples.describe(context) : [],
213
217
  url: context.resolved_url,
214
218
  method: route_method,
215
219
  help: "#{context.url}?method=#{route_method}"
@@ -547,9 +551,6 @@ module HaveAPI
547
551
  self.class.model_adapter(self.class.input.layout).input(@safe_params[input.namespace]))
548
552
  end
549
553
 
550
- # Remove duplicit key
551
- @safe_params.delete(input.namespace.to_s)
552
-
553
554
  # Now check required params, convert types and set defaults
554
555
  input.validate(@safe_params)
555
556
  end
@@ -1,3 +1,6 @@
1
+ require 'haveapi/action'
2
+ require 'haveapi/actions/paginable'
3
+
1
4
  module HaveAPI
2
5
  module Actions
3
6
  module Default
@@ -1,3 +1,5 @@
1
+ require 'haveapi/authentication/base'
2
+
1
3
  module HaveAPI::Authentication
2
4
  module Basic
3
5
  # HTTP basic authentication provider.
@@ -1,3 +1,5 @@
1
+ require 'haveapi/authentication/base'
2
+
1
3
  module HaveAPI::Authentication
2
4
  module Token
3
5
  # Exception that has to be raised when generated token already exists.
@@ -1,3 +1,6 @@
1
+ require 'haveapi/resource'
2
+ require 'haveapi/action'
3
+
1
4
  module HaveAPI::Authentication::Token
2
5
  module Resources
3
6
  class Token < HaveAPI::Resource
@@ -1,4 +1,5 @@
1
1
  require 'pp'
2
+ require 'haveapi/client_examples/http'
2
3
 
3
4
  module HaveAPI::ClientExamples
4
5
  class Curl < Http
@@ -1,3 +1,5 @@
1
+ require 'haveapi/client_example'
2
+
1
3
  module HaveAPI::ClientExamples
2
4
  class FsClient < HaveAPI::ClientExample
3
5
  label 'File system'
@@ -1,6 +1,7 @@
1
1
  require 'pp'
2
2
  require 'cgi'
3
3
  require 'rack/utils'
4
+ require 'haveapi/client_example'
4
5
 
5
6
  module HaveAPI::ClientExamples
6
7
  class Http < HaveAPI::ClientExample
@@ -1,3 +1,5 @@
1
+ require 'haveapi/client_example'
2
+
1
3
  module HaveAPI::ClientExamples
2
4
  class JsClient < HaveAPI::ClientExample
3
5
  label 'JavaScript'
@@ -1,3 +1,5 @@
1
+ require 'haveapi/client_example'
2
+
1
3
  module HaveAPI::ClientExamples
2
4
  class PhpClient < HaveAPI::ClientExample
3
5
  label 'PHP'
@@ -3,6 +3,7 @@ module HaveAPI
3
3
  end
4
4
 
5
5
  require 'haveapi/cli/output_formatter'
6
+ require 'haveapi/client_example'
6
7
 
7
8
  module HaveAPI::ClientExamples
8
9
  class RubyCli < HaveAPI::ClientExample
@@ -1,4 +1,5 @@
1
1
  require 'pp'
2
+ require 'haveapi/client_example'
2
3
 
3
4
  module HaveAPI::ClientExamples
4
5
  class RubyClient < HaveAPI::ClientExample
@@ -4,6 +4,10 @@ module HaveAPI
4
4
  @title = title
5
5
  end
6
6
 
7
+ def authorize(&block)
8
+ @authorization = block
9
+ end
10
+
7
11
  def url_params(*params)
8
12
  @url_params = params
9
13
  end
@@ -36,20 +40,29 @@ module HaveAPI
36
40
  @comment = str
37
41
  end
38
42
 
43
+ def authorized?(context)
44
+ if (context.endpoint || context.current_user) \
45
+ && @authorization && !@authorization.call(context.current_user)
46
+ false
47
+ else
48
+ true
49
+ end
50
+ end
51
+
39
52
  def provided?
40
53
  instance_variables.detect do |v|
41
54
  instance_variable_get(v)
42
55
  end ? true : false
43
56
  end
44
57
 
45
- def describe
58
+ def describe(context)
46
59
  if provided?
47
60
  {
48
61
  title: @title,
49
62
  comment: @comment,
50
63
  url_params: @url_params,
51
- request: @request,
52
- response: @response,
64
+ request: filter_input_params(context, @request),
65
+ response: filter_output_params(context, @response),
53
66
  status: @status.nil? ? true : @status,
54
67
  message: @message,
55
68
  errors: @errors,
@@ -59,5 +72,45 @@ module HaveAPI
59
72
  {}
60
73
  end
61
74
  end
75
+
76
+ protected
77
+ def filter_input_params(context, input)
78
+ case context.action.input.layout
79
+ when :object, :hash
80
+ context.authorization.filter_input(
81
+ context.action.input.params,
82
+ ModelAdapters::Hash.output(context, input),
83
+ )
84
+
85
+ when :object_list, :hash_list
86
+ input.map do |obj|
87
+ context.authorization.filter_input(
88
+ context.action.input.params,
89
+ ModelAdapters::Hash.output(context, obj),
90
+ true
91
+ )
92
+ end
93
+ end
94
+ end
95
+
96
+ def filter_output_params(context, output)
97
+ case context.action.output.layout
98
+ when :object, :hash
99
+ context.authorization.filter_output(
100
+ context.action.output.params,
101
+ ModelAdapters::Hash.output(context, output),
102
+ true
103
+ )
104
+
105
+ when :object_list, :hash_list
106
+ output.map do |obj|
107
+ context.authorization.filter_output(
108
+ context.action.output.params,
109
+ ModelAdapters::Hash.output(context, obj),
110
+ true
111
+ )
112
+ end
113
+ end
114
+ end
62
115
  end
63
116
  end
@@ -0,0 +1,28 @@
1
+ module HaveAPI
2
+ class ExampleList
3
+ def initialize
4
+ @examples = []
5
+ end
6
+
7
+ # @param example [Example]
8
+ def <<(example)
9
+ @examples << example
10
+ end
11
+
12
+ def describe(context)
13
+ ret = []
14
+
15
+ @examples.each do |e|
16
+ ret << e.describe(context) if e.authorized?(context)
17
+ end
18
+
19
+ ret
20
+ end
21
+
22
+ def each(&block)
23
+ @examples.each(&block)
24
+ end
25
+
26
+ include Enumerable
27
+ end
28
+ end
@@ -1,3 +1,5 @@
1
+ require 'haveapi/extensions/base'
2
+
1
3
  module HaveAPI::Extensions
2
4
  class ActionExceptions < Base
3
5
  class << self
@@ -1,5 +1,6 @@
1
1
  require 'net/smtp'
2
2
  require 'mail'
3
+ require 'haveapi/extensions/base'
3
4
 
4
5
  module HaveAPI::Extensions
5
6
  # This extension mails exceptions raised during action execution and description
@@ -1,3 +1,5 @@
1
+ require 'haveapi/model_adapter'
2
+
1
3
  module HaveAPI::ModelAdapters
2
4
  # Adapter for ActiveRecord models.
3
5
  class ActiveRecord < ::HaveAPI::ModelAdapter
@@ -1,3 +1,5 @@
1
+ require 'haveapi/model_adapter'
2
+
1
3
  module HaveAPI::ModelAdapters
2
4
  # Simple hash adapter. Model is just a hash of parameters
3
5
  # and their values.
@@ -1,3 +1,5 @@
1
+ require 'haveapi/output_formatters/base'
2
+
1
3
  module HaveAPI::OutputFormatters
2
4
  class Json < BaseFormatter
3
5
  handle 'application/json'
@@ -1,3 +1,5 @@
1
+ require 'haveapi/common'
2
+
1
3
  module HaveAPI
2
4
  class Resource < Common
3
5
  obj_type :resource
@@ -1,3 +1,5 @@
1
+ require 'haveapi/resource'
2
+
1
3
  module HaveAPI::Resources
2
4
  class ActionState < HaveAPI::Resource
3
5
  desc 'Browse states of blocking actions'
@@ -1,6 +1,8 @@
1
1
  require 'erb'
2
2
  require 'redcarpet'
3
+ require 'tilt'
3
4
  require 'cgi'
5
+ require 'haveapi/hooks'
4
6
 
5
7
  module HaveAPI
6
8
  class Server
@@ -159,6 +161,10 @@ module HaveAPI
159
161
  @root = prefix
160
162
 
161
163
  @sinatra = Sinatra.new do
164
+ # Preload template engine for .md -- without this, tilt will not search
165
+ # for markdown files with extension .md, only .markdown
166
+ Tilt[:md]
167
+
162
168
  set :views, settings.root + '/views'
163
169
  set :public_folder, settings.root + '/public'
164
170
  set :bind, '0.0.0.0'
@@ -269,7 +275,7 @@ module HaveAPI
269
275
  end
270
276
  end
271
277
 
272
- @sinatra.get %r{#{@root}doc/([^\.]+)[\.md]?} do |f|
278
+ @sinatra.get %r{#{@root}doc/([^\.]+)(\.md)?} do |f, _|
273
279
  content_type 'text/html'
274
280
  erb :doc_layout, layout: :main_layout do
275
281
  begin
@@ -1,3 +1,5 @@
1
+ require 'haveapi/validator'
2
+
1
3
  module HaveAPI
2
4
  # Accepts a single configured value.
3
5
  #
@@ -1,3 +1,5 @@
1
+ require 'haveapi/validator'
2
+
1
3
  module HaveAPI
2
4
  # Checks that two parameters are equal or not equal.
3
5
  #
@@ -1,3 +1,5 @@
1
+ require 'haveapi/validator'
2
+
1
3
  module HaveAPI
2
4
  # Custom validator. It has only a short form, taking the description
3
5
  # of the validator. This validator passes every value. It is up to the
@@ -1,3 +1,5 @@
1
+ require 'haveapi/validator'
2
+
1
3
  module HaveAPI
2
4
  # Checks that the value is not reserved.
3
5
  #
@@ -1,3 +1,5 @@
1
+ require 'haveapi/validator'
2
+
1
3
  module HaveAPI
2
4
  # Checks that the value is or is not in specified format.
3
5
  #
@@ -1,3 +1,5 @@
1
+ require 'haveapi/validator'
2
+
1
3
  module HaveAPI
2
4
  # Checks that the value is from given set of allowed values.
3
5
  #
@@ -1,3 +1,5 @@
1
+ require 'haveapi/validator'
2
+
1
3
  module HaveAPI
2
4
  # Checks the length of a string. It does not have a short form.
3
5
  #
@@ -1,3 +1,5 @@
1
+ require 'haveapi/validator'
2
+
1
3
  module HaveAPI
2
4
  # Checks the value is a number or a string containing only digits.
3
5
  #
@@ -8,7 +10,7 @@ module HaveAPI
8
10
  # step: 2,
9
11
  # message: 'the error message'
10
12
  # }
11
- #
13
+ #
12
14
  # Will allow values +3+, +5+, +7+ and +9+.
13
15
  #
14
16
  # string :param, number: {
@@ -16,7 +18,7 @@ module HaveAPI
16
18
  # max: 10,
17
19
  # mod: 2,
18
20
  # }
19
- #
21
+ #
20
22
  # Will allow values +4+, +6+, +8+ and +10+.
21
23
  class Validators::Numericality < Validator
22
24
  name :number
@@ -47,17 +49,17 @@ module HaveAPI
47
49
  msg += '; ' unless msg.empty?
48
50
  msg += "in steps of #{@step}"
49
51
  end
50
-
52
+
51
53
  if @mod
52
54
  msg += '; ' unless msg.empty?
53
55
  msg += "mod #{@step} must equal zero"
54
56
  end
55
-
57
+
56
58
  if @odd
57
59
  msg += '; ' unless msg.empty?
58
60
  msg += "odd"
59
61
  end
60
-
62
+
61
63
  if @even
62
64
  msg += '; ' unless msg.empty?
63
65
  msg += "even"
@@ -1,3 +1,5 @@
1
+ require 'haveapi/validator'
2
+
1
3
  module HaveAPI
2
4
  # Checks the value is present and not empty.
3
5
  #
@@ -1,4 +1,4 @@
1
1
  module HaveAPI
2
2
  PROTOCOL_VERSION = '1.2'
3
- VERSION = '0.11.1'
3
+ VERSION = '0.12.0'
4
4
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: haveapi
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.11.1
4
+ version: 0.12.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jakub Skokan
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-03-14 00:00:00.000000000 Z
11
+ date: 2017-11-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: require_all
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: 1.5.0
19
+ version: 2.0.0
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: 1.5.0
26
+ version: 2.0.0
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: json
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -58,28 +58,28 @@ dependencies:
58
58
  requirements:
59
59
  - - "~>"
60
60
  - !ruby/object:Gem::Version
61
- version: '1.4'
61
+ version: 2.0.5
62
62
  type: :runtime
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
66
  - - "~>"
67
67
  - !ruby/object:Gem::Version
68
- version: '1.4'
68
+ version: 2.0.5
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: tilt
71
71
  requirement: !ruby/object:Gem::Requirement
72
72
  requirements:
73
73
  - - "~>"
74
74
  - !ruby/object:Gem::Version
75
- version: '1.4'
75
+ version: 2.0.9
76
76
  type: :runtime
77
77
  prerelease: false
78
78
  version_requirements: !ruby/object:Gem::Requirement
79
79
  requirements:
80
80
  - - "~>"
81
81
  - !ruby/object:Gem::Version
82
- version: '1.4'
82
+ version: 2.0.9
83
83
  - !ruby/object:Gem::Dependency
84
84
  name: redcarpet
85
85
  requirement: !ruby/object:Gem::Requirement
@@ -142,14 +142,14 @@ dependencies:
142
142
  requirements:
143
143
  - - "~>"
144
144
  - !ruby/object:Gem::Version
145
- version: 0.10.0
145
+ version: 0.12.0
146
146
  type: :runtime
147
147
  prerelease: false
148
148
  version_requirements: !ruby/object:Gem::Requirement
149
149
  requirements:
150
150
  - - "~>"
151
151
  - !ruby/object:Gem::Version
152
- version: 0.10.0
152
+ version: 0.12.0
153
153
  - !ruby/object:Gem::Dependency
154
154
  name: mail
155
155
  requirement: !ruby/object:Gem::Requirement
@@ -207,6 +207,7 @@ files:
207
207
  - lib/haveapi/common.rb
208
208
  - lib/haveapi/context.rb
209
209
  - lib/haveapi/example.rb
210
+ - lib/haveapi/example_list.rb
210
211
  - lib/haveapi/exceptions.rb
211
212
  - lib/haveapi/extensions/action_exceptions.rb
212
213
  - lib/haveapi/extensions/base.rb
@@ -303,7 +304,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
303
304
  version: '0'
304
305
  requirements: []
305
306
  rubyforge_project:
306
- rubygems_version: 2.6.14
307
+ rubygems_version: 2.7.7
307
308
  signing_key:
308
309
  specification_version: 4
309
310
  summary: Framework for creating self-describing APIs