venduitz 0.0.0 → 0.2.0

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b6dac980ab2b8be2b8abf9dbeb46e7d3cfc65a12
4
- data.tar.gz: 313d678a9e19b38b8974cf7e4b87b81e287839cd
3
+ metadata.gz: 6cc6a028fa63bd40f96535d661fc2a8452d96148
4
+ data.tar.gz: b583d6e4bf160b0b4b7c705439ef9f43711aed07
5
5
  SHA512:
6
- metadata.gz: c65507fd4c7003b689382b8e863f2ea5ae0071035df5a18046df8e0144cfcd1a44f40d64392ec8e6b708deb195c7e9c8c60e83cc05caa46fe0bf8b545b468057
7
- data.tar.gz: 311d28bda3f9754a84dc59c354dc623f8b84f06ad6512626152ede825c8f9801b28e2a77ea96d722b749f4336df12540fd752638f5231e8546497d9f0488b211
6
+ metadata.gz: 96466bc3eb64cc8bdb8b6f3f244168c644c19d617c8db78d0a0f8e9229ee3e5754a9156ea7e6c3574455ed55fe093ed4c0431c6817a412f50fb0acef051afa94
7
+ data.tar.gz: ee9aeb8158cb506316a62471ee525f205bdc4b184f65e518268942f524bf368dfc836a9ff5346374b6112f5637d60f2b2bbf1dc0482c0a8c96ae9a15b6c29b52
data/.gitignore CHANGED
@@ -1,2 +1,2 @@
1
- *.gemspec
1
+ *.gem
2
2
  .DS_Store
data/Gemfile CHANGED
@@ -1,2 +1,9 @@
1
+ # Source
1
2
  source 'https://rubygems.org'
3
+
4
+ # Some test dependencies
5
+ gem 'grape'
6
+ gem 'rack-test'
7
+
8
+ # Add the Gem dependencies
2
9
  gemspec
data/Gemfile.lock CHANGED
@@ -1,16 +1,56 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- venduitz (0.0.0)
4
+ venduitz (0.1.0)
5
5
  multi_json
6
6
  oj
7
7
 
8
8
  GEM
9
9
  remote: https://rubygems.org/
10
10
  specs:
11
+ activesupport (4.2.6)
12
+ i18n (~> 0.7)
13
+ json (~> 1.7, >= 1.7.7)
14
+ minitest (~> 5.1)
15
+ thread_safe (~> 0.3, >= 0.3.4)
16
+ tzinfo (~> 1.1)
17
+ axiom-types (0.1.1)
18
+ descendants_tracker (~> 0.0.4)
19
+ ice_nine (~> 0.11.0)
20
+ thread_safe (~> 0.3, >= 0.3.1)
21
+ builder (3.2.2)
22
+ coercible (1.0.0)
23
+ descendants_tracker (~> 0.0.1)
24
+ descendants_tracker (0.0.4)
25
+ thread_safe (~> 0.3, >= 0.3.1)
11
26
  diff-lcs (1.2.5)
12
- multi_json (1.11.3)
27
+ enumerable-lazy (0.0.1)
28
+ equalizer (0.0.11)
29
+ grape (0.16.2)
30
+ activesupport
31
+ builder
32
+ hashie (>= 2.1.0)
33
+ multi_json (>= 1.3.2)
34
+ multi_xml (>= 0.5.2)
35
+ mustermann19 (~> 0.4.3)
36
+ rack (>= 1.3.0)
37
+ rack-accept
38
+ virtus (>= 1.0.0)
39
+ hashie (3.4.4)
40
+ i18n (0.7.0)
41
+ ice_nine (0.11.2)
42
+ json (1.8.3)
43
+ minitest (5.8.4)
44
+ multi_json (1.12.0)
45
+ multi_xml (0.5.5)
46
+ mustermann19 (0.4.3)
47
+ enumerable-lazy
13
48
  oj (2.15.0)
49
+ rack (1.6.4)
50
+ rack-accept (0.4.5)
51
+ rack (>= 0.4)
52
+ rack-test (0.6.3)
53
+ rack (>= 1.0)
14
54
  rspec (3.4.0)
15
55
  rspec-core (~> 3.4.0)
16
56
  rspec-expectations (~> 3.4.0)
@@ -24,11 +64,21 @@ GEM
24
64
  diff-lcs (>= 1.2.0, < 2.0)
