opushon 0.1.1 → 0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (62) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +0 -5
  3. data/.travis.yml +12 -3
  4. data/.yardopts +1 -0
  5. data/CODE_OF_CONDUCT.md +13 -0
  6. data/Gemfile +1 -0
  7. data/LICENSE.md +17 -18
  8. data/README.md +36 -3
  9. data/Rakefile +3 -2
  10. data/VERSION.semver +1 -1
  11. data/bin/console +7 -0
  12. data/bin/setup +5 -0
  13. data/lib/opushon/body.rb +35 -0
  14. data/lib/opushon/option.rb +22 -0
  15. data/lib/opushon/parameter.rb +52 -6
  16. data/lib/opushon/request.rb +19 -0
  17. data/lib/opushon/response.rb +17 -0
  18. data/lib/opushon/restricted_value.rb +4 -22
  19. data/lib/opushon.rb +23 -9
  20. data/opushon.gemspec +10 -6
  21. metadata +65 -68
  22. data/.coveralls.yml +0 -1
  23. data/.ruby-version +0 -1
  24. data/lib/opushon/error/base_error.rb +0 -6
  25. data/lib/opushon/error/min_is_greater_than_max_error.rb +0 -8
  26. data/lib/opushon/error/minlen_is_longer_than_maxlen_error.rb +0 -8
  27. data/lib/opushon/error/syntax_error.rb +0 -8
  28. data/lib/opushon/error/verb_error.rb +0 -8
  29. data/lib/opushon/error.rb +0 -3
  30. data/lib/opushon/instance.rb +0 -34
  31. data/lib/opushon/option_object.rb +0 -36
  32. data/lib/opushon/parameter/base.rb +0 -47
  33. data/lib/opushon/parameter/input.rb +0 -34
  34. data/lib/opushon/parameter/output.rb +0 -10
  35. data/lib/opushon/type/array.rb +0 -10
  36. data/lib/opushon/type/base.rb +0 -25
  37. data/lib/opushon/type/boolean.rb +0 -10
  38. data/lib/opushon/type/hash.rb +0 -10
  39. data/lib/opushon/type/number.rb +0 -29
  40. data/lib/opushon/type/string.rb +0 -32
  41. data/lib/opushon/type.rb +0 -10
  42. data/lib/opushon/verb/base.rb +0 -7
  43. data/lib/opushon/verb/delete.rb +0 -9
  44. data/lib/opushon/verb/get.rb +0 -9
  45. data/lib/opushon/verb/head.rb +0 -9
  46. data/lib/opushon/verb/patch.rb +0 -9
  47. data/lib/opushon/verb/post.rb +0 -9
  48. data/lib/opushon/verb/put.rb +0 -9
  49. data/lib/opushon/verb.rb +0 -9
  50. data/lib/opushon/version.rb +0 -9
  51. data/spec/opushon/parameter/input_spec.rb +0 -161
  52. data/spec/opushon/parameter/output_spec.rb +0 -108
  53. data/spec/opushon/parameter/spec_helper.rb +0 -1
  54. data/spec/opushon/restricted_value_spec.rb +0 -42
  55. data/spec/opushon/spec_helper.rb +0 -1
  56. data/spec/opushon/version_spec.rb +0 -9
  57. data/spec/opushon_spec.rb +0 -343
  58. data/spec/spec_helper.rb +0 -4
  59. data/spec/support/coverage.rb +0 -9
  60. data/spec/support/env.rb +0 -1
  61. data/spec/support/immutable.rb +0 -15
  62. data/spec/support.rb +0 -3
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 04f7e087af188ca2457a822bb5c427898f8f24c1
4
- data.tar.gz: 4a5d0680343e9ce1ee25fdfbb45078fc8249ea7f
3
+ metadata.gz: 12bfcf44e5abe21ffb58e9570189844ac28cf04f
4
+ data.tar.gz: 205212f42af692f349cd84b5047d15e015016690
5
5
  SHA512:
