towsta 1.3.0 → 2.0.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.
Files changed (53) hide show
  1. data/Rakefile +3 -0
  2. data/lib/towsta/envs/development.rb +7 -0
  3. data/lib/towsta/envs/production.rb +13 -0
  4. data/lib/towsta/envs/test.rb +5 -0
  5. data/lib/towsta/kinds/boolean.rb +2 -1
  6. data/lib/towsta/kinds/formated.rb +0 -4
  7. data/lib/towsta/kinds/gallery.rb +2 -3
  8. data/lib/towsta/kinds/money.rb +6 -0
  9. data/lib/towsta/kinds/multiple.rb +3 -10
  10. data/lib/towsta/kinds/user.rb +6 -8
  11. data/lib/towsta/kinds/vertical.rb +5 -9
  12. data/lib/towsta/memory.rb +2 -4
  13. data/lib/towsta/sinatra_extension.rb +1 -1
  14. data/lib/towsta/synchronizer.rb +29 -32
  15. data/lib/towsta/version.rb +1 -1
  16. data/lib/towsta/vertical-core/attributes.rb +52 -0
  17. data/lib/towsta/vertical-core/base.rb +24 -0
  18. data/lib/towsta/vertical-core/crud.rb +41 -0
  19. data/lib/towsta/vertical-core/locales.rb +21 -0
  20. data/lib/towsta/vertical-core/mail.rb +17 -0
  21. data/lib/towsta/vertical-core/references.rb +44 -0
  22. data/lib/towsta/vertical.rb +19 -134
  23. data/lib/towsta.rb +9 -21
  24. data/spec/kinds/boolean_spec.rb +54 -0
  25. data/spec/kinds/date_spec.rb +37 -0
  26. data/spec/kinds/datetime_spec.rb +37 -0
  27. data/spec/kinds/gallery_spec.rb +43 -0
  28. data/spec/kinds/image_spec.rb +39 -0
  29. data/spec/kinds/integer_spec.rb +33 -0
  30. data/spec/kinds/list_spec.rb +37 -0
  31. data/spec/kinds/main_spec.rb +24 -0
  32. data/spec/kinds/money_spec.rb +37 -0
  33. data/spec/kinds/multiple_spec.rb +41 -0
  34. data/spec/kinds/user_spec.rb +41 -0
  35. data/spec/kinds/vertical_spec.rb +41 -0
  36. data/spec/kinds/video_spec.rb +37 -0
  37. data/spec/spec_helper.rb +9 -0
  38. data/spec/synchronizer/base_spec.rb +6 -0
  39. data/spec/synchronizer/cache_string_spec.rb +25 -0
  40. data/spec/synchronizer/has_secret_spec.rb +25 -0
  41. data/spec/synchronizer/parse_json_spec.rb +26 -0
  42. data/spec/synchronizer/populate_verticals_spec.rb +25 -0
  43. data/spec/synchronizer/populate_verticals_spec.rb~ +25 -0
  44. data/spec/synchronizer/remote_string_spec.rb +25 -0
  45. data/spec/synchronizer/solve_params_spec.rb +28 -0
  46. data/spec/synchronizer/validate_response_spec.rb +25 -0
  47. data/spec/synchronizer/validate_secret_spec.rb +25 -0
  48. data/spec/towsta_spec.rb +7 -0
  49. data/spec/vertical/vertical_spec.rb +17 -0
  50. data/spec/webmock.json +1 -0
  51. data/towsta.gemspec +19 -13
  52. metadata +90 -16
  53. data/lib/towsta/string_extension.rb +0 -5
@@ -2,144 +2,29 @@ module Towsta
2
2
 
3
3
  class Vertical
4
4
 
