towsta 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,5 @@
1
+ pkg/*
2
+ *.gem
3
+ .bundle
4
+ *.swp
5
+ *.swo
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in towsta.gemspec
4
+ gemspec
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
@@ -0,0 +1,79 @@
1
+ require 'net/http'
2
+ require 'json'
3
+
4
+ module Towsta
5
+ class Synchronizer
6
+
7
+ attr_accessor :secret, :path, :json
8
+
9
+ def initialize args
10
+ @secret = args[:secret]
11
+ @path = "#{args[:path]}.json"
12
+ synchronize ? backup : find_old
13
+ puts just_do_it ? 'Ready to Towst!' : 'Unable to keep Towsting!'
14
+ end
15
+
16
+ def synchronize
17
+ unless @secret
18
+ puts 'you cant synchronize without a secret...'
19
+ return false
20
+ end
21
+ begin
22
+ Net::HTTP.start("manager.towsta.com"){|http| @json = http.get("/synchronizers/#{@secret}/export.json").body}
23
+ puts 'Synchronized with towsta...'
24
+ if @json == " "
25
+ puts 'Maybe your secret is wrong...'
26
+ return false
27
+ else
28
+ return true
29
+ end
30
+ rescue
31
+ puts 'failed to synchronize with towsta...'
32
+ return false
33
+ end
34
+ end
35
+
36
+ def backup
37
+ if @path
38
+ open(@path, "wb"){|file| file.write @json}
39
+ puts "creating a backup in #{@path}"
40
+ end
41
+ end
42
+
43
+ def find_old
44
+ unless File.exists? @path
45
+ puts 'could not find any old version...'
46
+ else
47
+ @json = IO.read(@path).to_s
48
+ puts 'assuming newest backup...'
49
+ end
50
+ end
51
+
52
+ def self.update_or_create arg
53
+ args = JSON.parse arg
54
+ eval(args[:vertical]).update_or_create args[:attributes]
55
+ end
56
+
57
+ def just_do_it
58
+ return false if @hash == " "
59
+ begin
60
+ hash = JSON.parse @json, :symbolize_names => true
61
+ rescue
62
+ puts 'Something went wrong tryng to parse JSON.'
63
+ return false
64
+ end
65
+ Vertical.create :name => 'User', :slices => {:id => 'integer', :nick => 'string', :email => 'string'}
66
+ hash[:users].each {|user| User.new user}
67
+ hash[:structures].each_with_index do |structure, i|
68
+ Vertical.create structure
69
+ Vertical.all << eval(structure[:name])
70
+ hash[:verticals][i][:horizontals].each {|horizontal| Vertical.all.last.new horizontal}
71
+ puts "vertical #{structure[:name]} was created with #{hash[:verticals][i][:horizontals].size} horizontals"
72
+ end
73
+ true
74
+ end
75
+
76
+ end
77
+
78
+ end
79
+
@@ -0,0 +1,31 @@
1
+ module Towsta
2
+
3
+ class Tree
4
+
5
+ attr_accessor :item, :left, :right
6
+
7
+ def initialize element
8
+ @item = element
9
+ end
10
+
11
+ def add element
12
+ item.id > element.id ? ( @right ? @right.add(element) : add_right(Tree.new(element))) : (@left ? @left.add(element): add_left(Tree.new(element)))
13
+ end
14
+
15
+ def add_left tree
16
+ @left = tree
17
+ end
18
+
19
+ def add_right tree
20
+ @right = tree
21
+ end
22
+
23
+ def find id
24
+ return self.item if @item.id == id
25
+ return @right.find id if @right && @item.id > id
26
+ return @left.find id if @left
27
+ end
28
+
29
+ end
30
+
31
+ end
@@ -0,0 +1,3 @@
1
+ module Towsta
2
+ VERSION = "0.1.2"
3
+ end
@@ -0,0 +1,89 @@
1
+ require 'date'
2
+ require 'time'
3
+
4
+ module Towsta
5
+
6
+ class Vertical
7
+
8
+ class << self
9
+ attr_accessor :all
10
+ Vertical.all = []
11
+ end
12
+
13
+ def self.create(args)
14
+ klass = Class.new do
15
+
16
+ args[:slices].each do |attr, kind|
17
+ eval "def #{attr}= value; #{Vertical.parse_set attr, kind}; end;"
18
+ eval "def #{attr}; #{Vertical.parse_get attr, kind}; end;"
19
+ eval "def self.find_by_#{attr} value; self.all.each {|e| return e if e.#{attr} == value}; nil; end;"
20
+ eval "def self.find_all_by_#{attr} value; found = []; self.all.each {|e| found << e if e.#{attr} == value}; found; end;"
21
+ end
22
+
23
+ class << self
24
+ attr_accessor :all, :tree
25
+ end
26
+
27
+ def self.count
28
+ self.all.size
29
+ end
30
+
31
+ def self.first
32
+ self.all.first
33
+ end
34
+
35
+ def self.last
36
+ self.all.last
37
+ end
38
+
39
+ def self.find id
40
+ self.tree ? self.tree.find(id) : nil
41
+ end
42
+
43
+ def self.update_or_create args
44
+ element = self.find args[:id]
45
+ element ? element.update(args) : self.new(args)
46
+ end
47
+
48
+ def initialize args
49
+ args.each {|k,v| eval "self.#{k}= '#{v}';"}
50
+ self.class.tree ? self.class.tree.add(self) : self.class.tree=(Tree.new(self))
51
+ self.class.all << self
52
+ end
53
+
54
+ def update args
55
+ args.each {|k,v| eval "self.#{k}= '#{v}';"}
56
+ end
57
+
58
+ def find_horizontal id
59
+ Vertical.all.each do |v|
60
+ horizontal = v.find_by_id id.to_i
61
+ return horizontal if horizontal
62
+ end
63
+ nil
64
+ end
65
+
66
+ end
67
+ klass.all = []
68
+ Object.const_set args[:name], klass
69
+ end
70
+
71
+ private
72
+ def self.parse_get attr, kind
73
+ return "@#{attr}.class == String ? self.find_horizontal(@#{attr}) : @#{attr}" if kind == 'vertical'
74
+ "@#{attr}"
75
+ end
76
+
77
+ def self.parse_set attr, kind
78
+ return "@#{attr} = value;" if ['main','text','formated','password','link'].include? kind
79
+ return "@#{attr} = value.to_f;" if kind == 'money'
80
+ return "@#{attr} = value.to_i;" if kind == 'integer'
81
+ return "@#{attr} = value == '1';" if kind == 'boolean'
82
+ return "@#{attr} = Date.strptime(value, '%m/%d/%Y');" if kind == 'date'
83
+ return "@#{attr} = DateTime.strptime(value, '%m/%d/%Y %H:%M').to_time;" if kind == 'datetime'
84
+ return "@#{attr} = User.find value.to_i;" if kind == 'user'
85
+ "@#{attr} = value;"
86
+ end
87
+ end
88
+
89
+ end
data/lib/towsta.rb ADDED
@@ -0,0 +1,6 @@
1
+ require File.expand_path('../towsta/tree', __FILE__)
2
+ require File.expand_path('../towsta/vertical', __FILE__)
3
+ require File.expand_path('../towsta/synchronizer', __FILE__)
4
+
5
+ module Towsta
6
+ end
data/test.json ADDED
@@ -0,0 +1 @@
1
+ {"name":"test","structures":[{"name":"Book","slices":{"name":"main","author":"user","review":"formated","id":"integer"}},{"name":"Asdas","slices":{"author":"user","id":"integer","asdads":"main"}},{"name":"Movie","slices":{"name":"main","author":"user","id":"integer","book":"boolean"}},{"name":"Movies","slices":{"name":"main","author":"user","review":"formated","id":"integer","data_de_lancamento":"date","book":"vertical"}}],"verticals":[{"horizontals":[{"name":"Kama Sutra","author":6,"review":"Sex positions book","id":6},{"name":"Shit my dad says","author":6,"review":"lot of shits my dad said","id":7}]},{"horizontals":[]},{"horizontals":[]},{"horizontals":[{"name":"Porn dance","author":6,"review":"aoshdoashd","id":12,"data_de_lancamento":"03/24/2011","book":"6"}]}],"revision":26,"users":[{"nick":"Admin","id":6,"email":"admin@test.com"}]}
data/towsta.gemspec ADDED
@@ -0,0 +1,23 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "towsta/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "towsta"
7
+ s.version = Towsta::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Mortaro"]
10
+ s.email = ["mortaro@towsta.com"]
11
+ s.homepage = "http://rubygems.org/gems/towsta"
12
+ s.summary = %q{Api Integration gem with towsta}
13
+ s.description = %q{Simply integrates the towsta api}
14
+
15
+ s.rubyforge_project = "towsta"
16
+
17
+ s.add_dependency("json")
18
+
19
+ s.files = `git ls-files`.split("\n")
20
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
21
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
22
+ s.require_paths = ["lib"]
23
+ end
metadata ADDED
@@ -0,0 +1,86 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: towsta
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ - 2
9
+ version: 0.1.2
10
+ platform: ruby
11
+ authors:
12
+ - Mortaro
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2011-03-02 00:00:00 -03:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: json
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ segments:
29
+ - 0
30
+ version: "0"
31
+ type: :runtime
32
+ version_requirements: *id001
33
+ description: Simply integrates the towsta api
34
+ email:
35
+ - mortaro@towsta.com
36
+ executables: []
37
+
38
+ extensions: []
39
+
40
+ extra_rdoc_files: []
41
+
42
+ files:
43
+ - .gitignore
44
+ - Gemfile
45
+ - Rakefile
46
+ - lib/towsta.rb
47
+ - lib/towsta/synchronizer.rb
48
+ - lib/towsta/tree.rb
49
+ - lib/towsta/version.rb
50
+ - lib/towsta/vertical.rb
51
+ - test.json
52
+ - towsta.gemspec
53
+ has_rdoc: true
54
+ homepage: http://rubygems.org/gems/towsta
55
+ licenses: []
56
+
57
+ post_install_message:
58
+ rdoc_options: []
59
+
60
+ require_paths:
61
+ - lib
62
+ required_ruby_version: !ruby/object:Gem::Requirement
63
+ none: false
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ segments:
68
+ - 0
69
+ version: "0"
70
+ required_rubygems_version: !ruby/object:Gem::Requirement
71
+ none: false
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ segments:
76
+ - 0
77
+ version: "0"
78
+ requirements: []
79
+
80
+ rubyforge_project: towsta
81
+ rubygems_version: 1.3.7
82
+ signing_key:
83
+ specification_version: 3
84
+ summary: Api Integration gem with towsta
85
+ test_files: []
86
+