6
- metadata.gz: a34d5465e4665d012a49f3aadcb3135ab53e7a44ae968f656d42541367c4cc80dabd79202c8956eb1b4ff73a4a4143d4d65c824aa8740c1d483270547906871b
7
- data.tar.gz: d69f4bcdc6579feafda7137fc91854d30bf1339d2bdafa5bbf8eef07f61623c1a3d0158318dc0eb611aa968d8509e718cbd3f976c403daaa428583451240b059
6
+ metadata.gz: 4c8e46f50c20d93378ae68efa405a079b23c31cca26269971d3fa1cc010c3c18cf07ec1ed32dd5eaa3ba7c6200b011f8dbf0a44ea06956e8b58788ba8ce0e915
7
+ data.tar.gz: c42a59b46a9b0d48a70ff70e539fbf11799e3dd38aa37ab4b2249cf384f6780fcaad0355181caa0eb3e45ad5b891272166956d1da0b9d695ec83c2c297f25422
data/.gitignore CHANGED
@@ -7,8 +7,3 @@
7
7
  /pkg/
8
8
  /spec/reports/
9
9
  /tmp/
10
- *.bundle
11
- *.so
12
- *.o
13
- *.a
14
- mkmf.log
data/.travis.yml CHANGED
@@ -1,6 +1,15 @@
1
1
  language: ruby
2
- script: 'bundle exec rake test:coverage'
2
+ sudo: false
3
+ cache: bundler
4
+ script: 'bundle exec rake test:coverage --trace'
5
+ before_install:
6
+ - gem install bundler
3
7
  rvm:
4
- - 2.0.0
5
- - 2.1.3
8
+ - 1.9.3
9
+ - 2.0
10
+ - 2.1
11
+ - 2.2
6
12
  - ruby-head
13
+ - jruby
14
+ - jruby-head
15
+ - rbx-2
data/.yardopts ADDED
@@ -0,0 +1 @@
1
+ - README.md
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http:contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile CHANGED
@@ -1,2 +1,3 @@
1
1
  source 'https://rubygems.org'
2
+
2
3
  gemspec
data/LICENSE.md CHANGED
@@ -1,22 +1,21 @@
1
- Copyright (c) 2014 Cyril Wack
1
+ The MIT License (MIT)
2
2
 
3
- MIT License
3
+ Copyright (c) 2014 Cyril Wack
4
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:
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
12
11
 
13
- The above copyright notice and this permission notice shall be
14
- included in all copies or substantial portions of the Software.
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
15
14
 
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.
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md CHANGED
@@ -1,6 +1,24 @@
1
1
  # Opushon
2
2
 
