cerulean 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/cerulean.rb +134 -0
  3. metadata +44 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: a9683b9cc88aef379cb7ad023971b7725a509287
4
+ data.tar.gz: 102f8bdee4494c24b4d808cfc83d80fce7078796
5
+ SHA512:
6
+ metadata.gz: a5a1c1f8e8128308f0f033f4e5876a0e51f500bdbbd15d0450ac25b1d28961856ae022aa8c707a71bb31527a0e20315447a5eb944d45ac02584ab18ce3daa7ca
7
+ data.tar.gz: e26d6dc9a7ebd8f2f7196324b51bef32132368b5ee6dc2784ba06170285a32fa2d5841c7f15cdb00da8f4d80129706f009f0345493984ad254ddd36340f55ef6
@@ -0,0 +1,134 @@
1
+ module Boolean; end
2
+
3
+ class ActionDSL
4
+ def initialize(controller, name:, &block)
5
+ @controller = controller
6
+ @name = name
7
+ @meta = {}
8
+
9
+ instance_eval(&block)
10
+
11
+ # yuck
12
+ meta = @controller.class_variable_get(:@@meta)
13
+ meta[name] = @meta
14
+ @controller.class_variable_set(:@@meta, meta)
15
+ end
16
+
17
+ def params(&block)
18
+ instance_eval(&block)
19
+ end
20
+
21
+ def param(name, type=String, opts={})
22
+ @meta[:params] ||= {}
23
+ @meta[:params][name] = opts.merge({ type: type })
24
+ end
25
+
26
+ def presenter(presenter)
27
+ @meta[:presenter] = presenter
28
+ end
29
+
30
+ def form(key, klass)
31
+ @meta[:form] = { key: key, klass: klass }
32
+ end
33
+
34
+ def request(&block)
35
+ @controller.send(:define_method, @name, &block)
36
+ end
37
+ end
38
+
39
+ module Cerulean
40
+ def self.included(receiver)
41
+ receiver.extend(ClassMethods)
42
+ end
43
+
44
+ module ClassMethods
45
+ def get(name, &block)
46
+ endpoint(name, &block)
47
+ end
48
+
49
+ def post(name, &block)
50
+ endpoint(name, &block)
51
+ end
52
+
53
+ def endpoint(name, &block)
54
+ self.class_variable_set(:@@meta, {}) unless self.class_variable_defined?(:@@meta)
55
+
56
+ ActionDSL.new(self, name: name, &block)
57
+
58
+ self.class_exec do
59
+ def declared()
60
+ @declared ||= {} #Hashie::Mash.new(params)
61
+ end
62
+
63
+ def present(object)
64
+ if object.is_a?(Array) || object.is_a?(ActiveRecord::Relation)
65
+ render json: object.map { |el| meta[:presenter].new(el).as_json }
66
+ else
67
+ render json: meta[:presenter].new(object).as_json
68
+ end
69
+ end
70
+
71
+ def meta
72
+ @meta ||= self.class.class_variable_get(:@@meta)[action_name.to_sym]
73
+ end
74
+
75
+ def validate_param_type(val, type)
76
+ case type.to_s
77
+ when 'Integer'
78
+ Integer(val) rescue false
79
+ when 'Float'
80
+ Float(val) rescue false
81
+ when 'Boolean'
82
+ %w(true false).include?(val.downcase)
83
+ when 'Array[Integer]'
84
+ !val.any? { |el| !Integer(el) rescue true }
85
+ when 'Array[Float]'
86
+ !val.any? { |el| !Float(el) rescue true }
87
+ when 'Array[Boolean]'
88
+ !val.any? { |el| %w(true false).include?(el.downcase) }
89
+ else
90
+ true
91
+ end
92
+ end
93
+
94
+ before_action do
95
+ if meta.has_key?(:form)
96
+ @form = meta[:form][:klass].new(params[meta[:form][:key]])
97
+ end
98
+
99
+ if meta.has_key?(:params)
100
+ @declared = {}
101
+ meta[:params].each do |param, opts|
102
+ if params.has_key?(param.to_s)
103
+ raise "#{param} is not a #{opts[:type]}" unless validate_param_type(params[param.to_s], opts[:type])
104
+
105
+ if opts.has_key?(:min)
106
+ if Float(params[param]) < opts[:min]
107
+ raise "#{param} must be greater than or equal to #{opts[:min]}"
108
+ end
109
+ end
110
+
111
+ if opts.has_key?(:max)
112
+ if Float(params[param]) > opts[:max]
113
+ raise "#{param} must be less than or equal to #{opts[:max]}"
114
+ end
115
+ end
116
+
117
+ if opts.has_key?(:values)
118
+ unless opts[:values].include?(params[param])
119
+ raise "#{param} must be one of #{opts[:values]}"
120
+ end
121
+ end
122
+
123
+ @declared[param] = params[param]
124
+ else
125
+ params[param] = opts[:default] if opts.has_key?(:default)
126
+ raise "#{param} is required" if opts[:required]
127
+ end
128
+ end
129
+ end
130
+ end
131
+ end
132
+ end
133
+ end
134
+ end
metadata ADDED
@@ -0,0 +1,44 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cerulean
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Kyle Kestell
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-07-27 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: REST API DSL.
14
+ email: kyle@kestell.org
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/cerulean.rb
20
+ homepage: https://github.com/kkestell/cerulean
21
+ licenses:
22
+ - MIT
23
+ metadata: {}
24
+ post_install_message:
25
+ rdoc_options: []
26
+ require_paths:
27
+ - lib
28
+ required_ruby_version: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: 2.0.0
33
+ required_rubygems_version: !ruby/object:Gem::Requirement
34
+ requirements:
35
+ - - ">="
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ requirements: []
39
+ rubyforge_project:
40
+ rubygems_version: 2.4.8
41
+ signing_key:
42
+ specification_version: 4
43
+ summary: Cerulean
44
+ test_files: []