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
data/Rakefile CHANGED
@@ -1,2 +1,5 @@
1
1
  require 'bundler'
2
2
  Bundler::GemHelper.install_tasks
3
+
4
+ require 'rspec/core/rake_task'
5
+ RSpec::Core::RakeTask.new('spec')
@@ -0,0 +1,7 @@
1
+ require "./towsta.rb"
2
+
3
+ def sync_with_towsta params={}
4
+ sync = Towsta::Synchronizer.new params: params, request: :all
5
+ (Dir["./controllers/*.rb"] + Dir["./models/*.rb"]).each {|file| load file}
6
+ sync.status
7
+ end
@@ -0,0 +1,13 @@
1
+ require "./towsta.rb"
2
+
3
+ require 'dalli'
4
+ set :cache, Dalli::Client.new
5
+ require File.expand_path('../../towsta/memory', __FILE__)
6
+
7
+ Towsta::Synchronizer.new params: {}, request: :structure
8
+
9
+ (Dir["./controllers/*.rb"] + Dir["./models/*.rb"]).each {|file| require file}
10
+
11
+ def sync_with_towsta params={}
12
+ Towsta::Memory.recover(params).status
13
+ end
@@ -0,0 +1,5 @@
1
+ def sync_with_towsta params={}
2
+ sync = Towsta::Synchronizer.new params: params, request: :all
3
+ (Dir["./controllers/*.rb"] + Dir["./models/*.rb"]).each {|file| load file}
4
+ sync.status
5
+ end
@@ -4,7 +4,8 @@ module Towsta
4
4
  class BooleanKind < MainKind
5
5
 
6
6
  def set content
7
- @content = content == '1'
7
+ return @content = content if [TrueClass, FalseClass].include? content.class
8
+ @content = content.to_i == 1
8
9
  end
9
10
 
10
11
  def export
@@ -3,10 +3,6 @@ module Towsta
3
3
 
4
4
  class FormatedKind < MainKind
5
5
 
6
- def set content
7
- @content = content.to_s
8
- end
9
-
10
6
  end
11
7
 
12
8
  end
@@ -15,9 +15,8 @@ module Towsta
15
15
  end
16
16
 
17
17
  def export
18
- string = '['
19
- @content.each {|con| string << "#{con.to_s},"}
20
- "#{string[0..-2]}]"
18
+ return '' if @content.class == Array && @content.empty?
19
+ "[#{@content.join ','}]"
21
20
  end
22
21
 
23
22
  end
@@ -12,6 +12,12 @@ module Towsta
12
12
  @content == object.to_f
13
13
  end
14
14
 
15
+ def export
16
+ return '0' if @content == 0
17
+ content = ("%.2f" % @content).split('.')
18
+ "#{content.first.reverse.scan(/.{1,3}/).join('.').reverse},#{content.last}"
19
+ end
20
+
15
21
  end
16
22
 
17
23
  end
@@ -5,21 +5,17 @@ module Towsta
5
5
 
6
6
  attr_accessor :content
7
7
 
8
- def initialize content=nil
9
- self.set content
10
- end
11
-
12
8
  def get
13
9
  return @content if @content.class == Array
10
+ return [] if !!(@content =~ /[^0-9 ]/)
14
11
  aux = []
15
12
  @content.to_s.split(' ').each do |i|
16
13
  Vertical.all.each do |v|
17
- horizontal = v.find_by_id i
14
+ horizontal = v.find_by_id i.to_i
18
15
  aux << horizontal if horizontal
19
16
  end
20
17
  end
21
18
  @content = aux
22
- @content
23
19
  end
24
20
 
25
21
  def set content
@@ -28,10 +24,7 @@ module Towsta
28
24
  end
29
25
 
30
26
  def export
31
- return @content.to_s if @content.class != Array
32
- aux = ''
33
- @content.each {|c| aux << c.id}
34
- aux
27
+ get.collect{|c| c.id}.join ' '
35
28
  end
36
29
 
37
30
  end
@@ -4,17 +4,15 @@ module Towsta
4
4
  class UserKind < MainKind
5
5
 
6
6
  def get
7
+ return nil unless @content
7
8
  return @content if @content.class == User
8
- user = User.find @content.to_i
9
- @content = user if user
10
- @content
9
+ @content = User.find_by_id @content
11
10
  end
12
11
 
13
12
  def set content
14
13
  return @content = content if content.class == User
15
- return @content = content.to_i if content.to_i != 0
16
- @content = nil
17
- @content = content
14
+ return @content = nil if !!(content =~ /[^0-9]/)
15
+ return @content = content.to_i
18
16
  end
19
17
 
20
18
  def compare object
@@ -23,8 +21,8 @@ module Towsta
23
21
  end