25
65
  rspec-support (~> 3.4.0)
26
66
  rspec-support (3.4.1)
67
+ thread_safe (0.3.5)
68
+ tzinfo (1.2.2)
69
+ thread_safe (~> 0.1)
70
+ virtus (1.0.5)
71
+ axiom-types (~> 0.1)
72
+ coercible (~> 1.0)
73
+ descendants_tracker (~> 0.0, >= 0.0.3)
74
+ equalizer (~> 0.0, >= 0.0.9)
27
75
 
28
76
  PLATFORMS
29
77
  ruby
30
78
 
31
79
  DEPENDENCIES
80
+ grape
81
+ rack-test
32
82
  rspec (~> 3.0)
33
83
  venduitz!
34
84
 
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # Venduitz [![Build Status](https://travis-ci.org/gabrielcorado/venduitz.svg?branch=develop)](https://travis-ci.org/gabrielcorado/venduitz)
1
+ # Venduitz [![Build Status](https://travis-ci.org/gabrielcorado/venduitz.svg?branch=develop)](https://travis-ci.org/gabrielcorado/venduitz) [![Gem Version](https://badge.fury.io/rb/venduitz.svg)](https://badge.fury.io/rb/venduitz)
2
2
  A simple JSON-only(until now) template engine, with focus on performance for better Restful-APIs.
3
3
 
4
4
  ## How to use it
