api-extensions 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ YjYxMWQxNDgwYmY0YjJmMDY5OGZhMWZkNzdhNmM0NjkwODAzNzZjMg==
5
+ data.tar.gz: !binary |-
6
+ YzViOWVhNGRkMjljMGI5ODhhZGU1ZjVhMTMwNmNlYWZmOTRhM2RkZQ==
7
+ !binary "U0hBNTEy":
8
+ metadata.gz: !binary |-
9
+ ZjA0NDk1M2VkMGI0YmE4MzFkY2RlYTQzN2IzZWRhNjljNzkxYThmNzgzODBi
10
+ MWMyOWIwYzBjMzhmYjRjMzIwZWQ5MGMyMDU4ZTdhODJkMDc4ZjQ4MTgyZjVj
11
+ YjNjZDIwMjg5ZTc1NjM2MDUzZDQxMWUyNzBmMzU2ZjhkMGQyYmU=
12
+ data.tar.gz: !binary |-
13
+ ZDYxZDQxMWFkODg0NzEwZjI1NmY0NzYzMjllNjBlOTAxNDAxN2UzMDkzMjhl
14
+ NGI0ZjE2NzA1NGY4MmExMDNlYTJjOWM0NWY0NTdhMTg2ZTU5OWUyZWM3ZjIz
15
+ NDBjNmRjODdmZGZhZmU3ZDY3YTIwMDU3YjRhZGI3MWJkYWQ2NGM=
data/.gitignore ADDED
@@ -0,0 +1,18 @@
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
+ .idea
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+
3
+ rvm:
4
+ - 1.9.3
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Nahuel Cuesta Luengo
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,54 @@
1
+ # api-extensions [![Build Status](https://travis-ci.org/ncuesta/api-extensions.png)](https://travis-ci.org/ncuesta/api-extensions) [![Dependency Status](https://gemnasium.com/ncuesta/api-extensions.png)](https://gemnasium.com/ncuesta/api-extensions) [![Code Climate](https://codeclimate.com/github/ncuesta/api-extensions.png)](https://codeclimate.com/github/ncuesta/api-extensions)
2
+
3
+ A collection of extensions for Hypermedia-driven APIs following the standard definitions
4
+ at [the api-doc repo](https://github.com/ncuesta/api-doc).
5
+
6
+ Included extensions:
7
+
8
+ - `fields`
9
+
10
+ ## Installation
11
+
12
+ Add this line to your application's Gemfile:
13
+
14
+ gem 'api-extensions'
15
+
16
+ And then execute:
17
+
18
+ $ bundle
19
+
20
+ Or install it yourself as:
21
+
22
+ $ gem install api-extensions
23
+
24
+ ## Usage
25
+
26
+ ### `fields` extension
27
+
28
+ The behavior of this extension is best described at
29
+ [the api-doc repo](https://github.com/ncuesta/api-doc/blob/master/README.en.md#partial-responses).
30
+
31
+ By including the `Api::Extensions::Fields` module in any class, you will get a `process_fields`
32
+ method that will handle the creation of partial responses - as described in the linked document.
33
+
34
+ ```ruby
35
+ require 'api/extensions/fields'
36
+
37
+ class MyApiHandler
38
+ include Api::Extensions::Fields
39
+
40
+ def handle(request)
41
+ response = fiddle_with request
42
+ process_fields request[:fields], response, request.path
43
+ end
44
+ end
45
+ ```
46
+
47
+
48
+ ## Contributing
49
+
50
+ 1. Fork it
51
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
52
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
53
+ 4. Push to the branch (`git push origin my-new-feature`)
54
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,13 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'cucumber/rake/task'
3
+
4
+
5
+ Cucumber::Rake::Task.new(:features) do |t|
6
+ t.profile = 'default'
7
+ end
8
+
9
+ Cucumber::Rake::Task.new(:"features:html", 'Run Cucumber features and produce HTML output') do |t|
10
+ t.profile = 'html_report'
11
+ end
12
+
13
+ task default: :features
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+
5
+ require 'api/extensions/version'
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = 'api-extensions'
9
+ spec.version = Api::Extensions::VERSION
10
+ spec.authors = ['Nahuel Cuesta Luengo']
11
+ spec.email = ['nahuelcuestaluengo@gmail.com']
12
+ spec.description = %q{Extensions for Hypermedia-driven APIs}
13
+ spec.summary = %q{A collection of extensions for Hypermedia-driven APIs}
14
+ spec.homepage = 'https://github.com/ncuesta/api-extensions'
15
+ spec.license = 'MIT'
16
+
17
+ spec.files = `git ls-files`.split($/)
18
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
19
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
20
+ spec.require_paths = ['lib']
21
+
22
+ spec.add_development_dependency 'bundler', '~> 1.3'
23
+ spec.add_development_dependency 'rake'
24
+ spec.add_development_dependency 'cucumber'
25
+
26
+ spec.add_dependency 'json', '~> 1.8.0'
27
+ end
data/cucumber.yml ADDED
@@ -0,0 +1,2 @@
1
+ default: --format pretty
2
+ html_report: --format progress --format html --out=features_report.html
@@ -0,0 +1,40 @@
1
+ Feature: Fields functional extension
2
+ In order to reduce the size of an API response
3
+ As a client application
4
+ I want to be able to specify which fields I need
5
+
6
+ Scenario Outline: Empty or invalid values
7
+ Given I get a request for resource "/articles/1.json"
8
+ And I get the fields parameter "<value>"
9
+ When I process the fields parameter
10
+ Then the resulting response should be the same as the original one
11
+
12
+ Examples:
13
+ | value |
14
+ | |
15
+ | , |
16
+ | ,, |
17
+ | ,,, |
18
+ | ,,,, |
19
+ | ,,,,, |
20
+
21
+
22
+ Scenario Outline: Specify partial responses
23
+ Given I get a request for resource "/articles/1.json"
24
+ And I get the fields parameter "<value>"
25
+ When I process the fields parameter
26
+ Then the resulting response should include the required fields
27
+ And the resulting response should only contain "<specific_non_required_fields>" as non-required fields
28
+
29
+ Examples:
30
+ | value | specific_non_required_fields |
31
+ | title | title |
32
+ | title,name | title,name |
33
+ | title,name, | title,name |
34
+ | title,name,non_existent_field | title,name |
35
+
36
+ Scenario: A link to the full resource is added to partial responses
37
+ Given I get a request for resource "/articles/1.json"
38
+ And I get the fields parameter "title,name"
39
+ When I process the fields parameter
40
+ Then the resulting response should include a link to the full resource
@@ -0,0 +1,22 @@
1
+ def resources
2
+ {
3
+ '/articles/1.json' => {
4
+ 'id' => 1,
5
+ 'title' => 'Title',
6
+ 'name' => 'Name',
7
+ 'number' => 9,
8
+ 'child' => {
9
+ 'nested' => true
10
+ },
11
+ 'links' => {
12
+ 'self' => { 'href' => '/link/to/self' },
13
+ 'other' => { 'href' => '/link/to/other' }
14
+ }
15
+ }
16
+ }
17
+ end
18
+
19
+ Given /^I get a request for resource "(.*)"$/ do |resource|
20
+ @request_path = resource
21
+ @response = resources[resource]
22
+ end
@@ -0,0 +1,49 @@
1
+ require 'api/extensions/fields'
2
+
3
+ Before do
4
+ @processor = Object.new
5
+ class << @processor
6
+ include(Api::Extensions::Fields)
7
+ end
8
+ end
9
+
10
+ def required_fields
11
+ %w(links)
12
+ end
13
+
14
+
15
+ Given /^I get the fields parameter "(.*)"$/ do |value|
16
+ @fields = value
17
+ end
18
+
19
+ When /^I process the fields parameter$/ do
20
+ @result = @processor.process_fields @fields, @response, @request_path
21
+ end
22
+
23
+ Then /^the resulting response should include the required fields$/ do
24
+ required_fields.each do |field|
25
+ assert_include @result, field, "The field #{field} is not included"
26
+ end
27
+ end
28
+
29
+ Then /^the resulting response should (contain|not contain) all the non-required fields$/ do |contain|
30
+ assert_equal @response.keys, @result.keys, 'At least one non-required field is missing' if contain == 'contain'
31
+ end
32
+
33
+ Then /^the resulting response should only contain "(.*)" as non-required fields$/ do |specific_fields|
34
+ specific_fields = specific_fields.split ','
35
+
36
+ if specific_fields.length > 0
37
+ keys = @result.keys - required_fields
38
+ assert keys.reject { |k| keys.include? k }.empty?, 'Some extra keys were found but not expected'
39
+ end
40
+ end
41
+
42
+ Then(/^the resulting response should be the same as the original one$/) do
43
+ assert_equal @response, @result, 'The response has changed'
44
+ end
45
+
46
+ Then(/^the resulting response should include a link to the full resource$/) do
47
+ assert @response['links'].has_key?('full'), 'The response lacks a "full" link'
48
+ assert_equal @request_path, @response['links']['full']['href'], 'The "full" link is wrong'
49
+ end
@@ -0,0 +1,30 @@
1
+ #
2
+ # Fields extension
3
+ #
4
+ # https://github.com/ncuesta/api-doc/blob/master/README.en.md#partial-responses
5
+ #
6
+ module Api
7
+ module Extensions
8
+ module Fields
9
+ # Process the fields functional extension
10
+ def process_fields(keys, response, full_link = nil)
11
+ fields = parse_fields keys
12
+ response.reject! { |k, v| !fields.include? k } if fields.length > 0
13
+ response['links']['full'] = { 'href' => full_link } unless full_link.nil?
14
+
15
+ response
16
+ end
17
+
18
+ # Parse a string for the fields functional extension
19
+ # - Add the required fields if not present
20
+ def parse_fields(fields_string)
21
+ required_fields + fields_string.split(',')
22
+ end
23
+
24
+ # Get the required fields for any response
25
+ def required_fields
26
+ ['links']
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,5 @@
1
+ module Api
2
+ module Extensions
3
+ VERSION = '0.0.1'
4
+ end
5
+ end
@@ -0,0 +1,7 @@
1
+ require 'api/extensions/fields'
2
+ require 'api/extensions/version'
3
+
4
+ module Api
5
+ module Extensions
6
+ end
7
+ end
metadata ADDED
@@ -0,0 +1,117 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: api-extensions
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Nahuel Cuesta Luengo
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-05-29 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ! '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ! '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: cucumber
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'
55
+ - !ruby/object:Gem::Dependency
56
+ name: json
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: 1.8.0
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ~>
67
+ - !ruby/object:Gem::Version
68
+ version: 1.8.0
69
+ description: Extensions for Hypermedia-driven APIs
70
+ email:
71
+ - nahuelcuestaluengo@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - .gitignore
77
+ - .travis.yml
78
+ - Gemfile
79
+ - LICENSE.txt
80
+ - README.md
81
+ - Rakefile
82
+ - api-extensions.gemspec
83
+ - cucumber.yml
84
+ - features/fields.feature
85
+ - features/step_definitions/common_steps.rb
86
+ - features/step_definitions/fields_steps.rb
87
+ - lib/api/extensions.rb
88
+ - lib/api/extensions/fields.rb
89
+ - lib/api/extensions/version.rb
90
+ homepage: https://github.com/ncuesta/api-extensions
91
+ licenses:
92
+ - MIT
93
+ metadata: {}
94
+ post_install_message:
95
+ rdoc_options: []
96
+ require_paths:
97
+ - lib
98
+ required_ruby_version: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - ! '>='
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ required_rubygems_version: !ruby/object:Gem::Requirement
104
+ requirements:
105
+ - - ! '>='
106
+ - !ruby/object:Gem::Version
107
+ version: '0'
108
+ requirements: []
109
+ rubyforge_project:
110
+ rubygems_version: 2.0.3
111
+ signing_key:
112
+ specification_version: 4
113
+ summary: A collection of extensions for Hypermedia-driven APIs
114
+ test_files:
115
+ - features/fields.feature
116
+ - features/step_definitions/common_steps.rb
117
+ - features/step_definitions/fields_steps.rb