interpol 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile CHANGED
@@ -5,6 +5,7 @@ gemspec
5
5
 
6
6
  group :extras do
7
7
  gem 'debugger' if defined?(RUBY_ENGINE) && RUBY_ENGINE == 'ruby' && RUBY_VERSION == '1.9.3'
8
+ gem 'ruby-debug', :platform => :mri_18
8
9
  end
9
10
 
10
11
  gem 'json-jruby', :platform => 'jruby'
@@ -33,7 +33,7 @@ module Interpol
33
33
 
34
34
  # Public: Defines interpol configuration.
35
35
  class Configuration
36
- attr_reader :endpoint_definition_files, :endpoints
36
+ attr_reader :endpoint_definition_files, :endpoints, :filter_example_data_blocks
37
37
  attr_accessor :validation_mode, :documentation_title, :endpoint_definition_merge_key_files
38
38
 
39
39
  def initialize
@@ -41,6 +41,7 @@ module Interpol
41
41
  self.endpoint_definition_merge_key_files = []
42
42
  self.documentation_title = "API Documentation Provided by Interpol"
43
43
  register_default_callbacks
44
+ @filter_example_data_blocks = []
44
45
 
45
46
  yield self if block_given?
46
47
  end
@@ -85,6 +86,10 @@ module Interpol
85
86
  execution_context.instance_exec(*args, &@unavailable_request_version_block)
86
87
  end
87
88
 
89
+ def filter_example_data(&block)
90
+ filter_example_data_blocks << block
91
+ end
92
+
88
93
  def self.default
89
94
  @default ||= Configuration.new
90
95
  end
@@ -1,6 +1,27 @@
1
1
  require 'json-schema'
2
2
  require 'interpol/errors'
3
3
 
4
+ module JSON
5
+ # The JSON-schema namespace
6
+ class Schema
7
+ # Monkey patch json-schema to reject unrecognized types.
8
+ # It allows them because the spec says they should be allowed,
9
+ # but we don't want to allow them.
10
+ # For more info, see:
11
+ # - https://github.com/hoxworth/json-schema/pull/37
12
+ # - https://github.com/hoxworth/json-schema/pull/38
13
+ class TypeAttribute
14
+ (class << self; self; end).class_eval do
15
+ alias original_data_valid_for_type? data_valid_for_type?
16
+ def data_valid_for_type?(data, type)
17
+ return false unless TYPE_CLASS_MAPPINGS.has_key?(type)
18
+ original_data_valid_for_type?(data, type)
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
24
+
4
25
  module Interpol
5
26
  module HashFetcher
6
27
  # Unfortunately, on JRuby 1.9, the error raised from Hash#fetch when
@@ -107,7 +128,8 @@ module Interpol
107
128
  # Provides the means to validate data against that version of the schema.
108
129
  class EndpointDefinition
109
130
  include HashFetcher
110
- attr_reader :endpoint_name, :message_type, :version, :schema, :examples
131
+ attr_reader :endpoint_name, :message_type, :version, :schema,
132
+ :path_params, :query_params, :examples
111
133
 
112
134
  def initialize(endpoint_name, version, message_type, definition)
113
135
  @endpoint_name = endpoint_name
@@ -115,6 +137,8 @@ module Interpol
115
137
  @status_codes = StatusCodeMatcher.new(definition['status_codes'])
116
138
  @version = version
117
139
  @schema = fetch_from(definition, 'schema')
140
+ @path_params = definition.fetch('path_params', {})
141
+ @query_params = definition.fetch('query_params', {})
118
142
  @examples = fetch_from(definition, 'examples').map { |e| EndpointExample.new(e, self) }
119
143
  make_schema_strict!(@schema)
120
144
  end
@@ -212,5 +236,47 @@ module Interpol
212
236
  def validate!
213
237
  definition.validate_data!(data)
214
238
  end
239
+
240
+ def apply_filters(filter_blocks, request_env)
241
+ deep_dup.tap do |example|
242
+ filter_blocks.each do |filter|
243
+ filter.call(example, request_env)
244
+ end
245
+ end
246
+ end
247
+
248
+ protected
249
+
250
+ attr_writer :data
251
+
252
+ private
253
+
254
+ def deep_dup
255
+ dup.tap { |d| d.data = dup_hash(d.data) }
256
+ end
257
+
258
+ DUPPERS = { Hash => :dup_hash, Array => :dup_array }
259
+
260
+ def dup_hash(hash)
261
+ duplicate = hash.dup
262
+ duplicate.each_pair do |k,v|
263
+ duplicate[k] = dup_object(v)
264
+ end
265
+ duplicate
266
+ end
267
+
268
+ def dup_array(array)
269
+ duplicate = array.dup
270
+ duplicate.each_with_index do |o, index|
271
+ duplicate[index] = dup_object(o)
272
+ end
273
+ duplicate
274
+ end
275
+
276
+ def dup_object(o)
277
+ dupper = DUPPERS[o.class]
278
+ return o unless dupper
279
+ send(dupper, o)
280
+ end
215
281
  end
216
282
  end
@@ -19,9 +19,11 @@ module Interpol
19
19
  end
20
20
 
21
21
  def example_for(endpoint, version, message_type)
22
- endpoint.find_example_for!(version, message_type)
22
+ example = endpoint.find_example_for!(version, message_type)
23
23
  rescue ArgumentError
24
24
  interpol_config.request_version_unavailable(self, version, endpoint.available_versions)
25
+ else
26
+ example.apply_filters(interpol_config.filter_example_data_blocks, request.env)
25
27
  end
26
28
  end
27
29
 
@@ -1,3 +1,3 @@
1
1
  module Interpol
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: interpol
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-06-12 00:00:00.000000000 Z
12
+ date: 2012-07-02 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: json-schema
@@ -18,7 +18,7 @@ dependencies:
18
18
  requirements:
19
19
  - - ~>
20
20
  - !ruby/object:Gem::Version
21
- version: 1.0.5
21
+ version: 1.0.8
22
22
  type: :runtime
23
23
  prerelease: false
24
24
  version_requirements: !ruby/object:Gem::Requirement
@@ -26,7 +26,7 @@ dependencies:
26
26
  requirements:
27
27
  - - ~>
28
28
  - !ruby/object:Gem::Version
29
- version: 1.0.5
29
+ version: 1.0.8
30
30
  - !ruby/object:Gem::Dependency
31
31
  name: nokogiri
32
32
  requirement: !ruby/object:Gem::Requirement
@@ -218,7 +218,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
218
218
  version: '0'
219
219
  segments:
220
220
  - 0
221
- hash: -430941373413258822
221
+ hash: -810990702936122317
222
222
  required_rubygems_version: !ruby/object:Gem::Requirement
223
223
  none: false
224
224
  requirements:
@@ -227,7 +227,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
227
227
  version: '0'
228
228
  segments:
229
229
  - 0
230
- hash: -430941373413258822
230
+ hash: -810990702936122317
231
231
  requirements: []
232
232
  rubyforge_project:
233
233
  rubygems_version: 1.8.24