cartomodel 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: b5c3063fdba6f802ecad416c4028764171507277
4
+ data.tar.gz: 385944e4e94f45ffbb272a0898ca5af535ae520b
5
+ SHA512:
6
+ metadata.gz: 74e38bac4b0d5f436958b391b288478ba730bfbaf205fbfbe8664d47b4d093c469c6f996c16c5454dab799ac549b0ce56a55c263a62e4b831be97d17f022042f
7
+ data.tar.gz: 09262f7ccb56f3785daae85436f72a7ba6242affc83448e2699871d9b17237a5f3bfb413a771222fae306e3f0b22288912d3fac950d57cc957e0aabbdd09b0cc
data/README.md ADDED
@@ -0,0 +1,39 @@
1
+ # Cartomodel
2
+
3
+ [![Build Status](https://travis-ci.org/Vizzuality/cartomodel.svg?branch=master)](https://travis-ci.org/Vizzuality/cartomodel)
4
+
5
+ ActiveRecord integration for Cartowrap gem
6
+
7
+ Currently happy case based, not very configurable
8
+
9
+ PRs are welcome ;)
10
+
11
+ # Usage
12
+
13
+ This gem integrates [Cartowrap](https://github.com/tiagojsag/cartowrap) with ActiveRecord.
14
+ Before using, follow the configuration instructions of Cartowrap.
15
+
16
+ You must manually configure your CartoDB account, create the table and ensure that the structure matches
17
+
18
+ Once that is done, configure your local model like so:
19
+
20
+ ```
21
+ class YourModelClass < ApplicationRecord
22
+ include Cartomodel::Model::Synchronizable
23
+
24
+ def cartodb_table
25
+ 'indicator_config'
26
+ end
27
+ end
28
+ ```
29
+
30
+ Make sure that your model has the two following columns:
31
+
32
+ ```
33
+ class AddCartomodelColumnsYourModel < ActiveRecord::Migration
34
+ def change
35
+ add_column :indicator_configs, :cartodb_id, :integer
36
+ add_column :indicator_configs, :sync_state, :integer
37
+ end
38
+ end
39
+ ```
data/Rakefile ADDED
@@ -0,0 +1,11 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ task :default => [:spec]
8
+ desc 'run Rspec specs'
9
+ task :spec do
10
+ sh 'rspec'
11
+ end
@@ -0,0 +1,5 @@
1
+ module Cartomodel
2
+
3
+ class CartomodelError < StandardError; end
4
+
5
+ end
@@ -0,0 +1,101 @@
1
+ module Cartomodel
2
+ module Model
3
+ module Synchronizable
4
+ extend ActiveSupport::Concern
5
+
6
+ STATE_UNSYNCED = 0
7
+ STATE_SYNCED = 1
8
+ STATE_FAILED = 2
9
+
10
+ def initialize(*args, &block)
11
+ @api_endpoint = Cartowrap::API.new()
12
+ super
13
+ end
14
+
15
+ def create_on_cartodb
16
+ prepare_dependencies
17
+
18
+ self.sync_state == STATE_UNSYNCED
19
+
20
+ attributes = prepare_attributes
21
+
22
+ query = @query_generator.insert(attributes)
23
+ begin
24
+ response = @api_endpoint.send_query(query)
25
+ json = ActiveSupport::JSON.decode(response)
26
+ self.cartodb_id = json['rows'].first['cartodb_id']
27
+ self.sync_state = STATE_SYNCED
28
+ rescue
29
+ self.sync_state = STATE_FAILED
30
+ end
31
+ end
32
+
33
+ def update_on_cartodb
34
+ prepare_dependencies
35
+
36
+ self.sync_state == STATE_UNSYNCED
37
+
38
+ attributes = prepare_attributes
39
+
40
+ query = @query_generator.update(attributes, :cartodb_id, self.cartodb_id)
41
+ begin
42
+ response = @api_endpoint.send_query(query)
43
+ json = ActiveSupport::JSON.decode(response)
44
+ self.cartodb_id = json['rows'].first['cartodb_id']
45
+ self.sync_state = STATE_SYNCED
46
+ rescue
47
+ self.sync_state = STATE_FAILED
48
+ end
49
+ end
50
+
51
+ def delete_on_cartodb
52
+ prepare_dependencies
53
+
54
+ query = @query_generator.delete(:cartodb_id, self.cartodb_id)
55
+ @api_endpoint.send_query(query)
56
+ end
57
+
58
+ def self.included(base)
59
+ unless base.ancestors.include? ActiveRecord::Base
60
+ raise RuntimeError.new('Cartomodel::Synchronizable can only extend ActiveRecord instances')
61
+ end
62
+
63
+ unless base.new.attributes.include? 'cartodb_id' and base.new.attributes.include? 'sync_state'
64
+ raise RuntimeError.new('Cartomodel::Synchronizable requires \'cartodb_id\' and \'sync_state\' attributes')
65
+ end
66
+
67
+ base.before_create :create_on_cartodb
68
+ base.before_update :update_on_cartodb
69
+ base.before_destroy :delete_on_cartodb
70
+ end
71
+
72
+ private
73
+
74
+ def prepare_dependencies
75
+ unless self.respond_to? :cartodb_table
76
+ raise RuntimeError.new('CartoDB table name not defined')
77
+ end
78
+
79
+ if cartodb_table.nil?
80
+ raise RuntimeError.new('CartoDB table name not defined')
81
+ end
82
+
83
+ if @query_generator.nil?
84
+ @query_generator = Cartomodel::QueryGenerator.new(cartodb_table)
85
+ end
86
+
87
+ if @api_endpoint.nil?
88
+ @api_endpoint = Cartowrap::API.new()
89
+ end
90
+ end
91
+
92
+ def prepare_attributes
93
+ attributes = self.attributes.dup
94
+ attributes.delete('id')
95
+ attributes.delete('sync_state')
96
+ attributes.delete('cartodb_id')
97
+ attributes
98
+ end
99
+ end
100
+ end
101
+ end
@@ -0,0 +1,45 @@
1
+ module Cartomodel
2
+ class QueryGenerator
3
+ def initialize(table_name)
4
+ @table_name = table_name
5
+ @table = Arel::Table.new(@table_name)
6
+ end
7
+
8
+ def insert(raw_values)
9
+ manager = Arel::InsertManager.new()
10
+ manager.into @table
11
+ values = []
12
+
13
+ raw_values.each do |key, value|
14
+ values << [@table[key], value]
15
+ end
16
+
17
+ manager.insert(values)
18
+ manager.to_sql + ' RETURNING cartodb_id'
19
+ end
20
+
21
+ def update(raw_values, id_column, id_value)
22
+ values = []
23
+
24
+ raw_values.each do |key, value|
25
+ values << [@table[key], value]
26
+ end
27
+
28
+ manager = Arel::UpdateManager.new
29
+ manager.table @table
30
+ manager.where @table[id_column].eq(id_value)
31
+ manager.set values
32
+ return manager.to_sql
33
+ end
34
+
35
+ def delete(id_column, id_value)
36
+ manager = Arel::DeleteManager.new
37
+ manager.from @table
38
+
39
+ manager.where @table[id_column].eq(id_value)
40
+
41
+ return manager.to_sql
42
+ end
43
+
44
+ end
45
+ end
@@ -0,0 +1,3 @@
1
+ module Cartomodel
2
+ VERSION = "0.1.0"
3
+ end
data/lib/cartomodel.rb ADDED
@@ -0,0 +1,9 @@
1
+ require 'active_support/concern'
2
+ require 'active_record'
3
+ require 'cartomodel/version'
4
+ require 'cartomodel/models/synchronizable'
5
+ require 'cartomodel/query_generator'
6
+ require 'cartowrap'
7
+
8
+ module Cartomodel
9
+ end
metadata ADDED
@@ -0,0 +1,163 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cartomodel
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Tiago Garcia
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-04-25 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rspec
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '3.0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '3.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec-expectations
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: sqlite3
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: activesupport
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: 5.0.0.beta3
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: 5.0.0.beta3
83
+ - !ruby/object:Gem::Dependency
84
+ name: activerecord
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: 5.0.0.beta3
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: 5.0.0.beta3
97
+ - !ruby/object:Gem::Dependency
98
+ name: arel
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '7.0'
104
+ type: :runtime
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '7.0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: cartowrap
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :runtime
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ description: CartoDB sync for ActiveRecord objects
126
+ email:
127
+ - tiagojsag@gmail.com
128
+ executables: []
129
+ extensions: []
130
+ extra_rdoc_files: []
131
+ files:
132
+ - README.md
133
+ - Rakefile
134
+ - lib/cartomodel.rb
135
+ - lib/cartomodel/errors.rb
136
+ - lib/cartomodel/models/synchronizable.rb
137
+ - lib/cartomodel/query_generator.rb
138
+ - lib/cartomodel/version.rb
139
+ homepage: http://vizzuality.com
140
+ licenses:
141
+ - MIT
142
+ metadata: {}
143
+ post_install_message:
144
+ rdoc_options: []
145
+ require_paths:
146
+ - lib
147
+ required_ruby_version: !ruby/object:Gem::Requirement
148
+ requirements:
149
+ - - ">="
150
+ - !ruby/object:Gem::Version
151
+ version: '0'
152
+ required_rubygems_version: !ruby/object:Gem::Requirement
153
+ requirements:
154
+ - - ">="
155
+ - !ruby/object:Gem::Version
156
+ version: '0'
157
+ requirements: []
158
+ rubyforge_project:
159
+ rubygems_version: 2.5.1
160
+ signing_key:
161
+ specification_version: 4
162
+ summary: CartoDB sync for ActiveRecord objects
163
+ test_files: []