24
22
 
25
23
  def export
26
- return @content.id.to_s if self.get.class == User
27
- @content.to_s
24
+ return get.id.to_s if get.class == User
25
+ get.to_s
28
26
  end
29
27
 
30
28
  end
@@ -4,6 +4,7 @@ module Towsta
4
4
  class VerticalKind < MainKind
5
5
 
6
6
  def get
7
+ return nil unless @content
7
8
  return @content if @content.class != Fixnum
8
9
  Vertical.all.each do |v|
9
10
  horizontal = v.find_by_id @content
@@ -16,26 +17,21 @@ module Towsta
16
17
  end
17
18
 
18
19
  def set content
19
- return @content = content if klasses.include? content.class
20
+ return @content = nil if !!(content =~ /[^0-9]/)
21
+ return @content = content if Vertical.all.include? content.class
20
22
  @content = content.to_i
21
23
  end
22
24
 
23
25
  def compare object
24
- self.get.id.to_i == object.id.to_i if klasses.include? object.class
26
+ self.get.id.to_i == object.id.to_i if Vertical.all.include? object.class
25
27
  self.get == object
26
28
  end
27
29
 
28
30
  def export
29
- return @content.id.to_s if klasses.include? self.get.class
31
+ return @content.id.to_s if Vertical.all.include? self.get.class
30
32
  @content.to_s
31
33
  end
32
34
 
33
- private
34
-
35
- def klasses
36
- Vertical.all + [User]
37
- end
38
-
39
35
  end
40
36
 
41
37
  end
data/lib/towsta/memory.rb CHANGED
@@ -1,5 +1,3 @@
1
- set :cache, Dalli::Client.new
2
-
3
1
  module Towsta
4
2
 
5
3
  class Memory
@@ -8,10 +6,10 @@ module Towsta
8
6
  md5 = Memory.md5(params)
9
7
  if settings.cache.get(md5)
10
8
  puts "\nUsing cache to Towst"
11
- Towsta::Synchronizer.new :path => $towsta_path, :cache => settings.cache.get(md5), :secret => $towsta_secret
9
+ Towsta::Synchronizer.new cache: settings.cache.get(md5), request: :horizontals
12
10
  else
13
11
  puts "\nCreating cache"
14
- syn = Towsta::Synchronizer.new :secret => $towsta_secret, :path => $towsta_path, :params => params
12
+ syn = Towsta::Synchronizer.new params: params, request: :horizontals
15
13
  settings.cache.set(md5,syn.response)
16
14
  end
17
15
  end
@@ -39,7 +39,7 @@ helpers do
39
39
  end
40
40
 
41
41
  post '/flush' do
42
- clear_sync
42
+ Towsta::Memory.flush
43
43
  'CLEAR'
44
44
  end
45
45
 
@@ -1,59 +1,65 @@
1
1
  module Towsta
2
2
  class Synchronizer
3
3
 
4
- attr_accessor :secret, :params, :cache, :response
4
+ attr_accessor :params, :cache, :response, :status
5
5
 
6
- def initialize args
7
- Vertical.all = []
8
- @secret = args[:secret]
9
- @params = args[:params]
6
+ def initialize args={}
7
+ @params = solve_params args[:params]
10
8
  @cache = args[:cache]
11
9
  if synchronize
12
- create_verticals
10
+ args[:request] ||= :horizontals
11
+ create_verticals if [:all, :structure].include? args[:request]
12
+ populate_verticals if [:all, :horizontals].include? args[:request]
13
13
  puts " Ready to Towst!\n\n"
14
+ @status = true
14
15
  else
15
16
  puts " Unable to keep Towsting!\n\n"
17
+ @status = false
16
18
  end
17
19
  end
18
20
 
19
21
  def self.save_request export
20
22
  begin
21
- uri = URI.parse("http://manager.towsta.com/synchronizers/#{$towsta_secret}/import.json")
22
- return JSON.parse Net::HTTP.post_form(uri, {:code => export.to_json}).body.to_s, :symbolize_names => true
23
+ uri = URI.parse("http://manager.towsta.com/synchronizers/#{Towsta.secret}/import.json")
24
+ return JSON.parse Net::HTTP.post_form(uri, {code: export.to_json}).body.to_s, symbolize_names: true
23
25
  rescue
24
- return {:status => false, :message => 'Internal Server Error'}
26
+ return {status: false, message: 'Internal Server Error'}
25
27
  end
26
28
  end
27
29
 
28
30
  def self.authentication_request params
29
31
  begin
30
32
  uri = URI.parse("http://manager.towsta.com/authenticate")
