venduitz 0.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +2 -0
- data/.travis.yml +5 -0
- data/Dockerfile +17 -0
- data/Gemfile +2 -0
- data/Gemfile.lock +36 -0
- data/README.md +55 -0
- data/lib/venduitz/version.rb +7 -0
- data/lib/venduitz/view.rb +70 -0
- data/lib/venduitz.rb +10 -0
- data/spec/spec_helper.rb +8 -0
- data/spec/venduitz/view_spec.rb +96 -0
- data/venduitz.gemspec +27 -0
- metadata +96 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: b6dac980ab2b8be2b8abf9dbeb46e7d3cfc65a12
|
4
|
+
data.tar.gz: 313d678a9e19b38b8974cf7e4b87b81e287839cd
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: c65507fd4c7003b689382b8e863f2ea5ae0071035df5a18046df8e0144cfcd1a44f40d64392ec8e6b708deb195c7e9c8c60e83cc05caa46fe0bf8b545b468057
|
7
|
+
data.tar.gz: 311d28bda3f9754a84dc59c354dc623f8b84f06ad6512626152ede825c8f9801b28e2a77ea96d722b749f4336df12540fd752638f5231e8546497d9f0488b211
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Dockerfile
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
# Ruby image
|
2
|
+
FROM ruby
|
3
|
+
|
4
|
+
# Install bundler
|
5
|
+
RUN gem install bundler --no-ri --no-rdoc
|
6
|
+
|
7
|
+
# Make app folder
|
8
|
+
RUN mkdir venduitz/
|
9
|
+
|
10
|
+
# Set as workdir
|
11
|
+
WORKDIR venduitz/
|
12
|
+
|
13
|
+
# Add the full source
|
14
|
+
ADD . .
|
15
|
+
|
16
|
+
# Install!
|
17
|
+
RUN bundle install
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
venduitz (0.0.0)
|
5
|
+
multi_json
|
6
|
+
oj
|
7
|
+
|
8
|
+
GEM
|
9
|
+
remote: https://rubygems.org/
|
10
|
+
specs:
|
11
|
+
diff-lcs (1.2.5)
|
12
|
+
multi_json (1.11.3)
|
13
|
+
oj (2.15.0)
|
14
|
+
rspec (3.4.0)
|
15
|
+
rspec-core (~> 3.4.0)
|
16
|
+
rspec-expectations (~> 3.4.0)
|
17
|
+
rspec-mocks (~> 3.4.0)
|
18
|
+
rspec-core (3.4.4)
|
19
|
+
rspec-support (~> 3.4.0)
|
20
|
+
rspec-expectations (3.4.0)
|
21
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
22
|
+
rspec-support (~> 3.4.0)
|
23
|
+
rspec-mocks (3.4.1)
|
24
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
25
|
+
rspec-support (~> 3.4.0)
|
26
|
+
rspec-support (3.4.1)
|
27
|
+
|
28
|
+
PLATFORMS
|
29
|
+
ruby
|
30
|
+
|
31
|
+
DEPENDENCIES
|
32
|
+
rspec (~> 3.0)
|
33
|
+
venduitz!
|
34
|
+
|
35
|
+
BUNDLED WITH
|
36
|
+
1.12.1
|
data/README.md
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
# Venduitz [![Build Status](https://travis-ci.org/gabrielcorado/venduitz.svg?branch=develop)](https://travis-ci.org/gabrielcorado/venduitz)
|
2
|
+
A simple JSON-only(until now) template engine, with focus on performance for better Restful-APIs.
|
3
|
+
|
4
|
+
## How to use it
|
5
|
+
|
6
|
+
### Install
|
7
|
+
```ruby
|
8
|
+
gem install venduitz
|
9
|
+
# OR...
|
10
|
+
gem 'venduitz'
|
11
|
+
```
|
12
|
+
|
13
|
+
### Define your views
|
14
|
+
```ruby
|
15
|
+
require 'venduitz'
|
16
|
+
|
17
|
+
# Image view
|
18
|
+
class ImageView < Venduitz::View
|
19
|
+
# Define the view properties
|
20
|
+
prop :url
|
21
|
+
prop :width
|
22
|
+
prop :height
|
23
|
+
prop :alt
|
24
|
+
end
|
25
|
+
|
26
|
+
# Product view
|
27
|
+
class ProductView < Venduitz::View
|
28
|
+
# Define the view properties
|
29
|
+
prop :name
|
30
|
+
prop :sku
|
31
|
+
prop :price
|
32
|
+
|
33
|
+
# Also some collections
|
34
|
+
collection :images, ImageView, -> (product) { product.variants.images }
|
35
|
+
|
36
|
+
# You could also pass a property as a proc
|
37
|
+
# In this case I will use the ImageView again
|
38
|
+
prop :cover, -> (product) { ImageView.generate(product.cover) }
|
39
|
+
end
|
40
|
+
|
41
|
+
# Get your Object ready
|
42
|
+
product = Product.first
|
43
|
+
|
44
|
+
# Transform it to JSON!
|
45
|
+
res = ProductView.to_json(product)
|
46
|
+
|
47
|
+
# Return to your framwork
|
48
|
+
[200, {'Content-Type' => 'application/json'}, res]
|
49
|
+
```
|
50
|
+
|
51
|
+
## Development
|
52
|
+
* Building the docker container: `docker build -t venduitz .`
|
53
|
+
* Running the tests:
|
54
|
+
* With volume: `docker run --rm -it -v (PWD):/venduitz venduitz bundle exec rspec`
|
55
|
+
* Without volume: `docker run --rm -it venduitz bundle exec rspec`
|
@@ -0,0 +1,70 @@
|
|
1
|
+
#
|
2
|
+
module Venduitz
|
3
|
+
#
|
4
|
+
class View
|
5
|
+
# Static methods
|
6
|
+
class << self
|
7
|
+
# Properties
|
8
|
+
attr_reader :props, :collects
|
9
|
+
|
10
|
+
# Define a property for the view
|
11
|
+
# @param {Symbol/String} name Name used as the JSON field
|
12
|
+
# @param {Proc} prc If its necessary pass a Proc to generate
|
13
|
+
# the value used by the generator
|
14
|
+
def prop(name, prc = nil)
|
15
|
+
# Initialize it if its necessary
|
16
|
+
@props = {} if @props.nil?
|
17
|
+
|
18
|
+
# Define!
|
19
|
+
@props[name] = prc.nil? ? name : prc
|
20
|
+
end
|
21
|
+
|
22
|
+
# Define a collection for it
|
23
|
+
def collection(name, view, prc = nil)
|
24
|
+
# Initialize it if its necessary
|
25
|
+
@collects = {} if @collects.nil?
|
26
|
+
|
27
|
+
# Define the collection
|
28
|
+
@collects[name] = {
|
29
|
+
view: view,
|
30
|
+
value: (prc.nil? ? name : prc)
|
31
|
+
}
|
32
|
+
end
|
33
|
+
|
34
|
+
# This method will generate the hash object
|
35
|
+
# based on the properties and the colletions
|
36
|
+
# for the specific argument
|
37
|
+
# @param {Object} obj This must contain the declared properies and collections
|
38
|
+
# @return {Hash} JSON ready hash containing the specified view fields
|
39
|
+
def generate(obj)
|
40
|
+
# Reset the values to prevent errors
|
41
|
+
@props = {} if @props.nil?
|
42
|
+
@collects = {} if @collects.nil?
|
43
|
+
|
44
|
+
# Return the properties
|
45
|
+
res = @props.map do |prop, value|
|
46
|
+
next [prop, obj.send(value)] if value.is_a?(Symbol)
|
47
|
+
[prop, value.call(obj)]
|
48
|
+
end
|
49
|
+
|
50
|
+
# Return the collections
|
51
|
+
coll = @collects.map do |collect, info|
|
52
|
+
values = info[:value].is_a?(Symbol) ? obj.send(info[:value]) : info[:value].call(obj)
|
53
|
+
next [collect, []] if values.nil? || values.empty?
|
54
|
+
[collect, values.map {|val| info[:view].to_json(val) }]
|
55
|
+
end
|
56
|
+
|
57
|
+
# Return the full hash
|
58
|
+
Hash[res].merge(Hash[coll])
|
59
|
+
end
|
60
|
+
|
61
|
+
# Parse it
|
62
|
+
# @param {Object} obj This must contain the declared properies and collections
|
63
|
+
# @return {Hash} The JSON string itself
|
64
|
+
def to_json(obj)
|
65
|
+
# Transform the result into json
|
66
|
+
MultiJson.dump generate(obj)
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
data/lib/venduitz.rb
ADDED
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,96 @@
|
|
1
|
+
# Include the helper
|
2
|
+
require 'spec_helper'
|
3
|
+
|
4
|
+
# Nested
|
5
|
+
class NestedView < Venduitz::View
|
6
|
+
prop :kind
|
7
|
+
prop :main, -> (o) { SubSampleView.generate(o.main) }
|
8
|
+
end
|
9
|
+
|
10
|
+
# Sub sample view
|
11
|
+
class SubSampleView < Venduitz::View
|
12
|
+
prop :name
|
13
|
+
end
|
14
|
+
|
15
|
+
# Sample view
|
16
|
+
class SampleView < Venduitz::View
|
17
|
+
# Define the property
|
18
|
+
prop :kind
|
19
|
+
prop :class, -> (o) { o.class }
|
20
|
+
collection :subs, SubSampleView
|
21
|
+
end
|
22
|
+
|
23
|
+
# Sample Class
|
24
|
+
class Sample
|
25
|
+
#
|
26
|
+
attr_accessor :subs, :main
|
27
|
+
|
28
|
+
#
|
29
|
+
def kind
|
30
|
+
'Nice!'
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
# Sub Sample class
|
35
|
+
class SubSample
|
36
|
+
def name
|
37
|
+
'Wow!'
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
#
|
42
|
+
describe Venduitz::View do
|
43
|
+
#
|
44
|
+
it 'should define the properties' do
|
45
|
+
expect(SampleView.props[:kind]).to eq(:kind)
|
46
|
+
expect(SampleView.props[:class]).to be_a(Proc)
|
47
|
+
end
|
48
|
+
|
49
|
+
#
|
50
|
+
it 'should define the collections' do
|
51
|
+
expect(SampleView.collects[:subs][:view]).to eq(SubSampleView)
|
52
|
+
expect(SampleView.collects[:subs][:value]).to eq(:subs)
|
53
|
+
end
|
54
|
+
|
55
|
+
#
|
56
|
+
it 'should generate the JSON of a simple view' do
|
57
|
+
# Generate the JSON object
|
58
|
+
res = SubSampleView.to_json(SubSample.new)
|
59
|
+
|
60
|
+
# Expectations
|
61
|
+
expect(res).to eq("{\"name\":\"Wow!\"}")
|
62
|
+
end
|
63
|
+
|
64
|
+
#
|
65
|
+
it 'should generate the JSON of a compund view' do
|
66
|
+
# Initialize the object
|
67
|
+
obj = Sample.new
|
68
|
+
|
69
|
+
# Add a sub object
|
70
|
+
obj.subs = [SubSample.new]
|
71
|
+
|
72
|
+
# Generate the JSON object
|
73
|
+
res = SampleView.to_json(obj)
|
74
|
+
|
75
|
+
# Expectations
|
76
|
+
expect(res).to eq("{\"kind\":\"Nice!\",\"class\":\"Sample\",\"subs\":[\"{\\\"name\\\":\\\"Wow!\\\"}\"]}")
|
77
|
+
end
|
78
|
+
|
79
|
+
#
|
80
|
+
context 'nested' do
|
81
|
+
#
|
82
|
+
it 'should generate the JSON of a simple view' do
|
83
|
+
# Initialize the object
|
84
|
+
obj = Sample.new
|
85
|
+
|
86
|
+
# Add a sub object
|
87
|
+
obj.main = SubSample.new
|
88
|
+
|
89
|
+
# Generate the JSON object
|
90
|
+
res = NestedView.to_json(obj)
|
91
|
+
|
92
|
+
# Expectations
|
93
|
+
expect(res).to eq("{\"kind\":\"Nice!\",\"main\":{\"name\":\"Wow!\"}}")
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
data/venduitz.gemspec
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
#
|
2
|
+
require './lib/venduitz/version'
|
3
|
+
|
4
|
+
#
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = 'venduitz'
|
7
|
+
s.version = Venduitz.version
|
8
|
+
|
9
|
+
s.summary = ''
|
10
|
+
s.description = ''
|
11
|
+
|
12
|
+
s.author = 'Gabriel Corado'
|
13
|
+
s.email = 'gabrielcorado@mail.com'
|
14
|
+
s.homepage = 'http://github.com/gabrielcorado/venduitz'
|
15
|
+
|
16
|
+
s.files = `git ls-files`.strip.split("\n")
|
17
|
+
s.executables = Dir["bin/*"].map { |f| File.basename(f) }
|
18
|
+
|
19
|
+
# Dependencies
|
20
|
+
# s.add_dependency 'concurrent-ruby', '~> 1.0'
|
21
|
+
# s.add_dependency 'docker-api', '~> 1.26'
|
22
|
+
s.add_dependency 'multi_json'
|
23
|
+
s.add_dependency 'oj'
|
24
|
+
|
25
|
+
# Development depencies
|
26
|
+
s.add_development_dependency 'rspec', '~> 3.0'
|
27
|
+
end
|
metadata
ADDED
@@ -0,0 +1,96 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: venduitz
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Gabriel Corado
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-05-03 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: multi_json
|
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: oj
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
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: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
55
|
+
description: ''
|
56
|
+
email: gabrielcorado@mail.com
|
57
|
+
executables: []
|
58
|
+
extensions: []
|
59
|
+
extra_rdoc_files: []
|
60
|
+
files:
|
61
|
+
- ".gitignore"
|
62
|
+
- ".travis.yml"
|
63
|
+
- Dockerfile
|
64
|
+
- Gemfile
|
65
|
+
- Gemfile.lock
|
66
|
+
- README.md
|
67
|
+
- lib/venduitz.rb
|
68
|
+
- lib/venduitz/version.rb
|
69
|
+
- lib/venduitz/view.rb
|
70
|
+
- spec/spec_helper.rb
|
71
|
+
- spec/venduitz/view_spec.rb
|
72
|
+
- venduitz.gemspec
|
73
|
+
homepage: http://github.com/gabrielcorado/venduitz
|
74
|
+
licenses: []
|
75
|
+
metadata: {}
|
76
|
+
post_install_message:
|
77
|
+
rdoc_options: []
|
78
|
+
require_paths:
|
79
|
+
- lib
|
80
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
81
|
+
requirements:
|
82
|
+
- - ">="
|
83
|
+
- !ruby/object:Gem::Version
|
84
|
+
version: '0'
|
85
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
requirements: []
|
91
|
+
rubyforge_project:
|
92
|
+
rubygems_version: 2.4.5.1
|
93
|
+
signing_key:
|
94
|
+
specification_version: 4
|
95
|
+
summary: ''
|
96
|
+
test_files: []
|