rhoconnect-rb 0.2.1 → 0.2.2

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 CHANGED
@@ -1,9 +1,9 @@
1
1
  rhoconnect-rb
2
2
  ===
3
3
 
4
- A ruby client library for the [Rhoconnect](http://rhomobile.com/products/rhosync) App Integration Server.
4
+ A ruby library for the [Rhoconnect](http://rhomobile.com/products/rhosync) App Integration Server.
5
5
 
6
- Using rhoconnect-rb, your application's model data will transparently synchronize with a mobile application built using the [Rhodes framework](http://rhomobile.com/products/rhodes), or any of the available [Rhoconnect clients](http://rhomobile.com/products/rhosync/). This client includes built-in support for [ActiveRecord](http://ar.rubyonrails.org/) and [DataMapper](http://datamapper.org/) models.
6
+ Using rhoconnect-rb, your application's model data will transparently synchronize with a mobile application built on the [Rhodes framework](http://rhomobile.com/products/rhodes), or any of the available [Rhoconnect clients](http://rhomobile.com/products/rhosync/). This client includes built-in support for [ActiveRecord](http://ar.rubyonrails.org/) and [DataMapper](http://datamapper.org/) models.
7
7
 
8
8
  ## Getting started
9
9
 
@@ -26,13 +26,15 @@ Or, if you are using DataMapper:
26
26
  include DataMapper::Resource
27
27
  include Rhoconnect::Resource
28
28
  end
29
+
30
+ ### Partitioning Datasets
29
31
 
30
- 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 application. It is typically an attribute on the model or related model. `rhoconnect-rb` supports two types of partitions:
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:
31
33
 
32
- * :app - No unique key will be used, a shared dataset is used for all users.
34
+ * :app - No unique key will be used, a shared dataset is synchronzied for all users.
33
35
  * lambda { some lambda } - Execute a lambda which returns the unique key string.
34
36
 
35
- For example, the `Product` model above might have a `belongs_to :user` relationship. The partition identifying the username would be declared as:
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:
36
38
 
37
39
  class Product < ActiveRecord::Base
38
40
  include Rhoconnect::Resource
@@ -42,9 +44,60 @@ For example, the `Product` model above might have a `belongs_to :user` relations
42
44
  partition lambda { self.user.username }
43
45
  end
44
46
 
47
+ 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
+
49
+ partition lambda { :app }
50
+
45
51
  For more information about Rhoconnect partitions, please refer to the [Rhoconnect docs](http://docs.rhomobile.com/rhosync/source-adapters#data-partitioning).
46
52
 
53
+ ### Querying Datasets
54
+
55
+ `rhoconnect-rb` installs a `/rhoconnect/query` route in your application which the Rhoconnect instance invokes to query the dataset for the dataset you want to synchronize. This route is mapped to a `rhoconnect_query` method in your model. This method should return a collection of objects:
56
+
57
+ class Product < ActiveRecord::Base
58
+ include Rhoconnect::Resource
59
+
60
+ belongs_to :user
61
+
62
+ partition lambda { self.user.username }
63
+
64
+ def self.rhoconnect_query(partition)
65
+ Product.where(:user_id => partition)
66
+ end
67
+ end
68
+
69
+ In this example, `self.rhoconnect_query` returns a list of products where the partition string (provided by the rhoconnect instance) matches the `user_id` field in the products table.
70
+
71
+ ### Configuration and Authentication
72
+
73
+ Configure Rhoconnect in an initializer like `config/initializers/rhoconnect.rb` (for Rails), or directly in your application (i.e. Sinatra):
74
+
75
+ # Setup the Rhoconnect uri and api token.
76
+ # Use rhoconnect:get_token to get the token value.
77
+
78
+ config.uri = "http://myrhoconnect.com"
79
+ config.token = "secrettoken"
80
+
81
+ Example:
82
+
83
+ Rhoconnect.configure do |config|
84
+ config.uri = "http://myrhoconnect-server.com"
85
+ config.token = "secrettoken"
86
+ end
87
+
88
+ Example with authentication:
89
+
90
+ Rhoconnect installs a `/rhoconnect/authenticate` route into your application which will receive credentials from the client. Add block which handles the credentials:
91
+
92
+ Rhoconnect.configure do |config|
93
+ config.uri = "http://myrhoconnect-server.com"
94
+ config.token = "secrettoken"
95
+ config.authenticate = lambda { |credentials|
96
+ User.authenticate(credentials[:login], credentials[:password])
97
+ }
98
+ end
99
+
47
100
  ## Meta
48
- Created and maintained by Vladimir Tarasov and Lars Burgess.
101
+ Created and maintained by Lucas Campbell-Rossen, Vladimir Tarasov and Lars Burgess.
49
102
 
50
103
  Released under the [MIT License](http://www.opensource.org/licenses/mit-license.php).
@@ -4,8 +4,12 @@ module Rhoconnect
4
4
  class Client
5
5
  attr_accessor :uri, :token
6
6
 
7
- def self.set_app_endpoint(url)
8
- RestClient::Request.execute(:method => :post, :url => url, :timeout => 2000)
7
+ def self.set_app_endpoint(args)
8
+ RestClient::Request.execute(
9
+ :method => :post, :url => args[:url],
10
+ :payload => args[:payload], :timeout => 2000,
11
+ :headers => args[:headers]
12
+ )
9
13
  end
10
14
 
11
15
  # allow configuration, uri or environment variable initialization
@@ -34,7 +34,7 @@ module Rhoconnect
34
34
  yield(configuration)
35
35
  # make a call to rhoconnect instance to set app url
36
36
  endpoint_url = ENV['APP_ENDPOINT'] || self.configuration.app_endpoint
37
- uri = ENV['URI'] || self.configuration.uri
37
+ uri = ENV['RHOCONNECT_URL'] || self.configuration.uri
38
38
  if uri
39
39
  uri = URI.parse(uri)
40
40
  token = uri.user
@@ -42,7 +42,10 @@ module Rhoconnect
42
42
  uri = uri.to_s
43
43
  end
44
44
  token ||= ENV['token'] || self.configuration.token
45
- Rhoconnect::Client.set_app_endpoint(uri + "/api/source/save_adapter?attributes[adapter_url]=#{endpoint_url}&api_token=#{token}") if endpoint_url && uri && token
45
+ Rhoconnect::Client.set_app_endpoint(:url => uri + "/api/source/save_adapter",
46
+ :payload => {:attributes => {:adapter_url => endpoint_url}, :api_token => token}.to_json,
47
+ :headers => {:content_type => 'application/json'}
48
+ ) if endpoint_url && uri && token
46
49
  end
47
50
  end
48
51
 
@@ -1,3 +1,3 @@
1
1
  module Rhoconnect
2
- VERSION = "0.2.1"
2
+ VERSION = "0.2.2"
3
3
  end
data/spec/client_spec.rb CHANGED
@@ -4,34 +4,34 @@ describe Rhoconnect::Client do
4
4
 
5
5
  context "on initialize" do
6
6
  it "should initialize with Rhoconnect_URL environment var" do
7
- ENV['RHOCONNECT_URL'] = "http://token@test.Rhoconnect.com"
7
+ ENV['RHOCONNECT_URL'] = "http://token@test.rhoconnect.com"
8
8
  c = Rhoconnect::Client.new
9
9
  c.token.should == 'token'
10
- c.uri.should == 'http://test.Rhoconnect.com'
10
+ c.uri.should == 'http://test.rhoconnect.com'
11
11
  ENV.delete('Rhoconnect_URL')
12
12
  end
13
13
 
14
14
  it "should initialize with :uri parameter" do
15
- c = Rhoconnect::Client.new(:uri => "http://token@test.Rhoconnect.com")
15
+ c = Rhoconnect::Client.new(:uri => "http://token@test.rhoconnect.com")
16
16
  c.token.should == 'token'
17
- c.uri.should == 'http://test.Rhoconnect.com'
17
+ c.uri.should == 'http://test.rhoconnect.com'
18
18
  end
19
19
 
20
20
  it "should initialize with :token parameter" do
21
- c = Rhoconnect::Client.new(:uri => "http://test.Rhoconnect.com", :token => "token")
21
+ c = Rhoconnect::Client.new(:uri => "http://test.rhoconnect.com", :token => "token")
22
22
  c.token.should == 'token'
23
- c.uri.should == 'http://test.Rhoconnect.com'
23
+ c.uri.should == 'http://test.rhoconnect.com'
24
24
  end
25
25
 
26
26
  it "should initialize with configure block" do
27
27
  Rhoconnect.configure do |config|
28
- config.uri = "http://test.Rhoconnect.com"
28
+ config.uri = "http://test.rhoconnect.com"
29
29
  config.token = "token"
30
30
  end
31
31
  begin
32
32
  c = Rhoconnect::Client.new
33
33
  c.token.should == 'token'
34
- c.uri.should == 'http://test.Rhoconnect.com'
34
+ c.uri.should == 'http://test.rhoconnect.com'
35
35
  ensure
36
36
  Rhoconnect.configure do |config|
37
37
  config.uri = nil
@@ -47,18 +47,18 @@ describe Rhoconnect::Client do
47
47
 
48
48
  it "should raise ArugmentError if token is missing" do
49
49
  lambda {
50
- Rhoconnect::Client.new(:uri => "http://test.Rhoconnect.com")
50
+ Rhoconnect::Client.new(:uri => "http://test.rhoconnect.com")
51
51
  }.should raise_error(ArgumentError, "Please provide a :token or set it in uri")
52
52
  end
53
53
  end
54
54
 
55
55
  context "on create update destroy" do
56
56
  before(:each) do
57
- @client = Rhoconnect::Client.new(:uri => "http://token@test.Rhoconnect.com")
57
+ @client = Rhoconnect::Client.new(:uri => "http://token@test.rhoconnect.com")
58
58
  end
59
59
 
60
60
  it "should create an object" do
61
- stub_request(:post, "http://test.Rhoconnect.com/api/source/push_objects").with(
61
+ stub_request(:post, "http://test.rhoconnect.com/api/source/push_objects").with(
62
62
  :headers => {"Content-Type" => "application/json"}
63
63
  ).to_return(:status => 200, :body => "done")
64
64
  resp = @client.create("Person", "user1",
@@ -72,7 +72,7 @@ describe Rhoconnect::Client do
72
72
  end
73
73
 
74
74
  it "should update an object" do
75
- stub_request(:post, "http://test.Rhoconnect.com/api/source/push_objects").with(
75
+ stub_request(:post, "http://test.rhoconnect.com/api/source/push_objects").with(
76
76
  :headers => {"Content-Type" => "application/json"}
77
77
  ).to_return(:status => 200, :body => "done")
78
78
  resp = @client.update("Person", "user1",
@@ -86,7 +86,7 @@ describe Rhoconnect::Client do
86
86
  end
87
87
 
88
88
  it "should destroy an object" do
89
- stub_request(:post, "http://test.Rhoconnect.com/api/source/push_deletes").with(
89
+ stub_request(:post, "http://test.rhoconnect.com/api/source/push_deletes").with(
90
90
  :headers => {"Content-Type" => "application/json"}
91
91
  ).to_return(:status => 200, :body => "done")
92
92
  resp = @client.destroy("Person", "user1",
@@ -102,11 +102,11 @@ describe Rhoconnect::Client do
102
102
 
103
103
  context "on set callbacks" do
104
104
  before(:each) do
105
- @client = Rhoconnect::Client.new(:uri => "http://token@test.Rhoconnect.com")
105
+ @client = Rhoconnect::Client.new(:uri => "http://token@test.rhoconnect.com")
106
106
  end
107
107
 
108
108
  it "should set auth callback" do
109
- stub_request(:post, "http://test.Rhoconnect.com/api/set_auth_callback").with(
109
+ stub_request(:post, "http://test.rhoconnect.com/api/set_auth_callback").with(
110
110
  :headers => {"Content-Type" => "application/json"}
111
111
  ).to_return(:status => 200, :body => "done")
112
112
  resp = @client.set_auth_callback("http://example.com/callback")
@@ -115,7 +115,7 @@ describe Rhoconnect::Client do
115
115
  end
116
116
 
117
117
  it "should set query callback" do
118
- stub_request(:post, "http://test.Rhoconnect.com/api/set_query_callback").with(
118
+ stub_request(:post, "http://test.rhoconnect.com/api/set_query_callback").with(
119
119
  :headers => {"Content-Type" => "application/json"}
120
120
  ).to_return(:status => 200, :body => "done")
121
121
  resp = @client.set_query_callback("Person", "http://example.com/callback")
@@ -60,7 +60,7 @@ describe Rhoconnect::EndpointHelpers do
60
60
 
61
61
  it "should return true if no authenticate block exists" do
62
62
  Rhoconnect.configure do |config|
63
- config.uri = "http://test.Rhoconnect.com"
63
+ config.uri = "http://test.rhoconnect.com"
64
64
  config.token = "token"
65
65
  end
66
66
  Rhoconnect.configuration.authenticate.should be_nil
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: 21
4
+ hash: 19
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 2
9
- - 1
10
- version: 0.2.1
9
+ - 2
10
+ version: 0.2.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - Rhomobile
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-06-27 00:00:00 Z
18
+ date: 2011-06-28 00:00:00 Z
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
21
  type: :runtime