5
- class << self
6
- attr_accessor :all
7
- end
8
-
9
- def self.create(args)
10
- klass = Class.new do
11
-
12
- class << self
13
- attr_accessor :all, :attributes, :count
14
- end
15
-
16
- attr_accessor :message
17
-
18
- include R18n::Helpers
19
-
20
- def i18n attr
21
- self.send :"#{attr.to_s}_#{R18n.get.locales.first.code}"
22
- end
23
-
24
- def to_mail
25
- string = ''
26
- meta_attributes.each do |attribute, value|
27
- string << "<b>#{attribute.to_s}:</b> #{value} <br/>"
28
- end
29
- string
30
- end
31
-
32
- def meta_attributes
33
- a = attributes.clone
34
- a.delete :author
35
- a.delete :created_at
36
- a.delete :updated_at
37
- a.delete :id
38
- a
39
- end
40
-
41
- def notify args
42
- Pony.mail({:html_body => to_mail}.merge(args))
43
- end
44
-
45
- args[:slices].each do |attr, kind|
46
- eval "def object_of_#{attr}; @#{attr}; end;"
47
- eval "def #{attr}= value; @#{attr} ||= Towsta::Kinds::#{kind[0].upcase + kind[1..-1]}Kind.new; @#{attr}.set value; end;"
48
- eval "def #{attr}; @#{attr}.get; end;"
49
- eval "def self.find_by_#{attr} value; self.all.each {|e| return e if e.object_of_#{attr}.compare value}; nil; end;"
50
- eval "def self.find_all_by_#{attr} value; found =[]; self.all.each {|e| found << e if e.object_of_#{attr}.compare value}; found; end;"
51
- eval "def option_for_#{attr} value; return {value: value, selected: 'selected'} if value == #{attr}; {value: value}; end"
52
- end
53
-
54
- def self.first
55
- self.all.first
56
- end
57
-
58
- def self.last
59
- self.all.last
60
- end
61
-
62
- def self.find id
63
- self.find_by_id id
64
- end
65
-
66
- def initialize args
67
- self.attributes.merge(args).each {|k,v| self.send("#{k}=".to_sym, v)}
68
- self.class.all << self
69
- end
5
+ cattr_accessor :all
70
6
 
71
- def update args, author=$towsta_default_author
72
- args.each {|k,v| self.send("#{k}=".to_sym, v)}
73
- self.save author
74
- end
7
+ self.all ||= []
75
8
 
76
- def destroy
77
- # self.class.all.delete self
78
- # self
79
- end
80
-
81
- def self.to_hash
82
- hashes = []
83
- self.all.each {|hash| hashes << hash.attributes}
84
- hashes
85
- end
86
-
87
- def save creator=$towsta_default_author
88
- creator = author.email if author
89
- export = self.attributes
90
- export.delete :author
91
- export.delete :created_at
92
- export.delete :updated_at
93
- id_aux = export.delete(:id)
94
- id_aux ? id_aux : '0'
95
- export = {:creator => creator, :vertical => self.class.to_s, :attributes => export, :id => id_aux}
96
- response = Towsta::Synchronizer.save_request export
97
- @message = response[:message]
98
- self.id = response[:id] if response[:status]
99
- self.author = User.find_by_email creator
100
- Towsta::Memory.flush if $towsta_cache
101
- response[:status]
102
- end
103
-
104
- def self.random
105
- position = (self.all.size) - 1
106
- self.all[rand(position)]
107
- end
108
-
109
- def self.create args, creator=$towsta_default_author
110
- new = self.new(args.merge(:id => nil))
111
- new.save creator
112
- new
113
- end
114
-
115
- def attributes
116
- horizontal = {}
117
- self.class.attributes.each do |attr|
118
- slice = eval("self.object_of_#{attr.to_s}")
119
- horizontal[attr] = slice ? slice.export : nil
120
- end
121
- horizontal
122
- end
123
-
124
- def self.add_occurrence occurrence
125
- self.send(:define_singleton_method, "occurrences_of_#{occurrence[:name].downcase}") do
126
- eval occurrence[:items].inspect
127
- end
128
- end
129
-
130
- def method_missing(meth, *args, &block)
131
- begin
132
- return self.i18n meth.to_sym
133
- rescue
134
- super
135
- end
136
- end
137
-
138
- end
9
+ def self.create args
10
+ klass = Class.new(VerticalCore) { args[:slices].each { |attr, kind| define_attribute attr, kind } }
139
11
  klass.all = []
140
- klass.count = args[:count]
141
- klass.attributes = args[:slices].keys
12
+ Vertical.all << klass
142
13
  Object.const_set args[:name], klass