31
- return JSON.parse Net::HTTP.post_form(uri, params).body.to_s, :symbolize_names => true
33
+ return JSON.parse Net::HTTP.post_form(uri, params).body.to_s, symbolize_names: true
32
34
  rescue
33
- return {:status => false}
35
+ return {status: false}
34
36
  end
35
37
  end
36
38
 
37
39
  private
38
40
 
39
41
  def synchronize
40
- if has_secret && (cache_string || remote_string)
41
- return false unless validate_secret
42
- return false unless validate_response
43
- return true
44
- end
45
- false
42
+ has_secret && (cache_string || remote_string) && validate_secret && validate_response && parse_json
43
+ end
44
+
45
+ def solve_params(params)
46
+ return params unless Towsta.global
47
+ Towsta.global.merge (params || {})
46
48
  end
47
49
 
48
50
  def create_verticals
49
- return false unless parse_json
50
- create_vertical({:name => 'User', :slices => {:id => 'integer', :nick => 'text', :email => 'text'}}, @hash[:users])
51
- @hash[:structures].each_with_index {|structure, i| create_vertical(structure, @hash[:verticals][i][:horizontals], @hash[:verticals][i][:occurrences])}
51
+ Vertical.create name: 'User', slices: {id: 'integer', nick: 'text', email: 'text'}
52
+ @hash[:structures].each {|structure| Vertical.create structure}
53
+ end
54
+
55
+ def populate_verticals
56
+ Vertical.populate 'User', @hash[:users]
57
+ @hash[:structures].each_with_index {|structure, i| Vertical.populate(structure[:name], @hash[:verticals][i][:horizontals], @hash[:verticals][i][:occurrences])}
52
58
  end
53
59
 
54
60
  def remote_string
55
61
  begin
56
- uri = "/synchronizers/#{@secret}/#{Time.now.to_i}/export.json"
62
+ uri = "/synchronizers/#{Towsta.secret}/#{Time.now.to_i}/export.json"
57
63
  uri += "?query=#{CGI::escape(@params.to_json)}" if @params
58
64
  @response = Net::HTTP.start("manager.towsta.com"){|http| @json = http.get(uri).body}
59
65
  puts "\nSynchronized with Towsta!"
@@ -82,14 +88,14 @@ module Towsta
82
88
  end
83
89
 
84
90
  def has_secret
85
- return true if @secret
91
+ return true if Towsta.secret
86
92
  puts "\nyou cant synchronize without a secret..."
87
93
  false
88
94
  end
89
95
 
90
96
  def parse_json
91
97
  begin
92
- @hash = JSON.parse @response, :symbolize_names => true
98
+ @hash = JSON.parse @response, symbolize_names: true
93
99
  return true
94
100
  rescue
95
101
  puts ' Something went wrong tryng to parse JSON.'
@@ -97,15 +103,6 @@ module Towsta
97
103
  end
98
104
  end
99
105
 
100
- def create_vertical structure, horizontals, occurrences=[]
101
- Object.instance_eval{ remove_const structure[:name].to_sym } if Object.const_defined?(structure[:name].to_sym)
102
- Vertical.create structure
103
- Vertical.all << eval(structure[:name])
104
- horizontals.each {|horizontal| eval(structure[:name].to_s).new(horizontal)}
105
- occurrences.each {|occurrence| eval(structure[:name].to_s).add_occurrence(occurrence)}
106
- puts " class #{structure[:name]} was created with #{horizontals.size} instances"
107
- end
108
-
109
106
  end
110
107
 
111
108
  end
@@ -1,3 +1,3 @@
1
1
  module Towsta
2
- VERSION = "1.3.0"
2
+ VERSION = "2.0.0"
3
3
  end
