rhoconnect-rb 0.2.5 → 0.2.6
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.
- data/README.md +22 -9
- data/lib/rhoconnect/resource.rb +7 -10
- data/lib/rhoconnect/version.rb +1 -1
- data/spec/resource_spec.rb +17 -7
- metadata +11 -13
data/README.md
CHANGED
@@ -31,7 +31,7 @@ Or, if you are using DataMapper:
|
|
31
31
|
|
32
32
|
Next, your models will need to declare a partition key for `rhoconnect-rb`. This partition key is used by `rhoconnect-rb` to uniquely identify the model dataset when it is stored in a rhoconnect instance. It is typically an attribute on the model or related model. `rhoconnect-rb` supports two types of partitions:
|
33
33
|
|
34
|
-
* :app - No unique key will be used, a shared dataset is
|
34
|
+
* :app - No unique key will be used, a shared dataset is synchronized for all users.
|
35
35
|
* lambda { some lambda } - Execute a lambda which returns the unique key string.
|
36
36
|
|
37
37
|
For example, the `Product` model above might have a `belongs_to :user` relationship. This provides us a simple way to organize the `Product` dataset for rhoconnect by reusing this relationship. The partition identifying a username would be declared as:
|
@@ -41,12 +41,16 @@ For example, the `Product` model above might have a `belongs_to :user` relations
|
|
41
41
|
|
42
42
|
belongs_to :user
|
43
43
|
|
44
|
-
partition
|
44
|
+
def partition
|
45
|
+
lambda { self.user.username }
|
46
|
+
end
|
45
47
|
end
|
46
48
|
|
47
49
|
Now all of the `Product` data synchronized by rhoconnect will organized by `self.user.username`. Note: You can also used a fixed key if the dataset doesn't require a dynamic value:
|
48
50
|
|
49
|
-
partition
|
51
|
+
def partition
|
52
|
+
:app
|
53
|
+
end
|
50
54
|
|
51
55
|
For more information about Rhoconnect partitions, please refer to the [Rhoconnect docs](http://docs.rhomobile.com/rhosync/source-adapters#data-partitioning).
|
52
56
|
|
@@ -59,7 +63,9 @@ For more information about Rhoconnect partitions, please refer to the [Rhoconnec
|
|
59
63
|
|
60
64
|
belongs_to :user
|
61
65
|
|
62
|
-
partition
|
66
|
+
def partition
|
67
|
+
lambda { self.user.username }
|
68
|
+
end
|
63
69
|
|
64
70
|
def self.rhoconnect_query(partition)
|
65
71
|
Product.where(:user_id => partition)
|
@@ -70,24 +76,30 @@ In this example, `self.rhoconnect_query` returns a list of products where the pa
|
|
70
76
|
|
71
77
|
### Configuration and Authentication
|
72
78
|
|
73
|
-
Configure Rhoconnect in an initializer like `config/initializers/rhoconnect.rb` (for Rails), or directly in your application (i.e. Sinatra):
|
79
|
+
Configure Rhoconnect in an initializer like `config/initializers/rhoconnect.rb` (for Rails), or directly in your application (i.e. Sinatra). Here you will setup the rhoconnect uri (the location of your rhoconnect instance), and app\_endpoint (the location of your ruby app):
|
80
|
+
|
74
81
|
|
75
|
-
# Setup the Rhoconnect uri and api token.
|
76
82
|
# Use rhoconnect:get_token to get the token value.
|
77
83
|
|
78
84
|
config.uri = "http://myrhoconnect.com"
|
79
85
|
config.token = "secrettoken"
|
86
|
+
config.app_endpoint = "http://myapp.heroku.com"
|
87
|
+
|
88
|
+
If `app_endpoint` is defined, your Rhoconnect instance will be configured to query data from the endpoint using the rhoconnect_query method in your model. For example, if your `app_endpoint` is defined as "http://myapp.heroku.com", Rhoconnect will query data with:
|
89
|
+
|
90
|
+
POST http://myapp.heroku.com/rhoconnect/query
|
80
91
|
|
81
92
|
Example:
|
82
93
|
|
83
94
|
Rhoconnect.configure do |config|
|
84
|
-
config.uri
|
85
|
-
config.token
|
95
|
+
config.uri = "http://myrhoconnect-server.com"
|
96
|
+
config.token = "secrettoken"
|
97
|
+
config.app_endpoint = "http://myapp.heroku.com"
|
86
98
|
end
|
87
99
|
|
88
100
|
Example with authentication:
|
89
101
|
|
90
|
-
|
102
|
+
`rhoconnect-rb` installs a `/rhoconnect/authenticate` route into your application which will receive credentials from the client. Add block which handles the credentials:
|
91
103
|
|
92
104
|
Rhoconnect.configure do |config|
|
93
105
|
config.uri = "http://myrhoconnect-server.com"
|
@@ -96,6 +108,7 @@ Rhoconnect installs a `/rhoconnect/authenticate` route into your application whi
|
|
96
108
|
User.authenticate(credentials[:login], credentials[:password])
|
97
109
|
}
|
98
110
|
end
|
111
|
+
|
99
112
|
|
100
113
|
## Meta
|
101
114
|
Created and maintained by Lucas Campbell-Rossen, Vladimir Tarasov and Lars Burgess.
|
data/lib/rhoconnect/resource.rb
CHANGED
@@ -11,14 +11,6 @@ module Rhoconnect
|
|
11
11
|
|
12
12
|
|
13
13
|
module ClassMethods
|
14
|
-
def partition(p)
|
15
|
-
@partition = p
|
16
|
-
end
|
17
|
-
|
18
|
-
def get_partition
|
19
|
-
@partition.is_a?(Proc) ? @partition.call : @partition
|
20
|
-
end
|
21
|
-
|
22
14
|
def rhoconnect_receive_create(partition, attributes)
|
23
15
|
instance = self.send(:new)
|
24
16
|
instance.send(:rhoconnect_apply_attributes, partition, attributes)
|
@@ -47,6 +39,11 @@ module Rhoconnect
|
|
47
39
|
|
48
40
|
module InstanceMethods
|
49
41
|
attr_accessor :skip_rhoconnect_callbacks
|
42
|
+
|
43
|
+
def get_partition
|
44
|
+
@partition = partition
|
45
|
+
@partition.is_a?(Proc) ? @partition.call : @partition
|
46
|
+
end
|
50
47
|
|
51
48
|
def rhoconnect_create
|
52
49
|
call_client_method(:create)
|
@@ -76,7 +73,7 @@ module Rhoconnect
|
|
76
73
|
attribs.each do |key,value|
|
77
74
|
attribs[key] = Time.parse(value.to_s).to_i.to_s if value.is_a?(Time) or value.is_a?(DateTime)
|
78
75
|
end if Rhoconnect.configuration.sync_time_as_int
|
79
|
-
attribs
|
76
|
+
attribs
|
80
77
|
end
|
81
78
|
|
82
79
|
private
|
@@ -85,7 +82,7 @@ module Rhoconnect
|
|
85
82
|
unless self.skip_rhoconnect_callbacks
|
86
83
|
attribs = self.normalized_attributes
|
87
84
|
begin
|
88
|
-
Rhoconnect::Client.new.send(action, self.class.to_s, self.
|
85
|
+
Rhoconnect::Client.new.send(action, self.class.to_s, self.get_partition, attribs)
|
89
86
|
rescue RestClient::Exception => re
|
90
87
|
warn "#{self.class.to_s}: rhoconnect_#{action} returned error: #{re.message} - #{re.http_body}"
|
91
88
|
rescue Exception => e
|
data/lib/rhoconnect/version.rb
CHANGED
data/spec/resource_spec.rb
CHANGED
@@ -7,20 +7,24 @@ describe Rhoconnect::Resource do
|
|
7
7
|
class TestModel1 < ActiveRecord::Base
|
8
8
|
include Rhoconnect::Resource
|
9
9
|
|
10
|
-
partition
|
10
|
+
def partition
|
11
|
+
:app
|
12
|
+
end
|
11
13
|
end
|
12
14
|
|
13
|
-
TestModel1.get_partition.should == :app
|
15
|
+
TestModel1.new.get_partition.should == :app
|
14
16
|
end
|
15
17
|
|
16
18
|
it "should set resource partition with lambda" do
|
17
19
|
class TestModel2 < ActiveRecord::Base
|
18
20
|
include Rhoconnect::Resource
|
19
21
|
|
20
|
-
partition
|
22
|
+
def partition
|
23
|
+
lambda{ 'helloworld' }
|
24
|
+
end
|
21
25
|
end
|
22
26
|
|
23
|
-
TestModel2.get_partition.should == 'helloworld'
|
27
|
+
TestModel2.new.get_partition.should == 'helloworld'
|
24
28
|
end
|
25
29
|
end
|
26
30
|
|
@@ -70,7 +74,9 @@ describe Rhoconnect::Resource do
|
|
70
74
|
it "should call create update delete hook" do
|
71
75
|
class TestModel7 < ActiveRecord::Base
|
72
76
|
include Rhoconnect::Resource
|
73
|
-
partition
|
77
|
+
def partition
|
78
|
+
:app
|
79
|
+
end
|
74
80
|
end
|
75
81
|
client = mock('Rhoconnect::Client')
|
76
82
|
client.stub!(:send)
|
@@ -86,7 +92,9 @@ describe Rhoconnect::Resource do
|
|
86
92
|
it "should warn on RestClient::Exception" do
|
87
93
|
class TestModel8 < ActiveRecord::Base
|
88
94
|
include Rhoconnect::Resource
|
89
|
-
partition
|
95
|
+
def partition
|
96
|
+
:app
|
97
|
+
end
|
90
98
|
end
|
91
99
|
client = mock('Rhoconnect::Client')
|
92
100
|
exception = RestClient::Exception.new(
|
@@ -105,7 +113,9 @@ describe Rhoconnect::Resource do
|
|
105
113
|
it "should warn on Exception" do
|
106
114
|
class TestModel8 < ActiveRecord::Base
|
107
115
|
include Rhoconnect::Resource
|
108
|
-
partition
|
116
|
+
def partition
|
117
|
+
:app
|
118
|
+
end
|
109
119
|
end
|
110
120
|
client = mock('Rhoconnect::Client')
|
111
121
|
client.stub!(:send).and_return { raise Exception.new("error connecting to server") }
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rhoconnect-rb
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 27
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 2
|
9
|
-
-
|
10
|
-
version: 0.2.
|
9
|
+
- 6
|
10
|
+
version: 0.2.6
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Rhomobile
|
@@ -15,10 +15,11 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2011-
|
19
|
-
default_executable:
|
18
|
+
date: 2011-08-09 00:00:00 Z
|
20
19
|
dependencies:
|
21
20
|
- !ruby/object:Gem::Dependency
|
21
|
+
name: rest-client
|
22
|
+
prerelease: false
|
22
23
|
version_requirements: &id001 !ruby/object:Gem::Requirement
|
23
24
|
none: false
|
24
25
|
requirements:
|
@@ -30,11 +31,11 @@ dependencies:
|
|
30
31
|
- 6
|
31
32
|
- 1
|
32
33
|
version: 1.6.1
|
33
|
-
requirement: *id001
|
34
|
-
prerelease: false
|
35
|
-
name: rest-client
|
36
34
|
type: :runtime
|
35
|
+
requirement: *id001
|
37
36
|
- !ruby/object:Gem::Dependency
|
37
|
+
name: json
|
38
|
+
prerelease: false
|
38
39
|
version_requirements: &id002 !ruby/object:Gem::Requirement
|
39
40
|
none: false
|
40
41
|
requirements:
|
@@ -46,10 +47,8 @@ dependencies:
|
|
46
47
|
- 4
|
47
48
|
- 6
|
48
49
|
version: 1.4.6
|
49
|
-
requirement: *id002
|
50
|
-
prerelease: false
|
51
|
-
name: json
|
52
50
|
type: :runtime
|
51
|
+
requirement: *id002
|
53
52
|
description: Rhoconnect rails plugin
|
54
53
|
email:
|
55
54
|
- support@rhomobile.com
|
@@ -79,7 +78,6 @@ files:
|
|
79
78
|
- spec/endpoints_spec.rb
|
80
79
|
- spec/resource_spec.rb
|
81
80
|
- spec/spec_helper.rb
|
82
|
-
has_rdoc: true
|
83
81
|
homepage: http://rhomobile.com
|
84
82
|
licenses: []
|
85
83
|
|
@@ -109,7 +107,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
109
107
|
requirements: []
|
110
108
|
|
111
109
|
rubyforge_project:
|
112
|
-
rubygems_version: 1.
|
110
|
+
rubygems_version: 1.8.6
|
113
111
|
signing_key:
|
114
112
|
specification_version: 3
|
115
113
|
summary: Rhoconnect rails plugin
|