14
+ puts " class #{args[:name]} was created"
15
+ end
16
+
17
+ def self.populate classname, horizontals, occurrences = []
18
+ klass = Kernel.const_get classname.to_s
19
+ klass.all = []
20
+ horizontals.each {|horizontal| klass.new(horizontal)}
21
+ occurrences.each {|occurrence| klass.add_occurrence(occurrence)}
22
+ puts " class #{classname} was populated with #{horizontals.size} instances"
23
+ end
24
+
25
+ def self.clear
26
+ Vertical.all.each {|vertical| Object.instance_eval{ remove_const vertical.to_s.to_sym }}
27
+ Vertical.all = []
143
28
  end
144
29
 
145
30
  end
data/lib/towsta.rb CHANGED
@@ -1,5 +1,4 @@
1
1
  #coding: utf-8
2
- require 'dalli'
3
2
  require 'digest/md5'
4
3
  require 'net/http'
5
4
  require 'cgi'
@@ -14,12 +13,17 @@ require 'compass'
14
13
  require 'sinatra/content_for'
15
14
  require 'i18n-router'
16
15
  require 'pony'
16
+ require 'active_support/all'
17
17
 
18
- require File.expand_path('../towsta/string_extension', __FILE__)
18
+ require File.expand_path('../towsta/vertical-core/base', __FILE__)
19
+ require File.expand_path('../towsta/vertical-core/attributes', __FILE__)
20
+ require File.expand_path('../towsta/vertical-core/crud', __FILE__)
21
+ require File.expand_path('../towsta/vertical-core/locales', __FILE__)
22
+ require File.expand_path('../towsta/vertical-core/mail', __FILE__)
23
+ require File.expand_path('../towsta/vertical-core/references', __FILE__)
19
24
  require File.expand_path('../towsta/kinds/main', __FILE__)
20
25
  require File.expand_path('../towsta/vertical', __FILE__)
21
26
  require File.expand_path('../towsta/synchronizer', __FILE__)
22
- require File.expand_path('../towsta/memory', __FILE__)
23
27
  require File.expand_path('../towsta/sinatra_extension', __FILE__)
24
28
  require File.expand_path('../towsta/login', __FILE__)
25
29
  require File.expand_path('../towsta/kinds/boolean', __FILE__)
@@ -40,24 +44,8 @@ require File.expand_path('../towsta/kinds/vertical', __FILE__)
40
44
  require File.expand_path('../towsta/kinds/video', __FILE__)
41
45
  require File.expand_path('../towsta/kinds/multiple', __FILE__)
42
46
 
43
- Dir["./controllers/*.rb"].each {|file| require file}
44
-
45
47
  module Towsta
48
+ mattr_accessor :secret, :global, :author
46
49
  end
47
50
 