@@ -0,0 +1,52 @@
1
+ module Towsta
2
+
3
+ class VerticalCore
4
+
5
+ class << self
6
+ attr_accessor :attributes
7
+ end
8
+
9
+ self.attributes ||= []
10
+
11
+ def attributes
12
+ horizontal = {}
13
+ self.class.attributes.each do |attr|
14
+ slice = send :"object_of_#{attr.to_s}"
15
+ horizontal[attr] = slice ? slice.export : nil
16
+ end
17
+ horizontal
18
+ end
19
+
20
+ def meta_attributes
21
+ a = attributes.clone
22
+ a.delete :author
23
+ a.delete :created_at
24
+ a.delete :updated_at
25
+ a.delete :id
26
+ a
27
+ end
28
+
29
+ def self.define_attribute attr, kind
30
+ self.class_eval("
31
+
32
+ def #{attr}= value
33
+ @#{attr} ||= Towsta::Kinds::#{kind[0].upcase + kind[1..-1]}Kind.new
34
+ @#{attr}.set value
35
+ end
36
+
37
+ def #{attr}
38
+ @#{attr}.get
39
+ end
40
+
41
+ def object_of_#{attr}
42
+ @#{attr}
43
+ end
44
+
45
+ ")
46
+ self.attributes ||= []
47
+ self.attributes << attr.to_sym
48
+ end
49
+
50
+ end
51
+
52
+ end
@@ -0,0 +1,24 @@
1
+ module Towsta
2
+
3
+ class VerticalCore
4
+
5
+ def initialize args={}
6
+ self.attributes.merge(args).each {|k,v| self.send("#{k}=".to_sym, v)}
7
+ self.class.all << self
8
+ end
9
+
10
+ def self.to_hash
11
+ hashes = []
12
+ self.all.each {|hash| hashes << hash.attributes}
13
+ hashes
14
+ end
15
+
16
+ def self.add_occurrence occurrence
17
+ self.send(:define_singleton_method, "occurrences_of_#{occurrence[:name].downcase}") do
18
+ eval occurrence[:items].inspect
19
+ end
20
+ end
21
+
22
+ end
23
+
24
+ end
@@ -0,0 +1,41 @@
1
+ module Towsta
2
+
3
+ class VerticalCore
4
+
5
+ attr_accessor :message
6
+
7
+ def update args, author=$towsta_default_author
8
+ args.each {|k,v| self.send("#{k}=".to_sym, v)}
9
+ self.save author
10
+ end
11
+
12
+ def destroy
13
+ #to-do
14
+ end
15
+
16
+ def save creator=$towsta_default_author
17
+ creator = author.email if author
18
+ export = self.attributes
19
+ export.delete :author
20
+ export.delete :created_at
21
+ export.delete :updated_at
22
+ id_aux = export.delete(:id)
23
+ id_aux ? id_aux : '0'
24
+ export = {:creator => creator, :vertical => self.class.to_s, :attributes => export, :id => id_aux}
25
+ response = Towsta::Synchronizer.save_request export
26
+ @message = response[:message]
27
+ self.id = response[:id] if response[:status]
28
+ self.author = User.find_by_email creator
29
+ Towsta::Memory.flush if $towsta_cache
30
+ response[:status]
31
+ end
32
+
33
+ def self.create args, creator=$towsta_default_author
34
+ new = self.new(args.merge(:id => nil))
35
+ new.save creator
36
+ new
37
+ end
38
+
39
+ end
40
+
41
+ end
@@ -0,0 +1,21 @@
1
+ module Towsta
2
+
3
+ class VerticalCore
4
+
5
+ include R18n::Helpers
6
+
7
+ def i18n attr
8
+ self.send :"#{attr.to_s}_#{R18n.get.locales.first.code}"
9
+ end
10
+
11
+ def method_missing meth, *args, &block
12
+ begin
13
+ return self.i18n meth.to_sym
14
+ rescue
15
+ super
16
+ end
17
+ end
18
+
19
+ end
20
+
21
+ end
@@ -0,0 +1,17 @@
1
+ module Towsta
2
+
3
+ class VerticalCore
4
+
5
+ def to_mail
6
+ string = ''
7
+ meta_attributes.each { |attribute, value| string << "<b>#{attribute.to_s}:</b> #{value} <br/>" }
8
+ string
9
+ end
10
+
11
+ def notify args
12
+ Pony.mail({:html_body => to_mail}.merge(args))
13
+ end
14
+
15
+ end
16
+
17
+ end
@@ -0,0 +1,44 @@
1
+ module Towsta
2
+
3
+ class VerticalCore
4
+
5
+ class << self
6
+ attr_accessor :all, :count
7
+ end
8
+
9
+ self.all ||= []
10
+
11
+ def self.first
12
+ self.all.first
13
+ end
14
+
15
+ def self.last
16
+ self.all.last
17
+ end
18
+
19
+ def self.find id
20
+ self.find_by_id id
21
+ end
22
+
23
+ def self.random
24
+ position = (self.all.size) - 1
25
+ self.all[rand(position)]
26
+ end
27
+
28
+ def self.find_by attr, value
29
+ self.all.each { |horizontal| return horizontal if horizontal.send(:"object_of_#{attr}").compare value }
30
+ end
31
+
32
+ def self.find_all_by attr, value
33
+ self.all.select { |horizontal| horizontal.send(:"object_of_#{attr}").compare value }
34
+ end
35
+
36
+ def self.method_missing m, *args, &block
37
+ return self.find_by Regexp.last_match(1), args.first if m =~ /find_by_(.+)/
38
+ return self.find_all_by Regexp.last_match(1), args.first if m =~ /find_all_by_(.+)/
39
+ super
40
+ end
41
+
42
+ end
43
+
44
+ end