yaks-simple 0.0.1

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: e0b188779102f23074095a7df3fa7dcb7f0aea31
4
+ data.tar.gz: 19baa136f08468ebd6507600f5256ef512e36ea0
5
+ SHA512:
6
+ metadata.gz: a0fbece8dd84ef53d94f86c350c5a79390a1b4a49d4a86d1da5a130d9c6aebb2efdd9192a1b16a41f43a7925c3f1603853eccc792eb1d5e149794a6f4ed2ddf5
7
+ data.tar.gz: 5b3fdf254c3937593641be50673561d15629d911589220489db65d1fd986556a287fb5aaed70f3a1c37e52dfa4bd2eedc4e076a683a1d1de1708a181518626a7
data/.gitignore ADDED
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --require spec_helper
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in yaks-simple.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Hobofan
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,33 @@
1
+ # Yaks::Simple
2
+
3
+ This gem adds the `:simple_json` format to yaks.
4
+ `:simple_json` completely ignores the purpose of yaks to build a hypermedia API
5
+ and currently doesn't render any links, just resource attributes.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'yaks-simple'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install yaks-simple
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Contributing
28
+
29
+ 1. Fork it ( https://github.com/[my-github-username]/yaks-simple/fork )
30
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
31
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
32
+ 4. Push to the branch (`git push origin my-new-feature`)
33
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,71 @@
1
+ module Yaks
2
+ class Format
3
+ class SimpleJson < self
4
+ register :simple_json, :json, 'application/json'
5
+
6
+ include FP
7
+
8
+ # @param [yaks::resource] resource
9
+ # @return [hash]
10
+ def serialize_resource(resource)
11
+ result = {}
12
+ items = if resource.is_a?(Yaks::CollectionResource)
13
+ resource.members.map {|r| serialize_item(r)}
14
+ else
15
+ serialize_item(resource.attributes)
16
+ end
17
+ result[pluralize(resource.type)] = items
18
+
19
+ result
20
+ end
21
+
22
+ # @param [yaks::resource] resource
23
+ # @return [hash]
24
+ def serialize_item(resource)
25
+ resource.attributes
26
+ end
27
+
28
+ # @param [Yaks::Resource] subresource
29
+ # @return [Hash]
30
+ def serialize_links(subresources)
31
+ {}
32
+ end
33
+
34
+ # @param [Yaks::Resource] resource
35
+ # @return [Array, String]
36
+ def serialize_link(resource)
37
+ {}
38
+ end
39
+
40
+ # @param [Hash] subresources
41
+ # @param [Array] array
42
+ # @return [Array]
43
+ def serialize_linked_subresources(subresources, array)
44
+ []
45
+ end
46
+
47
+ # @param [Array] resources
48
+ # @param [Array] linked
49
+ # @return [Array]
50
+ def serialize_linked_resources(subresource, linked)
51
+ []
52
+ end
53
+
54
+ # {shows => [{id: 3, name: 'foo'}]}
55
+ #
56
+ # @param [Yaks::Resource] resource
57
+ # @param [Hash] linked
58
+ # @return [Hash]
59
+ def serialize_subresource(resource, linked)
60
+ serialize_resource(resource)
61
+ end
62
+
63
+ def inverse
64
+ Yaks::Reader::SimpleJson.new
65
+ end
66
+ end
67
+
68
+ class Reader
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,21 @@
1
+ module Yaks
2
+ module Reader
3
+ class SimpleJson
4
+ def call(parsed_json, env = {})
5
+ type = env[:type] || parsed_json.first.first
6
+ attributes = env[:attributes] || parsed_json[type]
7
+
8
+ if attributes.is_a? Array
9
+ attributes = attributes.dup
10
+ members = attributes.map { |r| call(r, type: type, attributes: r) }
11
+ CollectionResource.new(members: members, type: Util.singularize(type))
12
+ else
13
+ Resource.new(
14
+ type: Util.singularize(type),
15
+ attributes: Util.symbolize_keys(attributes)
16
+ )
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,11 @@
1
+ require "yaks/simple/version"
2
+
3
+ require 'yaks/format/simple_json'
4
+ require 'yaks/reader/simple_json'
5
+
6
+ module Yaks
7
+ module Simple
8
+ # Set the Root constant as the gems root path
9
+ Root = Pathname(__FILE__).join('../../..')
10
+ end
11
+ end
@@ -0,0 +1,5 @@
1
+ module Yaks
2
+ module Simple
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,19 @@
1
+ RSpec.shared_examples_for 'JSON output format' do |yaks, format, name|
2
+ let(:input) { load_yaml_fixture(name) }
3
+ let(:output) { load_json_fixture("#{name}.#{format}") }
4
+
5
+ subject { yaks.call(input) }
6
+
7
+ it { should deep_eql output }
8
+ end
9
+
10
+ RSpec.shared_examples_for 'JSON round trip' do |yaks, format, name|
11
+ let(:json) { load_json_fixture("#{name}.#{format}") }
12
+
13
+ subject {
14
+ resource = yaks.read(json, hooks: [[:skip, :parse]])
15
+ yaks.call(resource, hooks: [[:skip, :map]], mapper: Struct.new(:context))
16
+ }
17
+
18
+ it { should deep_eql json }
19
+ end
@@ -0,0 +1,11 @@
1
+ require 'acceptance/json_shared_examples'
2
+
3
+ describe Yaks::Format::SimpleJson do
4
+ config = Yaks.new do
5
+ default_format :simple_json
6
+ skip :serialize
7
+ end
8
+
9
+ include_examples 'JSON output format', config, :simple_json, 'confucius'
10
+ include_examples 'JSON round trip', config, :simple_json, 'confucius'
11
+ end
@@ -0,0 +1,14 @@
1
+ require 'yaml'
2
+ require 'json'
3
+
4
+ module FixtureHelpers
5
+ module_function
6
+
7
+ def load_json_fixture(name)
8
+ JSON.parse(Yaks::Simple::Root.join('spec/json', name + '.json').read)
9
+ end
10
+
11
+ def load_yaml_fixture(name)
12
+ YAML.load(Yaks::Simple::Root.join('spec/yaml', name + '.yaml').read)
13
+ end
14
+ end
@@ -0,0 +1,10 @@
1
+ {
2
+ "scholars": [
3
+ {
4
+ "id": 9,
5
+ "name": "孔子",
6
+ "pinyin": "Kongzi",
7
+ "latinized": "Confucius"
8
+ }
9
+ ]
10
+ }
@@ -0,0 +1,98 @@
1
+ require 'yaks'
2
+ require 'yaks/simple'
3
+
4
+ require 'fixture_helpers'
5
+
6
+ require_relative 'support/deep_eql'
7
+ # This file was generated by the `rspec --init` command. Conventionally, all
8
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
9
+ # The generated `.rspec` file contains `--require spec_helper` which will cause
10
+ # this file to always be loaded, without a need to explicitly require it in any
11
+ # files.
12
+ #
13
+ # Given that it is always loaded, you are encouraged to keep this file as
14
+ # light-weight as possible. Requiring heavyweight dependencies from this file
15
+ # will add to the boot time of your test suite on EVERY test run, even for an
16
+ # individual file that may not need all of that loaded. Instead, consider making
17
+ # a separate helper file that requires the additional dependencies and performs
18
+ # the additional setup, and require it from the spec files that actually need
19
+ # it.
20
+ #
21
+ # The `.rspec` file also contains a few flags that are not defaults but that
22
+ # users commonly want.
23
+ #
24
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
25
+ RSpec.configure do |config|
26
+ config.include FixtureHelpers
27
+ # rspec-expectations config goes here. You can use an alternate
28
+ # assertion/expectation library such as wrong or the stdlib/minitest
29
+ # assertions if you prefer.
30
+ config.expect_with :rspec do |expectations|
31
+ # This option will default to `true` in RSpec 4. It makes the `description`
32
+ # and `failure_message` of custom matchers include text for helper methods
33
+ # defined using `chain`, e.g.:
34
+ # be_bigger_than(2).and_smaller_than(4).description
35
+ # # => "be bigger than 2 and smaller than 4"
36
+ # ...rather than:
37
+ # # => "be bigger than 2"
38
+ expectations.include_chain_clauses_in_custom_matcher_descriptions = true
39
+ end
40
+
41
+ # rspec-mocks config goes here. You can use an alternate test double
42
+ # library (such as bogus or mocha) by changing the `mock_with` option here.
43
+ config.mock_with :rspec do |mocks|
44
+ # Prevents you from mocking or stubbing a method that does not exist on
45
+ # a real object. This is generally recommended, and will default to
46
+ # `true` in RSpec 4.
47
+ mocks.verify_partial_doubles = true
48
+ end
49
+
50
+ # The settings below are suggested to provide a good initial experience
51
+ # with RSpec, but feel free to customize to your heart's content.
52
+ =begin
53
+ # These two settings work together to allow you to limit a spec run
54
+ # to individual examples or groups you care about by tagging them with
55
+ # `:focus` metadata. When nothing is tagged with `:focus`, all examples
56
+ # get run.
57
+ config.filter_run :focus
58
+ config.run_all_when_everything_filtered = true
59
+
60
+ # Limits the available syntax to the non-monkey patched syntax that is
61
+ # recommended. For more details, see:
62
+ # - http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax
63
+ # - http://teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
64
+ # - http://myronmars.to/n/dev-blog/2014/05/notable-changes-in-rspec-3#new__config_option_to_disable_rspeccore_monkey_patching
65
+ config.disable_monkey_patching!
66
+
67
+ # This setting enables warnings. It's recommended, but in some cases may
68
+ # be too noisy due to issues in dependencies.
69
+ config.warnings = true
70
+
71
+ # Many RSpec users commonly either run the entire suite or an individual
72
+ # file, and it's useful to allow more verbose output when running an
73
+ # individual spec file.
74
+ if config.files_to_run.one?
75
+ # Use the documentation formatter for detailed output,
76
+ # unless a formatter has already been configured
77
+ # (e.g. via a command-line flag).
78
+ config.default_formatter = 'doc'
79
+ end
80
+
81
+ # Print the 10 slowest examples and example groups at the
82
+ # end of the spec run, to help surface which specs are running
83
+ # particularly slow.
84
+ config.profile_examples = 10
85
+
86
+ # Run specs in random order to surface order dependencies. If you find an
87
+ # order dependency and want to debug it, you can fix the order by providing
88
+ # the seed, which is printed after each run.
89
+ # --seed 1234
90
+ config.order = :random
91
+
92
+ # Seed global randomization in this process using the `--seed` CLI option.
93
+ # Setting this allows you to use `--seed` to deterministically reproduce
94
+ # test failures related to randomization by passing the same `--seed` value
95
+ # as the one that triggered the failure.
96
+ Kernel.srand config.seed
97
+ =end
98
+ end
@@ -0,0 +1,123 @@
1
+ # When comparing deep nested structures, it can be really hard to figure out what
2
+ # the actual differences are looking at the RSpec output. This custom matcher
3
+ # traverses nested hashes and arrays recursively, and reports each difference
4
+ # separately, with a JSONPath string of where the difference was found
5
+ #
6
+ # e.g.
7
+ #
8
+ # at $.shows[0].venues[0].name, got Foo, expected Bar
9
+
10
+ module Matchers
11
+ class DeepEql
12
+ extend Forwardable
13
+ attr_reader :expectation, :stack, :target, :diffs, :result
14
+ def_delegators :stack, :push, :pop
15
+
16
+ def initialize(expectation, stack = [], diffs = [])
17
+ @expectation = expectation
18
+ @stack = stack
19
+ @diffs = diffs
20
+ @result = true
21
+ end
22
+
23
+ def description
24
+ 'be deeply equal'
25
+ end
26
+
27
+ def recurse(target, expectation)
28
+ @result &&= DeepEql.new(expectation, stack, diffs).matches?(target)
29
+ end
30
+
31
+ def stack_as_jsonpath
32
+ '$' + stack.map do |item|
33
+ case item
34
+ when Integer, /\W/
35
+ "[#{item.inspect}]"
36
+ else
37
+ ".#{item}"
38
+ end
39
+ end.join
40
+ end
41
+
42
+ def add_failure_message(message)
43
+ diffs << "at %s, %s" % [stack_as_jsonpath, message]
44
+ @result = false
45
+ end
46
+
47
+ def compare(key)
48
+ push key
49
+ if target[key] != expectation[key]
50
+ if [Hash, Array].any?{|klz| target[key].is_a? klz }
51
+ recurse(target[key], expectation[key])
52
+ else
53
+ add_failure_message begin
54
+ if expectation[key].class == target[key].class
55
+ "expected #{expectation[key].inspect}, got #{target[key].inspect}"
56
+ else
57
+ "expected #{expectation[key].class}: #{expectation[key].inspect}, got #{target[key].class}: #{target[key].inspect}"
58
+ end
59
+ rescue Encoding::CompatibilityError
60
+ "expected #{expectation[key].encoding}, got #{target[key].encoding}"
61
+ end
62
+ end
63
+ end
64
+ pop
65
+ end
66
+
67
+ def matches?(target)
68
+ @target = target
69
+
70
+ case expectation
71
+ when Hash
72
+ if target.is_a?(Hash)
73
+ if target.class != expectation.class # e.g. HashWithIndifferentAccess
74
+ add_failure_message("expected #{expectation.class}, got #{target.class}")
75
+ end
76
+ (expectation.keys - target.keys).each do |key|
77
+ add_failure_message "Expected key #{key}"
78
+ end
79
+ (target.keys - expectation.keys).each do |key|
80
+ add_failure_message "Unexpected key #{key}"
81
+ end
82
+ (target.keys | expectation.keys).each do |key|
83
+ compare key
84
+ end
85
+ else
86
+ add_failure_message("expected Hash got #{@target.inspect}")
87
+ end
88
+
89
+ when Array
90
+ if target.is_a?(Array)
91
+ 0.upto([target.count, expectation.count].max) do |idx|
92
+ compare idx
93
+ end
94
+ else
95
+ add_failure_message("expected Array got #{@target.inspect}")
96
+ end
97
+
98
+ else
99
+ if target != expectation
100
+ add_failure_message("expected #{expectation.inspect}, got #{@target.inspect}")
101
+ end
102
+ end
103
+
104
+ result
105
+ end
106
+
107
+ def failure_message_for_should
108
+ diffs.join("\n")
109
+ end
110
+ alias failure_message failure_message_for_should
111
+
112
+ def failure_message_for_should_not
113
+ "expected #{@target.inspect} not to be in deep_eql with #{@expectation.inspect}"
114
+ end
115
+ alias failure_message_when_negated failure_message_for_should_not
116
+ end
117
+ end
118
+
119
+ module RSpec::Matchers
120
+ def deep_eql(exp)
121
+ Matchers::DeepEql.new(exp)
122
+ end
123
+ end
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'yaks/simple/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "yaks-simple"
8
+ spec.version = Yaks::Simple::VERSION
9
+ spec.authors = ["Maximilian Goisser"]
10
+ spec.email = ["goisser94@gmail.com"]
11
+ spec.summary = %q{Simple JSON Format that ignores most of yaks features.}
12
+ spec.description = %q{Simple JSON Format that ignores most of yaks features.}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "yaks", "~> 0.9"
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.7"
24
+ spec.add_development_dependency "rake", "~> 10.0"
25
+ spec.add_development_dependency "rspec", "~> 3.2"
26
+ end
metadata ADDED
@@ -0,0 +1,123 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: yaks-simple
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Maximilian Goisser
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-03-31 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: yaks
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.9'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0.9'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.7'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.7'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.2'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.2'
69
+ description: Simple JSON Format that ignores most of yaks features.
70
+ email:
71
+ - goisser94@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".rspec"
78
+ - Gemfile
79
+ - LICENSE.txt
80
+ - README.md
81
+ - Rakefile
82
+ - lib/yaks/format/simple_json.rb
83
+ - lib/yaks/reader/simple_json.rb
84
+ - lib/yaks/simple.rb
85
+ - lib/yaks/simple/version.rb
86
+ - spec/acceptance/json_shared_examples.rb
87
+ - spec/acceptance/simple_json_spec.rb
88
+ - spec/fixture_helpers.rb
89
+ - spec/json/confucius.simple_json.json
90
+ - spec/spec_helper.rb
91
+ - spec/support/deep_eql.rb
92
+ - yaks-simple.gemspec
93
+ homepage: ''
94
+ licenses:
95
+ - MIT
96
+ metadata: {}
97
+ post_install_message:
98
+ rdoc_options: []
99
+ require_paths:
100
+ - lib
101
+ required_ruby_version: !ruby/object:Gem::Requirement
102
+ requirements:
103
+ - - ">="
104
+ - !ruby/object:Gem::Version
105
+ version: '0'
106
+ required_rubygems_version: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ requirements: []
112
+ rubyforge_project:
113
+ rubygems_version: 2.2.2
114
+ signing_key:
115
+ specification_version: 4
116
+ summary: Simple JSON Format that ignores most of yaks features.
117
+ test_files:
118
+ - spec/acceptance/json_shared_examples.rb
119
+ - spec/acceptance/simple_json_spec.rb
120
+ - spec/fixture_helpers.rb
121
+ - spec/json/confucius.simple_json.json
122
+ - spec/spec_helper.rb
123
+ - spec/support/deep_eql.rb