ixtlan-remote 0.1.4 → 0.1.5
Sign up to get free protection for your applications and to get access to all the features.
@@ -31,8 +31,9 @@ module Ixtlan
|
|
31
31
|
@new_method = new_method || @model.method(:new)
|
32
32
|
end
|
33
33
|
|
34
|
-
def create_result_objects( result )
|
35
|
-
|
34
|
+
def create_result_objects( result )
|
35
|
+
# naiv singularize english plural
|
36
|
+
root = @model.to_s.sub(/.*::/, '').underscore.sub( /s$/, '')
|
36
37
|
if result.is_a? Array
|
37
38
|
result.collect do |r|
|
38
39
|
new_instance( r[root] || r )
|
@@ -142,4 +143,4 @@ module Ixtlan
|
|
142
143
|
end
|
143
144
|
end
|
144
145
|
end
|
145
|
-
end
|
146
|
+
end
|
@@ -1,3 +1,23 @@
|
|
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
|
+
#
|
1
21
|
require 'ixtlan/remote/model_helpers'
|
2
22
|
module Ixtlan
|
3
23
|
module Remote
|
@@ -5,48 +25,42 @@ module Ixtlan
|
|
5
25
|
|
6
26
|
include ModelHelpers
|
7
27
|
|
8
|
-
def initialize(resource,
|
28
|
+
def initialize(resource, model, new_method = nil)
|
9
29
|
@base = resource
|
10
|
-
@
|
30
|
+
@model = model
|
31
|
+
@new_method = new_method || @model.method(:new)
|
11
32
|
end
|
12
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
|
+
|
13
49
|
def send_it(&block)
|
14
50
|
raise "no method given - call any CRUD method first" unless @method
|
15
51
|
headers = {:content_type => :json, :accept => :json}
|
16
52
|
headers[:params] = @params if @params
|
53
|
+
p @resource
|
17
54
|
result =
|
18
|
-
if @
|
19
|
-
@resource.send @method, @payload.to_json, headers, &block
|
55
|
+
if @method != :get
|
56
|
+
@resource.send( @method, @payload ? @payload.to_json : '', headers, &block )
|
20
57
|
else
|
21
|
-
@resource.send @method, headers, &block
|
22
|
-
end
|
23
|
-
unless result.blank?
|
24
|
-
result = JSON.load(result)
|
25
|
-
if @model
|
26
|
-
root = @map[ @model ].singular || @model_key
|
27
|
-
if result.is_a? Array
|
28
|
-
if result.empty?
|
29
|
-
[]
|
30
|
-
else
|
31
|
-
root =
|
32
|
-
if result[ 1 ].is_a?( Hash ) && result[ 1 ].size == 1
|
33
|
-
root
|
34
|
-
end
|
35
|
-
result.collect do |r|
|
36
|
-
new_instance( @model, r[root] || r )
|
37
|
-
end
|
38
|
-
end
|
39
|
-
else
|
40
|
-
attr = if result.is_a?( Hash ) && result.size == 1
|
41
|
-
result[root]
|
42
|
-
else
|
43
|
-
result
|
44
|
-
end
|
45
|
-
new_instance(@model, attr)
|
46
|
-
end
|
47
|
-
else
|
48
|
-
result
|
58
|
+
@resource.send( @method, headers, &block )
|
49
59
|
end
|
60
|
+
if result && result.size > 0
|
61
|
+
create_result_objects( JSON.load( result ) )
|
62
|
+
else
|
63
|
+
nil
|
50
64
|
end
|
51
65
|
end
|
52
66
|
|
@@ -55,10 +69,15 @@ module Ixtlan
|
|
55
69
|
self
|
56
70
|
end
|
57
71
|
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
72
|
+
def last(args)
|
73
|
+
if args.last.is_a?(Hash)
|
74
|
+
args.reverse!
|
75
|
+
l = args.shift
|
76
|
+
args.reverse!
|
77
|
+
l
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
62
81
|
def retrieve(*args)
|
63
82
|
@method = :get
|
64
83
|
@params = nil
|
@@ -67,11 +86,9 @@ module Ixtlan
|
|
67
86
|
self
|
68
87
|
end
|
69
88
|
|
70
|
-
# create(locale) => POST /locales
|
71
|
-
# create(:locale => {:code => 'en'}) => POST /locales
|
72
89
|
def create(*obj_or_hash)
|
73
90
|
@method = :post
|
74
|
-
@payload =
|
91
|
+
@payload = last(obj_or_hash)
|
75
92
|
@params = nil
|
76
93
|
@resource = @base[path(*obj_or_hash)]
|
77
94
|
self
|
@@ -79,7 +96,7 @@ module Ixtlan
|
|
79
96
|
|
80
97
|
def update(*obj_or_hash)
|
81
98
|
@method = :put
|
82
|
-
@payload =
|
99
|
+
@payload = last(obj_or_hash)
|
83
100
|
@params = nil
|
84
101
|
@resource = @base[path(*obj_or_hash)]
|
85
102
|
self
|
@@ -87,7 +104,7 @@ module Ixtlan
|
|
87
104
|
|
88
105
|
def delete(*obj_or_hash)
|
89
106
|
@method = :delete
|
90
|
-
@payload =
|
107
|
+
@payload = last(obj_or_hash)
|
91
108
|
@params = nil
|
92
109
|
@resource = @base[path(*obj_or_hash)]
|
93
110
|
self
|
@@ -99,54 +116,31 @@ module Ixtlan
|
|
99
116
|
|
100
117
|
private
|
101
118
|
|
102
|
-
def new_instance(
|
103
|
-
@
|
119
|
+
def new_instance( attributes )
|
120
|
+
@new_method.call( attributes )
|
104
121
|
end
|
105
122
|
|
106
123
|
def path(*parts)
|
107
|
-
parts.collect
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
124
|
+
parts.flatten.collect do |part|
|
125
|
+
case part
|
126
|
+
when Class
|
127
|
+
part.to_s.pluralize.underscore
|
128
|
+
else
|
129
|
+
if part.respond_to? :attributes
|
130
|
+
part.class.to_s.pluralize.underscore
|
131
|
+
else
|
132
|
+
part.to_s
|
133
|
+
end
|
134
|
+
end
|
135
|
+
end * '/'
|
116
136
|
end
|
117
137
|
|
118
138
|
def path_part(data)
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
@payload = data
|
123
|
-
if model_set(model)
|
124
|
-
model = model.pluralize
|
125
|
-
else
|
126
|
-
model = nil
|
127
|
-
end
|
128
|
-
when Array
|
129
|
-
@payload = data
|
130
|
-
if model_set model
|
131
|
-
model = model.pluralize
|
132
|
-
end
|
133
|
-
when String
|
134
|
-
model_set model
|
135
|
-
when Fixnum
|
136
|
-
model = data.to_s
|
137
|
-
when Symbol
|
138
|
-
model_set model
|
139
|
-
when Class
|
140
|
-
model_set model
|
141
|
-
model = @map.member?( model ) ? @map[ model ].path : model.pluralize
|
142
|
-
else
|
143
|
-
@payload = data
|
144
|
-
model_set model
|
145
|
-
model = model.pluralize
|
139
|
+
unless data.is_a?(Hash)
|
140
|
+
data = [data] unless data.is_a?(Array)
|
141
|
+
data.collect { |d| d.to_s }.join("/")
|
146
142
|
end
|
147
|
-
model.underscore if model
|
148
143
|
end
|
149
|
-
|
150
144
|
end
|
151
145
|
end
|
152
146
|
end
|
data/lib/ixtlan/remote/sync.rb
CHANGED
@@ -19,7 +19,6 @@
|
|
19
19
|
# along with ixtlan-remote. If not, see <http://www.gnu.org/licenses/>.
|
20
20
|
#
|
21
21
|
require 'ixtlan/remote/summary'
|
22
|
-
require 'active_support/all'
|
23
22
|
|
24
23
|
module Ixtlan
|
25
24
|
module Remote
|
@@ -43,18 +42,18 @@ module Ixtlan
|
|
43
42
|
# max method ORM dependent
|
44
43
|
if defined? ActiveRecord
|
45
44
|
def max(clazz)
|
46
|
-
(clazz.maximum(:updated_at) || DateTime.new(
|
45
|
+
( clazz.maximum( :updated_at ) || DateTime.new( 1 ) ).to_datetime
|
47
46
|
end
|
48
47
|
else
|
49
48
|
def max(clazz)
|
50
|
-
clazz.max(:updated_at)
|
49
|
+
clazz.max(:updated_at) || DateTime.new( 1 )
|
51
50
|
end
|
52
51
|
end
|
53
52
|
private :max
|
54
53
|
|
55
54
|
# UTC timestamp of last updated record
|
56
55
|
def last_update( clazz )
|
57
|
-
last_date =
|
56
|
+
last_date = max( clazz )
|
58
57
|
last_date.strftime( '%Y-%m-%d %H:%M:%S.' ) + ( "%06d" % ( last_date.sec_fraction / NANOSECONDS_IN_DAY / 1000 ) ) + "+0:00"
|
59
58
|
end
|
60
59
|
private :last_update
|
@@ -101,4 +100,4 @@ module Ixtlan
|
|
101
100
|
alias :to_s :to_log
|
102
101
|
end
|
103
102
|
end
|
104
|
-
end
|
103
|
+
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ixtlan-remote
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 17
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 1
|
9
|
-
-
|
10
|
-
version: 0.1.
|
9
|
+
- 5
|
10
|
+
version: 0.1.5
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Christian Meier
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2013-01-
|
18
|
+
date: 2013-01-16 00:00:00 Z
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
21
21
|
name: rest-client
|