ixtlan-remote 0.1.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 (89) hide show
  1. data/Gemfile +2 -0
  2. data/README.md +64 -0
  3. data/agpl-3.0.txt +661 -0
  4. data/lib/ixtlan-remote.rb +23 -0
  5. data/lib/ixtlan/passwords.rb +52 -0
  6. data/lib/ixtlan/passwords.rb~ +47 -0
  7. data/lib/ixtlan/railtie.rb~ +17 -0
  8. data/lib/ixtlan/remote.rb +23 -0
  9. data/lib/ixtlan/remote/access_controller.rb +62 -0
  10. data/lib/ixtlan/remote/access_controller.rb~ +25 -0
  11. data/lib/ixtlan/remote/constant_time_compare.rb +39 -0
  12. data/lib/ixtlan/remote/constant_time_compare.rb~ +19 -0
  13. data/lib/ixtlan/remote/heartbeat.rb~ +80 -0
  14. data/lib/ixtlan/remote/model_conversion.rb~ +245 -0
  15. data/lib/ixtlan/remote/model_helpers.rb +73 -0
  16. data/lib/ixtlan/remote/model_helpers.rb~ +53 -0
  17. data/lib/ixtlan/remote/permission.rb +40 -0
  18. data/lib/ixtlan/remote/permission.rb~ +19 -0
  19. data/lib/ixtlan/remote/railtie.rb +30 -0
  20. data/lib/ixtlan/remote/railtie.rb~ +9 -0
  21. data/lib/ixtlan/remote/remote_access_controller.rb~ +25 -0
  22. data/lib/ixtlan/remote/remote_permisson.rb~ +9 -0
  23. data/lib/ixtlan/remote/resource.rb +145 -0
  24. data/lib/ixtlan/remote/resource.rb-~ +165 -0
  25. data/lib/ixtlan/remote/resource.rb~ +152 -0
  26. data/lib/ixtlan/remote/rest.rb +108 -0
  27. data/lib/ixtlan/remote/rest.rb~ +83 -0
  28. data/lib/ixtlan/remote/rest_resource.rb~ +140 -0
  29. data/lib/ixtlan/remote/rest_resource_config.rb~ +63 -0
  30. data/lib/ixtlan/remote/rest_resource_factory.rb~ +259 -0
  31. data/lib/ixtlan/remote/rest_resource_old.rb-~ +259 -0
  32. data/lib/ixtlan/remote/server.rb +119 -0
  33. data/lib/ixtlan/remote/server.rb~ +82 -0
  34. data/lib/ixtlan/remote/summary.rb +46 -0
  35. data/lib/ixtlan/remote/summary.rb~ +23 -0
  36. data/lib/ixtlan/remote/sync.rb +104 -0
  37. data/lib/ixtlan/remote/sync.rb~ +78 -0
  38. data/lib/ixtlan/remote/sync_summary.rb~ +23 -0
  39. data/lib/ixtlan/remote/tranlation_key.rb~ +7 -0
  40. data/lib/ixtlan/remote/translation.rake~ +194 -0
  41. data/lib/ixtlan/remote/translation_models.rb~ +11 -0
  42. data/lib/ixtlan/remote/updater.rb~ +71 -0
  43. data/lib/ixtlan/user_management/application_model.rb +30 -0
  44. data/lib/ixtlan/user_management/application_model.rb~ +21 -0
  45. data/lib/ixtlan/user_management/application_resource.rb +48 -0
  46. data/lib/ixtlan/user_management/application_resource.rb~ +21 -0
  47. data/lib/ixtlan/user_management/authentcator.rb~ +31 -0
  48. data/lib/ixtlan/user_management/authentication_model.rb +31 -0
  49. data/lib/ixtlan/user_management/authentication_model.rb~ +21 -0
  50. data/lib/ixtlan/user_management/authenticator.rb +55 -0
  51. data/lib/ixtlan/user_management/authenticator.rb~ +20 -0
  52. data/lib/ixtlan/user_management/domain_resource.rb +48 -0
  53. data/lib/ixtlan/user_management/domain_resource.rb~ +21 -0
  54. data/lib/ixtlan/user_management/dummy_authentication.rb +50 -0
  55. data/lib/ixtlan/user_management/dummy_authentication.rb~ +49 -0
  56. data/lib/ixtlan/user_management/group.rb~ +39 -0
  57. data/lib/ixtlan/user_management/group_model.rb +31 -0
  58. data/lib/ixtlan/user_management/group_model.rb~ +21 -0
  59. data/lib/ixtlan/user_management/models.rb~ +39 -0
  60. data/lib/ixtlan/user_management/session-serializer.rb~ +18 -0
  61. data/lib/ixtlan/user_management/session_cuba.rb +47 -0
  62. data/lib/ixtlan/user_management/session_cuba.rb~ +44 -0
  63. data/lib/ixtlan/user_management/session_manager.rb +38 -0
  64. data/lib/ixtlan/user_management/session_model.rb +36 -0
  65. data/lib/ixtlan/user_management/session_model.rb~ +10 -0
  66. data/lib/ixtlan/user_management/session_plugin.rb +32 -0
  67. data/lib/ixtlan/user_management/session_serializer.rb +21 -0
  68. data/lib/ixtlan/user_management/session_serializer.rb~ +21 -0
  69. data/lib/ixtlan/user_management/user.rb~ +16 -0
  70. data/lib/ixtlan/user_management/user_model.rb +36 -0
  71. data/lib/ixtlan/user_management/user_model.rb~ +33 -0
  72. data/lib/ixtlan/user_management/user_resource.rb +55 -0
  73. data/lib/ixtlan/user_management/user_resource.rb~ +24 -0
  74. data/lib/ixtlan/user_management/user_serializer.rb +15 -0
  75. data/lib/ixtlan/user_management/user_serializer.rb~ +23 -0
  76. data/spec/access_controller_spec.rb +65 -0
  77. data/spec/access_controller_spec.rb~ +65 -0
  78. data/spec/model_helpers_spec.rb +40 -0
  79. data/spec/model_helpers_spec.rb~ +36 -0
  80. data/spec/remote_access_controller_spec.rb~ +36 -0
  81. data/spec/resource_spec.rb +181 -0
  82. data/spec/resource_spec.rb~ +173 -0
  83. data/spec/rest_resource_spec.rb~ +173 -0
  84. data/spec/rest_spec.rb +94 -0
  85. data/spec/rest_spec.rb~ +99 -0
  86. data/spec/rest_with_attribute_name_like_model_name_spec.rb +82 -0
  87. data/spec/sync_spec.rb +83 -0
  88. data/spec/sync_spec.rb~ +81 -0
  89. metadata +313 -0