@@ -14,6 +14,14 @@ gem 'venduitz'
14
14
  ```ruby
15
15
  require 'venduitz'
16
16
 
17
+ class MetaView < Venduitz::View
18
+ # Define the MEta view properties
19
+ prop :current, -> (search) { search.current_page }
20
+ prop :total, -> (search) { search.total_count }
21
+ prop :total_pages
22
+ prop :per_page
23
+ end
24
+
17
25
  # Image view
18
26
  class ImageView < Venduitz::View
19
27
  # Define the view properties
@@ -31,18 +39,32 @@ class ProductView < Venduitz::View
31
39
  prop :price
32
40
 
33
41
  # Also some collections
34
- collection :images, ImageView, -> (product) { product.variants.images }
42
+ collection :images, ImageView, value: -> (product) { product.variants.images }
35
43
 
36
44
  # You could also pass a property as a proc
37
45
  # In this case I will use the ImageView again
38
46
  prop :cover, -> (product) { ImageView.generate(product.cover) }
39
47
  end
40
48
 
49
+ # Its also possible to exclude collection/prop values when generating it
50
+ # in this way you could generate a Ember-like result
51
+ class SearchView < Venduitz::View
52
+ # Define the view properties
53
+ prop :meta, -> (search) { MetaView.generate(search) }
54
+
55
+ collection :products, ProductView, value: -> (search) { search.products }, exclude: [:images]
56
+ collection :images, ImageView, value: -> (search) { search.products.images }
57
+ end
58
+
41
59
  # Get your Object ready
42
- product = Product.first
60
+ search = Product.search 'Hello'
43
61
 
44
62
  # Transform it to JSON!
45
- res = ProductView.to_json(product)
63
+ res = SearchView.to_json(search)
64
+
65
+ # OR... If you don't want to show the images for this search
66
+ # just exclude them in the generation method
67
+ res = SearchView.to_json(search, [:images])
46
68
 
47
69
  # Return to your framwork
48
70
  [200, {'Content-Type' => 'application/json'}, res]
@@ -0,0 +1,17 @@
1
+ #
2
+ module Venduitz
3
+ #
4
+ module Grape
5
+ # Call method
6
+ def self.call(object, env)
7
+ # Define the Venduitz representer
8
+ representer = env['api.endpoint'].options.fetch(:route_options, {})[:venduitz]
9
+
10
+ # Must define the Representer in the endpoint
11
+ raise 'Must define the Venduitz in the endpoind' if representer.nil?
12
+
13
+ # Render it!
14
+ representer.to_json object
15
+ end
16
+ end
17
+ end
@@ -2,6 +2,6 @@
2
2
  module Venduitz
3
3
  # Gem version
4
4
  def self.version
5
- '0.0.0'.freeze
5
+ '0.2.0'.freeze
6
6
  end
7
7
  end
data/lib/venduitz/view.rb CHANGED
@@ -20,14 +20,15 @@ module Venduitz
20
20
  end
21
21
 
22
22
  # Define a collection for it
23
- def collection(name, view, prc = nil)
23
+ def collection(name, view, opts = {})
24
24
  # Initialize it if its necessary
25
25
  @collects = {} if @collects.nil?
26
26
 
27
27
  # Define the collection
28
28
  @collects[name] = {
29
29
  view: view,
30
- value: (prc.nil? ? name : prc)
30
+ value: (opts.key?(:value) ? opts[:value] : name),
31
+ excluded: (opts.key?(:exclude) ? opts[:exclude] : [])
31
32
  }
32
33
  end
33
34
 
@@ -35,35 +36,41 @@ module Venduitz
35
36
  # based on the properties and the colletions
36
37
  # for the specific argument
37
38
  # @param {Object} obj This must contain the declared properies and collections
39
+ # @param {Array} excluded This array contain the keys that will not be added to the result
38
40
  # @return {Hash} JSON ready hash containing the specified view fields
39
- def generate(obj)
41
+ def generate(obj, excluded = [])
40
42
  # Reset the values to prevent errors
41
43
  @props = {} if @props.nil?
42
44
  @collects = {} if @collects.nil?
43
45
 
46
+ # Map both props and collects
47
+ props = @props.select { |prop, value| !excluded.include?(prop) }
48
+ coll = @collects.select { |prop, value| !excluded.include?(prop) }
49
+
44
50
  # Return the properties
45
- res = @props.map do |prop, value|
51
+ props = props.map do |prop, value|
46
52
  next [prop, obj.send(value)] if value.is_a?(Symbol)
47
53
  [prop, value.call(obj)]
48
54
  end
49
55
 
50
56
  # Return the collections
51
- coll = @collects.map do |collect, info|
57
+ coll = coll.map do |collect, info|
52
58
  values = info[:value].is_a?(Symbol) ? obj.send(info[:value]) : info[:value].call(obj)
53
59
  next [collect, []] if values.nil? || values.empty?
54
- [collect, values.map {|val| info[:view].to_json(val) }]
60
+ [collect, values.map {|val| info[:view].generate(val, info[:excluded]) }]
55
61
  end
56
62
 
57
63
  # Return the full hash
58
- Hash[res].merge(Hash[coll])
64
+ Hash[props].merge(Hash[coll])
59
65
  end
60
66
 
61
67
  # Parse it
62
68
  # @param {Object} obj This must contain the declared properies and collections
69
+ # @param {Array} excluded This array contain the keys that will not be added to the result
63
70
  # @return {Hash} The JSON string itself
64
- def to_json(obj)
71
+ def to_json(obj, excluded = [])
65
72
  # Transform the result into json
66
- MultiJson.dump generate(obj)
73
+ MultiJson.dump generate(obj, excluded)
67
74
  end
68
75
  end
69
76
  end
data/spec/spec_helper.rb CHANGED
@@ -1,8 +1,54 @@
1
1
  # Include Venduitz
2
2
  require 'venduitz'
3
+ require 'rack/test'
3
4
 
4
5
  # Rspec conf
5
6
  RSpec.configure do |config|
6
7
  config.order = 'random'
7
8
  config.seed = '12345'
9
+ config.include Rack::Test::Methods
10
+ end
11
+
12
+ # Nested
13
+ class NestedView < Venduitz::View
14
+ prop :kind
15
+ prop :main, -> (o) { SubSampleView.generate(o.main) }
16
+ end
17
+
18
+ # Sub sample view
19
+ class SubSampleView < Venduitz::View
20
+ prop :name
21
+ prop :other
22
+ end
23
+
24
+ # Sample view
25
+ class SampleView < Venduitz::View
26
+ # Define the property
27
+ prop :kind
28
+ prop :class, -> (o) { o.class }
29
+ collection :subs, SubSampleView, exclude: [:other]
30
+ end
31
+
32
+ # Excluded SampleView
33
+
34
+ # Sample Class
35
+ class Sample
36
+ #
37
+ attr_accessor :subs, :main
38
+
39
+ #
40
+ def kind
41
+ 'Nice!'
42
+ end
43
+ end
44
+
45
+ # Sub Sample class
46
+ class SubSample
47
+ def name
48
+ 'Wow!'
49
+ end
50
+
51
+ def other
52
+ 'Here I come!'
53
+ end
8
54
  end
@@ -0,0 +1,31 @@
1
+ # Dependencies
2
+ require 'spec_helper'
3
+ require 'venduitz/grape'
4
+ require 'grape'
5
+
6
+ #
7
+ describe Venduitz::Grape do
8
+ # Create a Grape APP
9
+ let(:app) { Class.new(Grape::API) }
10
+
11
+ # Define the app configs
12
+ before do
13
+ app.format :json
14
+ app.formatter :json, Venduitz::Grape
15
+ end
16
+
17
+ #
18
+ it 'should generate the response for the Grape endpoint' do
19
+ # Generate the JSON object
20
+ app.get('/venduitz', venduitz: SubSampleView) do
21
+ # Return the proper object
22
+ SubSample.new
23
+ end
24
+
25
+ # Execute it
26
+ get '/venduitz'
27
+
28
+ # Expectations
29
+ expect(last_response.body).to eq("{\"name\":\"Wow!\",\"other\":\"Here I come!\"}")
30
+ end
31
+ end
@@ -1,45 +1,12 @@
1
1
  # Include the helper
2
2
  require 'spec_helper'
3
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
4
  #
42
5
  describe Venduitz::View do
6
+ #
7
+ let(:obj) { Sample.new }
8
+ let(:sub_obj) { o = Sample.new; o.subs = [SubSample.new]; o }
9
+
43
10
  #
44
11
  it 'should define the properties' do
45
12
  expect(SampleView.props[:kind]).to eq(:kind)
@@ -58,22 +25,25 @@ describe Venduitz::View do
58
25
  res = SubSampleView.to_json(SubSample.new)
59
26
 
60
27
  # Expectations
61
- expect(res).to eq("{\"name\":\"Wow!\"}")
28
+ expect(res).to eq("{\"name\":\"Wow!\",\"other\":\"Here I come!\"}")
62
29
  end
63
30
 
64
31
  #
65
- it 'should generate the JSON of a compund view' do
66
- # Initialize the object
67
- obj = Sample.new
32
+ it 'should exclude values from the view' do
33
+ # Gen the JSON
34
+ res = SubSampleView.to_json(SubSample.new, [:other])
68
35
 
69
- # Add a sub object
70
- obj.subs = [SubSample.new]
36
+ # Expectations
37
+ expect(res).to eq("{\"name\":\"Wow!\"}")
38
+ end
71
39
 
40
+ #
41
+ it 'should generate the JSON of a compund view' do
72
42
  # Generate the JSON object
73
- res = SampleView.to_json(obj)
43
+ res = SampleView.to_json(sub_obj)
74
44
 
75
45
  # Expectations
76
- expect(res).to eq("{\"kind\":\"Nice!\",\"class\":\"Sample\",\"subs\":[\"{\\\"name\\\":\\\"Wow!\\\"}\"]}")
46
+ expect(res).to eq("{\"kind\":\"Nice!\",\"class\":\"Sample\",\"subs\":[{\"name\":\"Wow!\"}]}")
77
47
  end
78
48
 
79
49
  #
@@ -90,7 +60,7 @@ describe Venduitz::View do
90
60
  res = NestedView.to_json(obj)
91
61
 
92
62
  # Expectations
93
- expect(res).to eq("{\"kind\":\"Nice!\",\"main\":{\"name\":\"Wow!\"}}")
63
+ expect(res).to eq("{\"kind\":\"Nice!\",\"main\":{\"name\":\"Wow!\",\"other\":\"Here I come!\"}}")
94
64
  end
95
65
  end
96
66
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: venduitz
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gabriel Corado
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-05-03 00:00:00.000000000 Z
11
+ date: 2016-05-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: multi_json
@@ -65,9 +65,11 @@ files:
65
65
  - Gemfile.lock
66
66
  - README.md
67
67
  - lib/venduitz.rb
68
+ - lib/venduitz/grape.rb
68
69
  - lib/venduitz/version.rb
69
70
  - lib/venduitz/view.rb
70
71
  - spec/spec_helper.rb
72
+ - spec/venduitz/grape_spec.rb
71
73
  - spec/venduitz/view_spec.rb
72
74
  - venduitz.gemspec
73
75
  homepage: http://github.com/gabrielcorado/venduitz