3
- An [Opushon's body](https://github.com/cyril/opushon) parser and emitter.
3
+ [![Build Status](https://travis-ci.org/cyril/opushon.svg?branch=master)](https://travis-ci.org/cyril/opushon)
4
+ [![Dependency Status](https://gemnasium.com/cyril/opushon.svg)](https://gemnasium.com/cyril/opushon)
5
+ [![Gem Version](http://img.shields.io/gem/v/opushon.svg)](https://rubygems.org/gems/opushon)
6
+ [![Inline docs](http://inch-ci.org/github/cyril/opushon.svg?branch=master)](http://inch-ci.org/github/cyril/opushon)
7
+ [![Documentation](http://img.shields.io/:yard-docs-38c800.svg)](http://rubydoc.info/gems/opushon/frames)
8
+
9
+ > An [Opushon's body](https://github.com/cyril/opushon) parser and emitter.
10
+
11
+ ## Contact
12
+
13
+ * Home page: https://github.com/cyril/opushon
14
+ * Bugs/issues: https://github.com/cyril/opushon/issues
15
+ * Support: https://stackoverflow.com/questions/tagged/opushon
16
+
17
+ ## Rubies
18
+
19
+ * [MRI](https://www.ruby-lang.org/)
20
+ * [Rubinius](http://rubini.us/)
21
+ * [JRuby](http://jruby.org/)
4
22
 
5
23
  ## Installation
6
24
 
@@ -22,10 +40,25 @@ Or install it yourself as:
22
40
 
23
41
  ```ruby
24
42
  # Parse some Opushon
25
- Opushon.load('{"DELETE":{"title": "Delete issues","description":"Remove every issues."}}') # => an Opushon instance
43
+ opushon = Opushon.load('{
44
+ "DELETE": {
45
+ "title": "Delete issues",
46
+ "description": "Remove every issues.",
47
+ "request": {
48
+ "headers": {},
49
+ "query_string": {},
50
+ "body": {}
51
+ },
52
+ "response": {
53
+ "headers": {},
54
+ "query_string": {},
55
+ "body": {}
56
+ }
57
+ }
58
+ }') # => an Opushon instance
26
59
 
27
60
  # Emit some Opushon
28
- Opushon.dump(an Opushon instance) # => '{"DELETE":{"title": "Delete issues","description":"Remove every issues."}}'
61
+ Opushon.dump(opushon) # => '{"DELETE":{"title":"Delete issues","description":"Remove every issues.","request":{"headers":{},"query_string":{},"body":{}},"response":{"headers":{},"query_string":{},"body":{}}}}'
29
62
  ```
30
63
 
31
64
  ## Contributing
data/Rakefile CHANGED
@@ -2,7 +2,7 @@ require 'bundler/gem_tasks'
2
2
  require 'rake/testtask'
3
3
 
4
4
  Rake::TestTask.new do |t|
5
- t.pattern = File.join 'spec', '**', '*_spec.rb'
5
+ t.pattern = File.join 'test', '**', 'test_*.rb'
6
6
  t.verbose = true
7
7
  t.warning = true
8
8
  end
@@ -14,4 +14,5 @@ namespace :test do
14
14
  end
15
15
  end
16
16
 
17
- task default: :test
17
+ task(:doc_stats) { ruby '-S yard stats' }
18
+ task default: [:test, :doc_stats]
data/VERSION.semver CHANGED
@@ -1 +1 @@
1
- 0.1.1
1
+ 0.2
data/bin/console ADDED
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'opushon'
5
+
6
+ require 'irb'
7
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,5 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
@@ -0,0 +1,35 @@
1
+ require_relative 'option'
2
+
3
+ module Opushon
4
+ # The body structure is a hash where each key is a HTTP method and each value
5
+ # is a sub-hash, called an option object.
6
+ class Body
7
+ include Virtus.model(strict: true)
8
+
9
+ attribute :GET, Option, required: false
10
+ attribute :PATCH, Option, required: false
11
+ attribute :PUT, Option, required: false
12
+ attribute :POST, Option, required: false
13
+ attribute :DELETE, Option, required: false
14
+
15
+ def to_h
16
+ {
17
+ GET: maybe_to_h(@GET),
18
+ PATCH: maybe_to_h(@PATCH),
19
+ PUT: maybe_to_h(@PUT),
20
+ POST: maybe_to_h(@POST),
21
+ DELETE: maybe_to_h(@DELETE)
22
+ }
23
+ end
24
+
25
+ private
26
+
27
+ def maybe_to_h(object)
28
+ if object.nil?
29
+ nil
30
+ else
31
+ object.to_h
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,22 @@
1
+ require_relative 'request'
2
+ require_relative 'response'
3
+
4
+ module Opushon
5
+ class Option
6
+ include Virtus.model(strict: true)
7
+
8
+ attribute :title, String, default: '', required: true
9
+ attribute :description, String, default: '', required: true
10
+ attribute :request, Request, default: Request.new, required: true
11
+ attribute :response, Response, default: Response.new, required: true
12
+
13
+ def to_h
14
+ {
15
+ title: @title,
16
+ description: @description,
17
+ request: @request.to_h,
18
+ response: @response.to_h
19
+ }
20
+ end
21
+ end
22
+ end
@@ -1,9 +1,55 @@
1
+ require_relative 'restricted_value'
2
+
1
3
  module Opushon
2
- # Namespace for parameters parsing.
3
- module Parameter
4
- end
5
- end
4
+ class Parameter
5
+ include Virtus.model(strict: true)
6
+
7
+ attribute :title, String, default: '', required: true
8
+ attribute :description, String, default: '', required: true
9
+ attribute :type, String, default: 'string', required: true
10
+ attribute :nullifiable, Boolean, default: true, required: true
11
+ attribute :restricted_values, Array[RestrictedValue], required: false
12
+ attribute :example, BasicObject, required: false
13
+ attribute :minlen, Fixnum, required: false
14
+ attribute :maxlen, Fixnum, required: false
15
+ attribute :pattern, Regexp, required: false
16
+ attribute :min, Fixnum, required: false
17
+ attribute :max, Fixnum, required: false
18
+
19
+ def to_h
20
+ h = {
21
+ title: @title,
22
+ description: @description,
23
+ type: @type,
24
+ nullifiable: @nullifiable,
25
+ restricted_values: maybe_to_a(@restricted_values),
26
+ example: @example
27
+ }
6
28
 
7
- Dir[File.join File.dirname(__FILE__), 'parameter', '*.rb'].each do |filename|
8
- require_relative filename
29
+ if @type.eql?('string')
30
+ h.update(
31
+ minlen: @minlen,
32
+ maxlen: @maxlen,
33
+ pattern: (@pattern.nil? ? nil : @pattern.to_s)
34
+ )
35
+ end
36
+
37
+ if @type.eql?('number')
38
+ h.update(
39
+ min: @min,
40
+ max: @max
41
+ )
42
+ end
43
+ end
44
+
45
+ private
46
+
47
+ def maybe_to_a(object)
48
+ if object.nil?
49
+ nil
50
+ else
51
+ object.map(&:to_h)
52
+ end
53
+ end
54
+ end
9
55
  end
@@ -0,0 +1,19 @@
1
+ require_relative 'parameter'
2
+
3
+ module Opushon
4
+ class Request
5
+ include Virtus.model(strict: true)
6
+
7
+ attribute :headers, Hash[Symbol => Parameter], default: {}, required: true
8
+ attribute :query_string, Hash[Symbol => Parameter], default: {}, required: true
9
+ attribute :body, Hash[Symbol => Parameter], default: {}, required: true
10
+
11
+ def to_h
12
+ {
13
+ headers: @headers.map { |k, v| [k, v.to_h] }.to_h,
14
+ query_string: @query_string.map { |k, v| [k, v.to_h] }.to_h,
15
+ body: @body.map { |k, v| [k, v.to_h] }.to_h
16
+ }
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,17 @@
1
+ require_relative 'parameter'
2
+
3
+ module Opushon
4
+ class Response
5
+ include Virtus.model(strict: true)
6
+
7
+ attribute :headers, Hash[Symbol => Parameter], default: {}, required: true
8
+ attribute :body, Hash[Symbol => Parameter], default: {}, required: true
9
+
10
+ def to_h
11
+ {
12
+ headers: @headers.map { |k, v| [k, v.to_h] }.to_h,
13
+ body: @body.map { |k, v| [k, v.to_h] }.to_h
14
+ }
15
+ end
16
+ end
17
+ end
@@ -1,29 +1,11 @@
1
- # Namespace for the Opushon library.
2
1
  module Opushon
3
- # Parse a Opushon string in opushon.
4
2
  class RestrictedValue
5
- attr_reader :value
3
+ include Virtus.model(strict: true)
6
4
 
7
- def self.load_list(restricted_values)
8
- return if restricted_values.nil?
5
+ attribute :title, String, default: '', required: true
6
+ attribute :description, String, default: '', required: true
7
+ attribute :value, BasicObject, required: true
9
8
 
10
- restricted_values.map do |restricted_value|
11
- new(JSON.parse(restricted_value.to_json, symbolize_names: true)).to_h
12
- end
13
- end
14
-
15
- def initialize( title: '',
16
- description: '',
17
- value: )
18
-
19
- @title = title.to_s
20
- @description = description.to_s
21
- @value = value
22
-
23
- freeze
24
- end
25
-
26
- # Dump instance's opushon to a hash.
27
9
  def to_h
28
10
  {
29
11
  title: @title,
data/lib/opushon.rb CHANGED
@@ -1,22 +1,36 @@
1
- require_relative File.join(*%w(opushon error))
2
- require_relative File.join(*%w(opushon instance))
3
- require_relative File.join(*%w(opushon version))
1
+ require 'virtus'
2
+ require 'json'
3
+ require_relative File.join 'opushon', 'body'
4
4
 
5
5
  # Namespace for the Opushon library.
6
6
  #
7
7
  # @api private
8
8
  module Opushon
9
9
  class << self
10
- # Load opushon in to a Ruby data structure.
10
+ # Load opushon in to the Ruby data structure.
11
+ #
11
12
  # @api public
12
- def load(opushon)
13
- Instance.new(opushon)
13
+ #
14
+ # @example Load the option of a DELETE interface
15
+ # load('{"DELETE":{"title":"Delete issues","description":"Remove every issues.","request":{"headers":{},"query_string":{},"body":{}},"response":{"headers":{},"query_string":{},"body":{}}}}')
16
+ #
17
+ # @param [String] opushon_string
18
+ #
19
+ # @return [Body] the Ruby data structure
20
+ def load(opushon_string)
21
+ opushon_hash = JSON.load(opushon_string)
22
+ Body.new(opushon_hash)
14
23
  end
15
24
 
16
- # Dump Ruby object o to a Opushon string.
25
+ # Dump Ruby object to a Opushon string.
26
+ #
17
27
  # @api public
18
- def dump(o)
19
- JSON.dump o
28
+ #
29
+ # @param [Body] opushon
30
+ #
31
+ # @return [String] the Opushon data representation
32
+ def dump(opushon)
33
+ opushon.to_h.to_json
20
34
  end
21
35
  end
22
36
  end
data/opushon.gemspec CHANGED
@@ -3,18 +3,22 @@ Gem::Specification.new do |spec|
3
3
  spec.version = File.read('VERSION.semver')
4
4
  spec.authors = ['Cyril Wack']
5
5
  spec.email = ['contact@cyril.email']
6
+
6
7
  spec.summary = 'A HTTP Opushon parser and emitter'
7
8
  spec.description = 'An Opushon\'s body parser and emitter.'
8
9
  spec.homepage = 'https://github.com/cyril/opushon.rb'
9
10
  spec.license = 'MIT'
10
11
 
11
- spec.files = `git ls-files -z`.split("\x0")
12
- spec.test_files = spec.files.grep(/^spec\//)
12
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(/^test\//) }
13
+ spec.executables = spec.files.grep(/^exe\//) { |f| File.basename(f) }
13
14
  spec.require_paths = ['lib']
14
- spec.required_ruby_version = '>= 2.0.0'
15
15
 
16
- spec.add_development_dependency 'bundler', '~> 1.7'
17
- spec.add_development_dependency 'minitest', '~> 5'
16
+ spec.add_dependency 'virtus', '~> 1.0.4'
17
+
18
+ spec.add_development_dependency 'bundler', '~> 1.8'
18
19
  spec.add_development_dependency 'rake', '~> 10.0'
19
- spec.add_development_dependency 'coveralls', '~> 0.7'
20
+ spec.add_development_dependency 'yard', '~> 0.8'
21
+ spec.add_development_dependency 'simplecov', '~> 0.9.1'
22
+ spec.add_development_dependency 'rubocop', '~> 0.29'
23
+ spec.add_development_dependency 'expect', '~> 0.0.6'
20
24
  end