48
- def sync_with_towsta params=nil
49
- params ||= {} if $towsta_sync
50
- $towsta_sync ||= {}
51
- params = $towsta_sync.merge(params) if params
52
- $towsta_cache ||= production?
53
- if $towsta_cache
54
- Towsta::Memory.recover params
55
- else
56
- Towsta::Synchronizer.new :secret => $towsta_secret, :path => $towsta_path, :params => params
57
- end
58
- Dir["./models/*.rb"].each {|file| load file }
59
- end
60
-
61
- def clear_sync
62
- Towsta::Memory.flush
63
- end
51
+ require File.expand_path("../towsta/envs/#{ENV['RACK_ENV'].downcase}", __FILE__)
@@ -0,0 +1,54 @@
1
+ describe Towsta::Kinds::BooleanKind do
2
+
3
+ context 'when sets with "1"' do
4
+
5
+ subject {Towsta::Kinds::BooleanKind.new '1'}
6
+
7
+ its(:get) {should == true}
8
+
9
+ its(:export) {should == '1'}
10
+
11
+ end
12
+
13
+ context 'when sets with "0"' do
14
+
15
+ subject {Towsta::Kinds::BooleanKind.new '0'}
16
+
17
+ its(:get) {should == false}
18
+
19
+ its(:export) {should == '0'}
20
+
21
+ end
22
+
23
+ context 'when sets with "Test String"' do
24
+
25
+ subject {Towsta::Kinds::BooleanKind.new 'Test String'}
26
+
27
+ its(:get) {should == false}
28
+
29
+ its(:export) {should == '0'}
30
+
31
+ end
32
+
33
+ context 'when sets with True' do
34
+
35
+ subject {Towsta::Kinds::BooleanKind.new true}
36
+
37
+ its(:get) {should == true}
38
+
39
+ its(:export) {should == '1'}
40
+
41
+ end
42
+
43
+ context 'when sets with False' do
44
+
45
+ subject {Towsta::Kinds::BooleanKind.new false}
46
+
47
+ its(:get) {should == false}
48
+
49
+ its(:export) {should == '0'}
50
+
51
+ end
52
+
53
+ end
54
+
@@ -0,0 +1,37 @@
1
+ describe Towsta::Kinds::DateKind do
2
+
3
+ let(:input) {'10/16/1992'}
4
+ let(:format) {'%m/%d/%Y'}
5
+ let(:date) {DateTime.strptime('10/16/1992', '%m/%d/%Y')}
6
+
7
+ context 'when sets with %m/%d/%Y' do
8
+
9
+ subject {Towsta::Kinds::DateKind.new input}
10
+
11
+ its(:get) {should == date}
12
+
13
+ its(:export) {should == input}
14
+
15
+ end
16
+
17
+ context 'when sets with a bad string' do
18
+
19
+ subject {Towsta::Kinds::DateKind.new 'Bad String'}
20
+
21
+ its(:get) {should be_nil}
22
+
23
+ its(:export) {should be_blank}
24
+
25
+ end
26
+
27
+ context 'when sets with a DateTime' do
28
+
29
+ subject {Towsta::Kinds::DateKind.new date}
30
+
31
+ its(:get) {should == date}
32
+
33
+ its(:export) {should == input}
34
+
35
+ end
36
+
37
+ end
@@ -0,0 +1,37 @@
1
+ describe Towsta::Kinds::DatetimeKind do
2
+
3
+ let(:input) {'10/16/1992 00:00'}
4
+ let(:format) {'%m/%d/%Y %H:%M'}
5
+ let(:datetime) {DateTime.strptime(input, format).to_time}
6
+
7
+ context 'when sets with %m/%d/%Y %H:%M' do
8
+
9
+ subject {Towsta::Kinds::DatetimeKind.new input}
10
+
11
+ its(:get) {should == datetime}
12
+
13
+ #its(:export) {should == input}
14
+
15
+ end
16
+
17
+ context 'when sets with a bad string' do
18
+
19
+ subject {Towsta::Kinds::DatetimeKind.new 'Bad String'}
20
+
21
+ its(:get) {should be_nil}
22
+
23
+ its(:export) {should be_blank}
24
+
25
+ end
26
+
27
+ context 'when sets with a DateTime' do
28
+
29
+ subject {Towsta::Kinds::DatetimeKind.new datetime}
30
+
31
+ its(:get) {should == datetime}
32
+
33
+ #its(:export) {should == input}
34
+
35
+ end
36
+
37
+ end
@@ -0,0 +1,43 @@
1
+ describe Towsta::Kinds::GalleryKind do
2
+
3
+ let(:input) {[
4
+ {reference: 'upload', source: 'URLImage', link: 'mypicture.png'},
5
+ {reference: 'upload', source: 'URLImage', link: 'otherpicture.png'}
6
+ ]}
7
+
8
+ let(:gallery){[
9
+ Bresson::ImageReference.new(input.first),
10
+ Bresson::ImageReference.new(input.last)
11
+ ]}
12
+
13
+ context 'when sets with json' do
14
+
15
+ subject {Towsta::Kinds::GalleryKind.new input.to_json}
16
+
17
+ #its(:get) {should == gallery}
18
+
19
+ its(:export) {should == input.to_json}
20
+
21
+ end
22
+
23
+ context 'when sets with a bad string' do
24
+
25
+ subject {Towsta::Kinds::GalleryKind.new 'Bad String'}
26
+
27
+ its(:get) {should == []}
28
+
29
+ its(:export) {should be_blank}
30
+
31
+ end
32
+
33
+ context 'when sets with a Gallery' do
34
+
35
+ subject {Towsta::Kinds::GalleryKind.new gallery}
36
+
37
+ its(:get) {should == gallery}
38
+
39
+ its(:export) {should == input.to_json}
40
+
41
+ end
42
+
43
+ end
@@ -0,0 +1,39 @@
1
+ describe Towsta::Kinds::ImageKind do
2
+
3
+ let(:input) {{reference: 'upload', source: 'URLImage', link: 'mypicture.png'}}
4
+
5
+ let(:json) {"(#{input.to_json})"}
6
+
7
+ let(:image){Bresson::ImageReference.new input}
8
+
9
+ context 'when sets with json' do
10
+
11
+ subject {Towsta::Kinds::ImageKind.new json}
12
+
13
+ #its(:get) {should == image}
14
+
15
+ its(:export) {should == json}
16
+
17
+ end
18
+
19
+ context 'when sets with a bad string' do
20
+
21
+ subject {Towsta::Kinds::ImageKind.new 'Bad String'}
22
+
23
+ its(:get) {should be_nil}
24
+
25
+ its(:export) {should be_blank}
26
+
27
+ end
28
+
29
+ context 'when sets with a Image' do
30
+
31
+ subject {Towsta::Kinds::ImageKind.new image}
32
+
33
+ its(:get) {should == image}
34
+
35
+ its(:export) {should == json}
36
+
37
+ end
38
+
39
+ end
@@ -0,0 +1,33 @@
1
+ describe Towsta::Kinds::IntegerKind do
2
+
3
+ context 'when sets with numeric string' do
4
+
5
+ subject {Towsta::Kinds::IntegerKind.new '1'}
6
+
7
+ its(:get) {should == 1}
8
+
9
+ its(:export) {should == '1'}
10
+
11
+ end
12
+
13
+ context 'when sets with a bad string' do
14
+
15
+ subject {Towsta::Kinds::IntegerKind.new 'Bad String'}
16
+
17
+ its(:get) {should be_zero}
18
+
19
+ its(:export) {should == '0'}
20
+
21
+ end
22
+
23
+ context 'when sets with a integer' do
24
+
25
+ subject {Towsta::Kinds::IntegerKind.new 1}
26
+
27
+ its(:get) {should == 1}
28
+
29
+ its(:export) {should == '1'}
30
+
31
+ end
32
+
33
+ end
@@ -0,0 +1,37 @@
1
+ describe Towsta::Kinds::ListKind do
2
+
3
+ let(:input) {'some, test, string'}
4
+
5
+ let(:list){%w{some test string}}
6
+
7
+ context 'when sets with json' do
8
+
9
+ subject {Towsta::Kinds::ListKind.new input}
10
+
11
+ its(:get) {should == list}
12
+
13
+ its(:export) {should == input}
14
+
15
+ end
16
+
17
+ context 'when sets with a bad string' do
18
+
19
+ subject {Towsta::Kinds::ListKind.new 'Bad String'}
20
+
21
+ its(:get) {should == ['Bad String']}
22
+
23
+ its(:export) {should == 'Bad String'}
24
+
25
+ end
26
+
27
+ context 'when sets with a List' do
28
+
29
+ subject {Towsta::Kinds::ListKind.new list}
30
+
31
+ its(:get) {should == list}
32
+
33
+ its(:export) {should == input}
34
+
35
+ end
36
+
37
+ end
@@ -0,0 +1,24 @@
1
+ describe Towsta::Kinds::MainKind do
2
+
3
+ subject { Towsta::Kinds::MainKind.new "Test String"}
4
+
5
+ its(:get) { should == 'Test String'}
6
+
7
+ it 'unescapes on set' do
8
+ subject.set "\'"
9
+ subject.get.should == "'"
10
+ end
11
+
12
+ it 'compares two equal objects' do
13
+ subject.compare('Test String').should == true
14
+ end
15
+
16
+ it 'compares two different objects' do
17
+ subject.compare('Other String').should == false
18
+ end
19
+
20
+ its(:export) {should == 'Test String'}
21
+
22
+ its(:kind) {should == 'main'}
23
+
24
+ end
@@ -0,0 +1,37 @@
1
+ describe Towsta::Kinds::MoneyKind do
2
+
3
+ let(:input) {'1.000,00'}
4
+
5
+ let(:money){1_000.00}
6
+
7
+ context 'when sets with string' do
8
+
9
+ subject {Towsta::Kinds::MoneyKind.new input}
10
+
11
+ its(:get) {should == money}
12
+
13
+ its(:export) {should == input}
14
+
15
+ end
16
+
17
+ context 'when sets with a bad string' do
18
+
19
+ subject {Towsta::Kinds::MoneyKind.new 'Bad String'}
20
+
21
+ its(:get) {should be_zero}
22
+
23
+ its(:export) {should == '0'}
24
+
25
+ end
26
+
27
+ context 'when sets with a Float' do
28
+
29
+ subject {Towsta::Kinds::MoneyKind.new input}
30
+
31
+ its(:get) {should == money}
32
+
33
+ its(:export) {should == input}
34
+
35
+ end
36
+
37
+ end
@@ -0,0 +1,41 @@
1
+ describe Towsta::Kinds::MultipleKind do
2
+
3
+ let(:input) {'1 2'}
4
+
5
+ before(:all){
6
+ Towsta::Vertical.clear
7
+ Towsta::Vertical.create name: 'Dog', slices: {id: 'integer'}
8
+ Towsta::Vertical.populate 'Dog', [{id: 1}, {id: 2}]
9
+ }
10
+
11
+ context 'when sets with string' do
12
+
13
+ subject {Towsta::Kinds::MultipleKind.new input}
14
+
15
+ its(:get) {should == Dog.all}
16
+
17
+ its(:export) {should == input}
18
+
19
+ end
20
+
21
+ context 'when sets with a bad string' do
22
+
23
+ subject {Towsta::Kinds::MultipleKind.new 'Bad String'}
24
+
25
+ its(:get) {should == []}
26
+
27
+ its(:export) {should == ''}
28
+
29
+ end
30
+
31
+ context 'when sets with a Multiple' do
32
+
33
+ subject {Towsta::Kinds::MultipleKind.new Dog.all}
34
+
35
+ its(:get) {should == Dog.all}
36
+
37
+ its(:export) {should == input}
38
+
39
+ end
40
+
41
+ end
@@ -0,0 +1,41 @@
1
+ describe Towsta::Kinds::UserKind do
2
+
3
+ let(:input) {'1'}
4
+
5
+ before(:all){
6
+ Towsta::Vertical.clear
7
+ Towsta::Vertical.create name: 'User', slices: {id: 'integer'}
8
+ Towsta::Vertical.populate 'User', [{id: 1}]
9
+ }
10
+
11
+ context 'when sets with string' do
12
+
13
+ subject {Towsta::Kinds::UserKind.new input}
14
+
15
+ its(:get) {should == User.first}
16
+
17
+ its(:export) {should == input}
18
+
19
+ end
20
+
21
+ context 'when sets with a bad string' do
22
+
23
+ subject {Towsta::Kinds::UserKind.new 'Bad String'}
24
+
25
+ its(:get) {should be_nil}
26
+
27
+ its(:export) {should be_blank}
28
+
29
+ end
30
+
31
+ context 'when sets with a User' do
32
+
33
+ subject {Towsta::Kinds::UserKind.new User.first}
34
+
35
+ its(:get) {should == User.first}
36
+
37
+ its(:export) {should == input}
38
+
39
+ end
40
+
41
+ end
@@ -0,0 +1,41 @@
1
+ describe Towsta::Kinds::VerticalKind do
2
+
3
+ let(:input) {'1'}
4
+
5
+ before(:all){
6
+ Towsta::Vertical.clear
7
+ Towsta::Vertical.create name: 'Cat', slices: {id: 'integer'}
8
+ Towsta::Vertical.populate 'Cat', [{id: 1}]
9
+ }
10
+
11
+ context 'when sets with string' do
12
+
13
+ subject {Towsta::Kinds::VerticalKind.new input}
14
+
15
+ its(:get) {should == Cat.first}
16
+
17
+ its(:export) {should == input}
18
+
19
+ end
20
+
21
+ context 'when sets with a bad string' do
22
+
23
+ subject {Towsta::Kinds::VerticalKind.new 'Bad String'}
24
+
25
+ its(:get) {should be_nil}
26
+
27
+ its(:export) {should be_blank}
28
+
29
+ end
30
+
31
+ context 'when sets with a Cat' do
32
+
33
+ subject {Towsta::Kinds::VerticalKind.new Cat.first}
34
+
35
+ its(:get) {should == Cat.first}
36
+
37
+ its(:export) {should == input}
38
+
39
+ end
40
+
41
+ end