@@ -0,0 +1,73 @@
1
+ #
2
+ # ixtlan-remote - helper sync data between miniapps or communicate wth realtime
3
+ # rest-services
4
+ # Copyright (C) 2012 Christian Meier
5
+ #
6
+ # This file is part of ixtlan-remote.
7
+ #
8
+ # ixtlan-remote is free software: you can redistribute it and/or modify
9
+ # it under the terms of the GNU Affero General Public License as
10
+ # published by the Free Software Foundation, either version 3 of the
11
+ # License, or (at your option) any later version.
12
+ #
13
+ # ixtlan-remote is distributed in the hope that it will be useful,
14
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
15
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
+ # GNU Affero General Public License for more details.
17
+ #
18
+ # You should have received a copy of the GNU Affero General Public License
19
+ # along with ixtlan-remote. If not, see <http://www.gnu.org/licenses/>.
20
+ #
21
+ module Ixtlan
22
+ module Remote
23
+ module ModelHelpers
24
+
25
+ def to_model_underscore( data )
26
+ m = to_model( data )
27
+ m.to_s.underscore if m
28
+ end
29
+
30
+ def to_model_singular_underscore( data )
31
+ to_model_underscore( data ).sub( /s$/, '' )
32
+ end
33
+
34
+ def to_model( data )
35
+ case data
36
+ when Hash
37
+ if data.size == 1
38
+ case data.values.first
39
+ when Array
40
+ data.keys.first
41
+ when Hash
42
+ data.keys.first
43
+ end
44
+ end
45
+ when Array
46
+ if data.empty?
47
+ #TODO
48
+ else
49
+ to_model_class(data[0].class)
50
+ end
51
+ when String
52
+ data
53
+ when Symbol
54
+ data
55
+ when Class
56
+ to_model_class(data)
57
+ else
58
+ to_model_class(data.class)
59
+ end
60
+ end
61
+
62
+ def to_model_class(obj)
63
+ if obj.respond_to?(:model_name)
64
+ obj.model_name.constantize
65
+ elsif obj.is_a? Class
66
+ obj
67
+ else
68
+ obj.class
69
+ end
70
+ end
71
+ end
72
+ end
73
+ end
@@ -0,0 +1,53 @@
1
+ module Ixtlan
2
+ module Remote
3
+ module ModelHelpers
4
+
5
+ def to_model_underscore(data)
6
+ m = to_model(data)
7
+ m.to_s.underscore if m
8
+ end
9
+
10
+ def to_model_singular_underscore(data)
11
+ to_model_underscore(data).singularize
12
+ end
13
+
14
+ def to_model(data)
15
+ case data
16
+ when Hash
17
+ if data.size == 1
18
+ case data.values.first
19
+ when Array
20
+ data.keys.first
21
+ when Hash
22
+ data.keys.first
23
+ end
24
+ end
25
+ when Array
26
+ if data.empty?
27
+ #TODO
28
+ else
29
+ to_model_class(data[0].class)
30
+ end
31
+ when String
32
+ data
33
+ when Symbol
34
+ data
35
+ when Class
36
+ to_model_class(data)
37
+ else
38
+ to_model_class(data.class)
39
+ end
40
+ end
41
+
42
+ def to_model_class(obj)
43
+ if obj.respond_to?(:model_name)
44
+ obj.model_name.constantize
45
+ elsif obj.is_a? Class
46
+ obj
47
+ else
48
+ obj.class
49
+ end
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,40 @@
1
+ #
2
+ # ixtlan-remote - helper sync data between miniapps or communicate wth realtime
3
+ # rest-services
4
+ # Copyright (C) 2012 Christian Meier
5
+ #
6
+ # This file is part of ixtlan-remote.
7
+ #
8
+ # ixtlan-remote is free software: you can redistribute it and/or modify
9
+ # it under the terms of the GNU Affero General Public License as
10
+ # published by the Free Software Foundation, either version 3 of the
11
+ # License, or (at your option) any later version.
12
+ #
13
+ # ixtlan-remote is distributed in the hope that it will be useful,
14
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
15
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
+ # GNU Affero General Public License for more details.
17
+ #
18
+ # You should have received a copy of the GNU Affero General Public License
19
+ # along with ixtlan-remote. If not, see <http://www.gnu.org/licenses/>.
20
+ #
21
+ module Ixtlan
22
+ module Remote
23
+ class Permission
24
+
25
+ include DataMapper::Resource
26
+
27
+ # key for selectng the IdentityMap should remain this class if
28
+ # there is no single table inheritance with Discriminator in place
29
+ # i.e. the subclass used as key for the IdentityMap
30
+ def self.base_model
31
+ self
32
+ end
33
+
34
+ property :id, Serial
35
+
36
+ property :allowed_ip, String
37
+ property :authentication_token, String
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,19 @@
1
+ module Ixtlan
2
+ module Remote
3
+ class Permission
4
+
5
+ include DataMapper::Resource
6
+
7
+ # key for selectng the IdentityMap should remain the class
8
+ # there is no single table inheritance with Discrimantor in place
9
+ def self.base_model
10
+ self
11
+ end
12
+
13
+ property :id, Serial
14
+
15
+ property :ip, String
16
+ property :token, String
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,30 @@
1
+ #
2
+ # ixtlan-remote - helper sync data between miniapps or communicate wth realtime
3
+ # rest-services
4
+ # Copyright (C) 2012 Christian Meier
5
+ #
6
+ # This file is part of ixtlan-remote.
7
+ #
8
+ # ixtlan-remote is free software: you can redistribute it and/or modify
9
+ # it under the terms of the GNU Affero General Public License as
10
+ # published by the Free Software Foundation, either version 3 of the
11
+ # License, or (at your option) any later version.
12
+ #
13
+ # ixtlan-remote is distributed in the hope that it will be useful,
14
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
15
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
+ # GNU Affero General Public License for more details.
17
+ #
18
+ # You should have received a copy of the GNU Affero General Public License
19
+ # along with ixtlan-remote. If not, see <http://www.gnu.org/licenses/>.
20
+ #
21
+ require 'ixtlan/remote/rest'
22
+ module Ixtlan
23
+ module Remote
24
+ class Railtie < Rails::Railtie
25
+
26
+ config.rest = Ixtlan::Remote::Rest.new
27
+
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,9 @@
1
+ require 'ixtlan/remote/rest'
2
+ module Ixtlan
3
+
4
+ class RemoteGettextRailtie < Rails::Railtie
5
+
6
+ config.rest = Ixtlan::Remote::Rest.new
7
+
8
+ end
9
+ end
@@ -0,0 +1,25 @@
1
+ require 'ixtlan/remote/constant_time_compare'
2
+ module Ixtlan
3
+ module Remote
4
+ module RemoteAccessController
5
+ include Ixtlan::Remote::ConstantTimeCompare
6
+
7
+ def x_service_token
8
+ @_x_service_token ||= request.headers['X-SERVICE-TOKEN']
9
+ end
10
+
11
+ def remote_permission
12
+ # constant time for finding the right permission
13
+ perm = nil
14
+ Permission.all.each do |rp|
15
+ perm = rp if constant_time_compare(rp.token, x_service_token)
16
+ end
17
+ raise "ip #{request.remote_ip} wrong authentication" unless perm
18
+ # if the perm.ip == nil then do not check IP
19
+ # server clusters have many IPs then use perm.ip = nil
20
+ raise "ip #{request.remote_ip} not allowed" if (!perm.ip.blank? && request.remote_ip != perm.ip)
21
+ perm
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,9 @@
1
+ class RemotePermission
2
+
3
+ include DataMapper::Resource
4
+
5
+ property :id, Serial
6
+
7
+ property :ip, String
8
+ property :token, String
9
+ end
@@ -0,0 +1,145 @@
1
+ #
2
+ # ixtlan-remote - helper sync data between miniapps or communicate wth realtime
3
+ # rest-services
4
+ # Copyright (C) 2012 Christian Meier
5
+ #
6
+ # This file is part of ixtlan-remote.
7
+ #
8
+ # ixtlan-remote is free software: you can redistribute it and/or modify
9
+ # it under the terms of the GNU Affero General Public License as
10
+ # published by the Free Software Foundation, either version 3 of the
11
+ # License, or (at your option) any later version.
12
+ #
13
+ # ixtlan-remote is distributed in the hope that it will be useful,
14
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
15
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
+ # GNU Affero General Public License for more details.
17
+ #
18
+ # You should have received a copy of the GNU Affero General Public License
19
+ # along with ixtlan-remote. If not, see <http://www.gnu.org/licenses/>.
20
+ #
21
+ require 'ixtlan/remote/model_helpers'
22
+ module Ixtlan
23
+ module Remote
24
+ class Resource
25
+
26
+ include ModelHelpers
27
+
28
+ def initialize(resource, model, new_method = nil)
29
+ @base = resource
30
+ @model = model
31
+ @new_method = new_method || @model.method(:new)
32
+ end
33
+
34
+ def create_result_objects( result )
35
+ root = @model.to_s.sub(/.*::/, '').underscore.singularize
36
+ if result.is_a? Array
37
+ result.collect do |r|
38
+ new_instance( r[root] || r )
39
+ end
40
+ else
41
+ attr = if result.is_a?( Hash ) && result.size == 1
42
+ result[root]
43
+ end
44
+ new_instance( attr || result )
45
+ end
46
+ end
47
+ private :create_result_objects
48
+
49
+ def send_it(&block)
50
+ raise "no method given - call any CRUD method first" unless @method
51
+ headers = {:content_type => :json, :accept => :json}
52
+ headers[:params] = @params if @params
53
+ result =
54
+ if @method != :get
55
+ @resource.send( @method, @payload ? @payload.to_json : '', headers, &block )
56
+ else
57
+ @resource.send( @method, headers, &block )
58
+ end
59
+ if result && result.size > 0
60
+ create_result_objects( JSON.load( result ) )
61
+ else
62
+ nil
63
+ end
64
+ end
65
+
66
+ def query_params(params)
67
+ @params = params
68
+ self
69
+ end
70
+
71
+ def last(args)
72
+ if args.last.is_a?(Hash)
73
+ args.reverse!
74
+ l = args.shift
75
+ args.reverse!
76
+ l
77
+ end
78
+ end
79
+
80
+ def retrieve(*args)
81
+ @method = :get
82
+ @params = nil
83
+ @resource = @base[path(*args)]
84
+ @payload = nil
85
+ self
86
+ end
87
+
88
+ def create(*obj_or_hash)
89
+ @method = :post
90
+ @payload = last(obj_or_hash)
91
+ @params = nil
92
+ @resource = @base[path(*obj_or_hash)]
93
+ self
94
+ end
95
+
96
+ def update(*obj_or_hash)
97
+ @method = :put
98
+ @payload = last(obj_or_hash)
99
+ @params = nil
100
+ @resource = @base[path(*obj_or_hash)]
101
+ self
102
+ end
103
+
104
+ def delete(*obj_or_hash)
105
+ @method = :delete
106
+ @payload = last(obj_or_hash)
107
+ @params = nil
108
+ @resource = @base[path(*obj_or_hash)]
109
+ self
110
+ end
111
+
112
+ def model
113
+ @model
114
+ end
115
+
116
+ private
117
+
118
+ def new_instance( attributes )
119
+ @new_method.call( attributes )
120
+ end
121
+
122
+ def path(*parts)
123
+ parts.flatten.collect do |part|
124
+ case part
125
+ when Class
126
+ part.to_s.pluralize.underscore
127
+ else
128
+ if part.respond_to? :attributes
129
+ part.class.to_s.pluralize.underscore
130
+ else
131
+ part.to_s
132
+ end
133
+ end
134
+ end * '/'
135
+ end
136
+
137
+ def path_part(data)
138
+ unless data.is_a?(Hash)
139
+ data = [data] unless data.is_a?(Array)
140
+ data.collect { |d| d.to_s }.join("/")
141
+ end
142
+ end
143
+ end
144
+ end
145
+ end
@@ -0,0 +1,165 @@
1
+ require 'ixtlan/remote/model_helpers'
2
+ module Ixtlan
3
+ module Remote
4
+ class Resource
5
+
6
+ include ModelHelpers
7
+
8
+ def initialize(resource, model_map = {})
9
+ @base = resource
10
+ @map = model_map
11
+ end
12
+
13
+ def send_it(&block)
14
+ raise "no method given - call any CRUD method first" unless @method
15
+ headers = {:content_type => :json, :accept => :json}
16
+ headers[:params] = @params if @params
17
+ result =
18
+ if @method != :get
19
+ @resource.send( @method, @payload.to_json, headers, &block )
20
+ else
21
+ @resource.send( @method, headers, &block )
22
+ end
23
+ unless result.blank?
24
+ result = JSON.load( result )
25
+ puts "+" * 80
26
+ p result
27
+ p @model
28
+ p @map
29
+ p @model_key
30
+ puts "+" * 80
31
+ if @model && @map[ @model ]
32
+ root = @map[ @model ]
33
+ # root.sub!(/s$/, '') if root.is_a? String
34
+ root = @model_key
35
+ if result.is_a? Array
36
+ if result.empty?
37
+ []
38
+ else
39
+ root =
40
+ if result[ 0 ].is_a?( Hash ) && result[ 0 ].size == 1
41
+ root
42
+ end
43
+ result.collect do |r|
44
+ new_instance( @model, r[root] || r )
45
+ end
46
+ end
47
+ else
48
+ attr = if result.is_a?( Hash ) && result.size == 1
49
+ result[root] || result
50
+ else
51
+ result
52
+ end
53
+ p attr
54
+ new_instance(@model, attr)
55
+ end
56
+ else
57
+ result
58
+ end
59
+ end
60
+ end
61
+
62
+ def query_params(params)
63
+ @params = params
64
+ self
65
+ end
66
+
67
+ # retrieve(Locale) => GET /locales
68
+ # retrieve(Locale, 1) => GET /locales/1
69
+ # retrieve(:locales) => GET /locales
70
+ # retrieve(:locales, 1) => GET /locales/1
71
+ def retrieve(*args)
72
+ @method = :get
73
+ @params = nil
74
+ @resource = @base[path(*args)]
75
+ @payload = nil
76
+ self
77
+ end
78
+
79
+ # create(locale) => POST /locales
80
+ # create(:locale => {:code => 'en'}) => POST /locales
81
+ def create(*obj_or_hash)
82
+ @method = :post
83
+ @payload = nil#obj_or_hash.is_a?(Class)? nil : obj_or_hash
84
+ @params = nil
85
+ @resource = @base[path(*obj_or_hash)]
86
+ self
87
+ end
88
+
89
+ def update(*obj_or_hash)
90
+ @method = :put
91
+ @payload = nil#obj_or_hash.is_a?(Class)? nil : obj_or_hash
92
+ @params = nil
93
+ @resource = @base[path(*obj_or_hash)]
94
+ self
95
+ end
96
+
97
+ def delete(*obj_or_hash)
98
+ @method = :delete
99
+ @payload = nil #obj_or_hash.is_a?(Class)? nil : obj_or_hash
100
+ @params = nil
101
+ @resource = @base[path(*obj_or_hash)]
102
+ self
103
+ end
104
+
105
+ def model
106
+ @model
107
+ end
108
+
109
+ private
110
+
111
+ def new_instance( clazz, attributes )
112
+ @map[ clazz ].new( attributes )
113
+ end
114
+
115
+ def path(*parts)
116
+ parts.collect { |p| path_part( p ) }.delete_if { |p| p.nil? } * '/'
117
+ end
118
+
119
+ def model_set(model)
120
+ if model
121
+ m = model.to_s.singularize.camelize.constantize rescue nil
122
+ if m
123
+ @model = m
124
+ @model_key = model.to_s.singularize.underscore
125
+ end
126
+ end
127
+ end
128
+
129
+ def path_part(data)
130
+ model = to_model_underscore(data)
131
+ p "model" * 10
132
+ p model
133
+ case data
134
+ when Hash
135
+ @payload = data
136
+ if model_set(model)
137
+ model = model.pluralize
138
+ else
139
+ model = nil
140
+ end
141
+ when Array
142
+ @payload = data
143
+ if model_set model
144
+ model = model.pluralize
145
+ end
146
+ when String
147
+ model_set model
148
+ when Fixnum
149
+ model = data.to_s
150
+ when Symbol
151
+ model_set model
152
+ when Class
153
+ model_set model
154
+ model = @map.member?( data ) ? @map[ data ].path : model.pluralize
155
+ else
156
+ @payload = data
157
+ model_set model
158
+ model = model.pluralize
159
+ end
160
+ model.underscore if model
161
+ end
162
+
163
+ end
164
+ end
165
+ end