blumquist 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: eb3fa8a72a576d8cc9343cf7fa6ad38125a763d1
4
+ data.tar.gz: dadff91b9e4aa6909d97e8e46b254b033333c141
5
+ SHA512:
6
+ metadata.gz: 358263522e0cd18da0b2690cfe8832121eb5234cf33a033cda0007cfaf0e0841454a366ecf15ecaef46895e9e535084966c79c65480f74dfdd18bbd4190d2033
7
+ data.tar.gz: 2c3f956c6b52928ddc45579802a8ac291df640c1da8d0c9a02b28e458d265e93462c912a7880f6d4e748b825d6a78ad59c2b38802a8671e3e21372992b9f530f
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ gems.tags
11
+ tags
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -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, ethnicity, 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 ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in blumquist.gemspec
4
+ gemspec
data/Guardfile ADDED
@@ -0,0 +1,41 @@
1
+ # A sample Guardfile
2
+ # More info at https://github.com/guard/guard#readme
3
+
4
+ ## Uncomment and set this to only include directories you want to watch
5
+ # directories %w(app lib config test spec features) \
6
+ # .select{|d| Dir.exists?(d) ? d : UI.warning("Directory #{d} does not exist")}
7
+
8
+ ## Note: if you are using the `directories` clause above and you are not
9
+ ## watching the project directory ('.'), then you will want to move
10
+ ## the Guardfile to a watched dir and symlink it back, e.g.
11
+ #
12
+ # $ mkdir config
13
+ # $ mv Guardfile config/
14
+ # $ ln -s config/Guardfile .
15
+ #
16
+ # and, you'll have to watch "config/Guardfile" instead of "Guardfile"
17
+
18
+ guard :bundler do
19
+ watch('Gemfile')
20
+ # Uncomment next line if your Gemfile contains the `gemspec' command.
21
+ watch(/^.+\.gemspec/)
22
+ end
23
+
24
+ guard 'ctags-bundler', :src_path => ["lib"] do
25
+ watch(/^(app|lib|spec\/support)\/.*\.rb$/)
26
+ watch('Gemfile.lock')
27
+ watch(/^.+\.gemspec/)
28
+ end
29
+
30
+ guard :rspec, cmd: 'IGNORE_LOW_COVERAGE=1 rspec', all_on_start: true do
31
+ watch(%r{^spec/support/.*\.mson$}) { "spec" }
32
+ watch(%r{^spec/support/.*\.rb$}) { "spec" }
33
+ watch('spec/spec_helper.rb') { "spec" }
34
+
35
+ # We could run individual specs, sure, but for now I dictate the tests
36
+ # are only green when we have 100% coverage, so partial runs will never
37
+ # succeed. Therefore, always run all the things.
38
+ watch(%r{^(spec/.+_spec\.rb)$}) { "spec" }
39
+ watch(%r{^lib/(.+)\.rb$}) { "spec" }
40
+ end
41
+
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Jannis Hermanns
4
+
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:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
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 ADDED
@@ -0,0 +1,108 @@
1
+ # Blumquist [![Circle CI](https://circleci.com/gh/moviepilot/blumquist/tree/master.svg?style=svg)](https://circleci.com/gh/moviepilot/blumquist/tree/master) [![Coverage Status](https://coveralls.io/repos/moviepilot/blumquist/badge.svg?branch=master&service=github)](https://coveralls.io/github/moviepilot/blumquist?branch=master)
2
+
3
+ ![](https://dl.dropboxusercontent.com/u/1953503/blumquist.jpg)
4
+
5
+ Given a JSON schema and some data, Blumquist will give you an immutable object that has getters defined for all properties from the JSON schema. Works well with schemas using JSON pointers as well as for properties that are themselves objects or arrays.
6
+
7
+ ## Usage
8
+
9
+ 1. Give it a schema
10
+ 2. Give it some data
11
+ 3. Get an object with getters
12
+
13
+ (Sorry, no profit step.)
14
+
15
+ ### 1. Give it a schema
16
+
17
+ ```json
18
+ schema = {
19
+ "$schema": "http://json-schema.org/draft-04/schema#",
20
+
21
+ "definitions": {
22
+ "address": {
23
+ "type": "object",
24
+ "properties": {
25
+ "street": { "type": "string" },
26
+ "city": { "type": "string" },
27
+ "state": { "type": "string" }
28
+ },
29
+ "required": ["street", "city", "state"]
30
+ }
31
+ },
32
+
33
+ "type": "object",
34
+
35
+ "properties": {
36
+ "name": { "type": "string" },
37
+ "current_address": { "$ref": "#/definitions/address" },
38
+ "old_addresses": { "type": "array", "items": { "$ref": "#/definitions/address" } }
39
+ }
40
+ }
41
+ ```
42
+
43
+ ### 2. Give it some data
44
+ ```ruby
45
+ data = {
46
+ "name": "Moviepilot, Inc.",
47
+ "current_address": {
48
+ "street_address": "Friedrichstr. 58",
49
+ "city": "Berlin",
50
+ "state": "Berlin"
51
+ },
52
+ "old_addresses": [
53
+ {
54
+ "street_address": "Blücherstr. 22",
55
+ "city": "Berlin",
56
+ "state": "Berlin"
57
+ },
58
+ {
59
+ "street_address": "Mehringdamm 33",
60
+ "city": "Berlin",
61
+ "state": "Berlin"
62
+ }
63
+ ]
64
+ }
65
+
66
+ ```
67
+ ### 3. And you get...
68
+ ... an object with getters for all properties defined in the schema
69
+
70
+ ```ruby
71
+ > b = Blumquist.new(schema, data)
72
+ => #<Blumquist:0x....>
73
+ > b.name
74
+ => "Moviepilot, Inc."
75
+ > b.old_addresses.first.street
76
+ => "Blücherstr. 22"
77
+ ```
78
+
79
+ ## TODO
80
+
81
+ - Put it to use, fix bugs
82
+ - Validate data against schema and reject invalid data
83
+
84
+
85
+ ## Installation
86
+
87
+ Add this line to your application's Gemfile:
88
+
89
+ ```ruby
90
+ gem 'blumquist'
91
+ ```
92
+
93
+ And then execute:
94
+
95
+ $ bundle
96
+
97
+ Or install it yourself as:
98
+
99
+ $ gem install blumquist
100
+
101
+ ## Contributing
102
+
103
+ Bug reports and pull requests are welcome on GitHub in the [issues section](https://github.com/moviepilot/blumquist/issues). This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](contributor-covenant.org) code of conduct.
104
+
105
+
106
+ ## License
107
+
108
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/blumquist.gemspec ADDED
@@ -0,0 +1,32 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'blumquist/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "blumquist"
8
+ spec.version = Blumquist::VERSION
9
+ spec.authors = ["Jannis Hermanns"]
10
+ spec.email = ["jannis@moviepilot.com"]
11
+
12
+ spec.summary = "Turn some data and a json schema into an immutable object with getters from the schema"
13
+ spec.description = "What the summary said."
14
+ spec.homepage = "https://github.com/moviepilot/blumquist"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = "bin"
19
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_runtime_dependency "activesupport"
23
+
24
+ spec.add_development_dependency "bundler", "~> 1"
25
+ spec.add_development_dependency "rake", "~> 10.0"
26
+ spec.add_development_dependency "rspec"
27
+ spec.add_development_dependency "coveralls"
28
+ spec.add_development_dependency "simplecov"
29
+ spec.add_development_dependency "guard-rspec"
30
+ spec.add_development_dependency "guard-bundler"
31
+ spec.add_development_dependency "guard-ctags-bundler"
32
+ end
data/lib/blumquist.rb ADDED
@@ -0,0 +1,101 @@
1
+ require 'pry'
2
+ require "blumquist/version"
3
+ require 'active_support/core_ext/hash/indifferent_access'
4
+ require 'json'
5
+
6
+ class Blumquist
7
+ def initialize(schema, data)
8
+ # Poor man's deep clone: json 🆗 🆒
9
+ @data = JSON.parse(data.to_json)
10
+ @schema = schema.with_indifferent_access
11
+
12
+ validate_schema
13
+ resolve_json_pointers
14
+ define_getters
15
+ rescue
16
+ binding.pry
17
+ end
18
+
19
+ private
20
+
21
+ def validate_schema
22
+ if @schema[:type] != 'object'
23
+ raise "Can only deal with 'object' types, not '#{@schema[:type]}'"
24
+ end
25
+ unless @schema[:properties].is_a?(Hash)
26
+ raise "Properties are a #{@schema[:properties].class.name}, not a Hash"
27
+ end
28
+ end
29
+
30
+ def resolve_json_pointers
31
+ @schema[:properties].each do |property, type_def|
32
+ next unless type_def[:$ref]
33
+ resolve_json_pointer!(type_def)
34
+ end
35
+ end
36
+
37
+ def resolve_json_pointer!(type_def)
38
+ # Should read up on how json pointers are really resolved
39
+ pointer = type_def.delete(:$ref)
40
+ key = pointer.split('/').last
41
+ definition = @schema[:definitions][key]
42
+
43
+ raise "Can't resolve pointer #{pointer}" unless definition
44
+
45
+ type_def.merge! definition
46
+ end
47
+
48
+ def primitive_type?(type)
49
+ %w{null boolean number string}.include? type.to_s
50
+ end
51
+
52
+ def define_getters
53
+ @schema[:properties].each do |property, type_def|
54
+
55
+ # Wrap objects recursively
56
+ if type_def[:type] == 'object'
57
+ blumquistify_object(property)
58
+
59
+ # Turn array elements into Blumquists
60
+ elsif type_def[:type] == 'array'
61
+ blumquistify_array(property)
62
+
63
+ # Nothing to do for primitive values
64
+ elsif primitive_type?(type_def[:type])
65
+
66
+ # We don't know what to do, so let's panic
67
+ else
68
+ raise "Can't handle type '#{type_def}' yet, I'm sorry"
69
+ end
70
+
71
+ # And define the getter
72
+ define_getter(property)
73
+ end
74
+ end
75
+
76
+ def define_getter(property)
77
+ self.class.class_eval do
78
+ define_method(property) do
79
+ @data[property]
80
+ end
81
+ end
82
+ end
83
+
84
+ def blumquistify_object(property)
85
+ sub_schema = @schema[:properties][property].merge(
86
+ definitions: @schema[:definitions]
87
+ )
88
+ @data[property] = Blumquist.new(sub_schema, @data[property])
89
+ end
90
+
91
+ def blumquistify_array(property)
92
+ item_schema = resolve_json_pointer!(@schema[:properties][property][:items])
93
+ sub_schema = item_schema.merge(
94
+ definitions: @schema[:definitions]
95
+ )
96
+ @data[property] ||= []
97
+ @data[property] = @data[property].map do |item|
98
+ Blumquist.new(sub_schema, item)
99
+ end
100
+ end
101
+ end
@@ -0,0 +1,3 @@
1
+ class Blumquist
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,182 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: blumquist
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Jannis Hermanns
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-10-30 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
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'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1'
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: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: coveralls
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: simplecov
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: guard-rspec
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: guard-bundler
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: guard-ctags-bundler
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ description: What the summary said.
140
+ email:
141
+ - jannis@moviepilot.com
142
+ executables: []
143
+ extensions: []
144
+ extra_rdoc_files: []
145
+ files:
146
+ - ".gitignore"
147
+ - ".rspec"
148
+ - CODE_OF_CONDUCT.md
149
+ - Gemfile
150
+ - Guardfile
151
+ - LICENSE.txt
152
+ - README.md
153
+ - Rakefile
154
+ - blumquist.gemspec
155
+ - lib/blumquist.rb
156
+ - lib/blumquist/version.rb
157
+ homepage: https://github.com/moviepilot/blumquist
158
+ licenses:
159
+ - MIT
160
+ metadata: {}
161
+ post_install_message:
162
+ rdoc_options: []
163
+ require_paths:
164
+ - lib
165
+ required_ruby_version: !ruby/object:Gem::Requirement
166
+ requirements:
167
+ - - ">="
168
+ - !ruby/object:Gem::Version
169
+ version: '0'
170
+ required_rubygems_version: !ruby/object:Gem::Requirement
171
+ requirements:
172
+ - - ">="
173
+ - !ruby/object:Gem::Version
174
+ version: '0'
175
+ requirements: []
176
+ rubyforge_project:
177
+ rubygems_version: 2.4.8
178
+ signing_key:
179
+ specification_version: 4
180
+ summary: Turn some data and a json schema into an immutable object with getters from
181
+ the schema
182
+ test_files: []