rhoconnect-rb 0.1.0 → 0.2.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.
data/README.md CHANGED
@@ -1,9 +1,9 @@
1
1
  rhoconnect-rb
2
2
  ===
3
3
 
4
- A ruby client library for the [RhoSync](http://rhomobile.com/products/rhosync) App Integration Server.
4
+ A ruby client 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 [RhoSync 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 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.
7
7
 
8
8
  ## Getting started
9
9
 
@@ -11,23 +11,23 @@ Load the `rhoconnect-rb` library:
11
11
 
12
12
  require 'rhoconnect-rb'
13
13
 
14
- Note, if you are using datamapper, install the `dm-serializer` library and require it in your application. `rhoconnect-rb` depends on this utility to interact with RhoSync applications using JSON.
14
+ Note, if you are using datamapper, install the `dm-serializer` library and require it in your application. `rhoconnect-rb` depends on this utility to interact with Rhoconnect applications using JSON.
15
15
 
16
16
  ## Usage
17
- Now include Rhosync::Resource in a model that you want to synchronize with your mobile application:
17
+ Now include Rhoconnect::Resource in a model that you want to synchronize with your mobile application:
18
18
 
19
19
  class Product < ActiveRecord::Base
20
- include Rhosync::Resource
20
+ include Rhoconnect::Resource
21
21
  end
22
22
 
23
23
  Or, if you are using DataMapper:
24
24
 
25
25
  class Product
26
26
  include DataMapper::Resource
27
- include Rhosync::Resource
27
+ include Rhoconnect::Resource
28
28
  end
29
29
 
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 RhoSync application. It is typically an attribute on the model or related model. `rhoconnect-rb` supports two types of partitions:
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:
31
31
 
32
32
  * :app - No unique key will be used, a shared dataset is used for all users.
33
33
  * lambda { some lambda } - Execute a lambda which returns the unique key string.
@@ -35,14 +35,14 @@ Next, your models will need to declare a partition key for `rhoconnect-rb`. Thi
35
35
  For example, the `Product` model above might have a `belongs_to :user` relationship. The partition identifying the username would be declared as:
36
36
 
37
37
  class Product < ActiveRecord::Base
38
- include Rhosync::Resource
38
+ include Rhoconnect::Resource
39
39
 
40
40
  belongs_to :user
41
41
 
42
42
  partition lambda { self.user.username }
43
43
  end
44
44
 
45
- For more information about RhoSync partitions, please refer to the [RhoSync docs](http://docs.rhomobile.com/rhosync/source-adapters#data-partitioning).
45
+ For more information about Rhoconnect partitions, please refer to the [Rhoconnect docs](http://docs.rhomobile.com/rhosync/source-adapters#data-partitioning).
46
46
 
47
47
  ## Meta
48
48
  Created and maintained by Vladimir Tarasov and Lars Burgess.
data/config/routes.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  Rails.application.routes.draw do
2
- match '/rhosync/authenticate' => Rhosync::Authenticate
3
- match '/rhosync/query' => Rhosync::Query
4
- match '/rhosync/create' => Rhosync::Create
5
- match '/rhosync/update' => Rhosync::Update
6
- match '/rhosync/delete' => Rhosync::Delete
2
+ match '/rhoconnect/authenticate' => Rhoconnect::Authenticate
3
+ match '/rhoconnect/query' => Rhoconnect::Query
4
+ match '/rhoconnect/create' => Rhoconnect::Create
5
+ match '/rhoconnect/update' => Rhoconnect::Update
6
+ match '/rhoconnect/delete' => Rhoconnect::Delete
7
7
  end
@@ -1,20 +1,25 @@
1
1
  require 'uri'
2
2
 
3
- module Rhosync
3
+ module Rhoconnect
4
4
  class Client
5
5
  attr_accessor :uri, :token
6
+
7
+ def self.set_app_endpoint(url)
8
+ RestClient::Request.execute(:method => :post, :url => url, :timeout => 2000)
9
+ end
6
10
 
7
11
  # allow configuration, uri or environment variable initialization
8
12
  def initialize(params = {})
9
- uri = params[:uri] || Rhosync.configuration.uri || ENV['RHOSYNC_URL']
10
- raise ArgumentError.new("Please provide a :uri or set RHOSYNC_URL") unless uri
11
- uri = URI.parse(uri)
13
+ uri = params[:uri] || Rhoconnect.configuration.uri || ENV['RHOCONNECT_URL']
14
+ raise ArgumentError.new("Please provide a :uri or set RHOCONNECT_URL") unless uri
15
+ @uri = URI.parse(uri)
16
+
17
+ @token = params[:token] || Rhoconnect.configuration.token || @uri.user
18
+ @uri.user = nil; @uri = @uri.to_s
12
19
 
13
- @token = params[:token] || Rhosync.configuration.token || uri.user
14
- uri.user = nil; @uri = uri.to_s
15
20
  raise ArgumentError.new("Please provide a :token or set it in uri") unless @token
16
21
 
17
- RestClient.proxy = ENV['HTTP_PROXY'] || ENV['http_proxy']
22
+ RestClient.proxy = ENV['HTTP_PROXY'] || ENV['http_proxy'] || Rhoconnect.configuration.http_proxy
18
23
  end
19
24
 
20
25
  def create(source_name, partition, obj = {})
@@ -52,7 +57,7 @@ module Rhosync
52
57
  def send_objects(action, source_name, partition, obj = {}) # :nodoc:
53
58
  validate_args(source_name, partition, obj)
54
59
 
55
- process(:post, "/api/#{action}",
60
+ process(:post, "/api/source/#{action}",
56
61
  {
57
62
  :source_id => source_name,
58
63
  :user_id => partition,
@@ -78,7 +83,7 @@ module Rhosync
78
83
 
79
84
  def api_headers # :nodoc:
80
85
  {
81
- 'User-Agent' => Rhosync::VERSION,
86
+ 'User-Agent' => Rhoconnect::VERSION,
82
87
  'X-Ruby-Version' => RUBY_VERSION,
83
88
  'X-Ruby-Platform' => RUBY_PLATFORM
84
89
  }
@@ -0,0 +1,49 @@
1
+ module Rhoconnect
2
+ class Configuration
3
+ attr_accessor :uri, :token, :authenticate, :sync_time_as_int, :app_endpoint, :http_proxy
4
+
5
+ def initialize
6
+ @sync_time_as_int = true
7
+ end
8
+
9
+ end
10
+
11
+ class << self
12
+ attr_accessor :configuration
13
+ end
14
+
15
+ # Configure Rhoconnect in an initializer:
16
+ # like config/initializers/rhoconnect.rb
17
+ #
18
+ # Setup the Rhoconnect uri and api token.
19
+ # Use rhoconnect:get_token to get the token value.
20
+ #
21
+ # config.uri = "http://myrhoconnect.com"
22
+ # config.token = "secrettoken"
23
+ # config.authenticate = lambda { |credentials|
24
+ # User.authenticate(credentials)
25
+ # }
26
+ #
27
+ # @example
28
+ # Rhoconnect.configure do |config|
29
+ # config.uri = "http://myrhoconnect.com"
30
+ # config.token = "secrettoken"
31
+ # end
32
+ def self.configure
33
+ self.configuration = Configuration.new
34
+ yield(configuration)
35
+ # make a call to rhoconnect instance to set app url
36
+ endpoint_url = self.configuration.app_endpoint || ENV['APP_ENDPOINT']
37
+ uri = self.configuration.uri || ENV['URI']
38
+ if uri
39
+ uri = URI.parse(uri)
40
+ token = uri.user
41
+ uri.user = nil
42
+ uri = uri.to_s
43
+ end
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
46
+ end
47
+ end
48
+
49
+ Rhoconnect.configure { }
@@ -1,20 +1,20 @@
1
1
  require 'json'
2
2
 
3
- module Rhosync
3
+ module Rhoconnect
4
4
  class EndpointHelpers
5
5
  def self.authenticate(content_type, body)
6
6
  code, params = 200, parse_params(content_type, body)
7
- if Rhosync.configuration.authenticate
8
- code = 401 unless Rhosync.configuration.authenticate.call(params)
7
+ if Rhoconnect.configuration.authenticate
8
+ code = 401 unless Rhoconnect.configuration.authenticate.call(params)
9
9
  end
10
10
  [code, {'Content-Type' => 'text/plain'}, [""]]
11
11
  end
12
12
 
13
13
  def self.query(content_type, body)
14
14
  params = parse_params(content_type, body)
15
- action, c_type, result, records = :rhosync_query, 'application/json', {}, []
16
- # Call resource rhosync_query class method
17
- code, error = get_rhosync_resource(params['resource'], action) do |klass|
15
+ action, c_type, result, records = :rhoconnect_query, 'application/json', {}, []
16
+ # Call resource rhoconnect_query class method
17
+ code, error = get_rhoconnect_resource(params['resource'], action) do |klass|
18
18
  records = klass.send(action, params['partition'])
19
19
  end
20
20
  if code == 200
@@ -35,8 +35,8 @@ module Rhosync
35
35
  def self.on_cud(action, content_type, body)
36
36
  params = parse_params(content_type, body)
37
37
  object_id = ""
38
- code, error = get_rhosync_resource(params['resource'], action) do |klass|
39
- object_id = klass.send("rhosync_receive_#{action}".to_sym,
38
+ code, error = get_rhoconnect_resource(params['resource'], action) do |klass|
39
+ object_id = klass.send("rhoconnect_receive_#{action}".to_sym,
40
40
  params['partition'], params['attributes'])
41
41
  object_id = object_id.to_s if object_id
42
42
  end
@@ -57,7 +57,7 @@ module Rhosync
57
57
 
58
58
  private
59
59
 
60
- def self.get_rhosync_resource(resource_name, action)
60
+ def self.get_rhoconnect_resource(resource_name, action)
61
61
  code, error = 200, nil
62
62
  begin
63
63
  klass = Kernel.const_get(resource_name)
@@ -66,7 +66,7 @@ module Rhosync
66
66
  error = "error on method `#{action}` for #{resource_name}: #{ne.message}"
67
67
  code = 404
68
68
  rescue NameError
69
- error = "Missing Rhosync::Resource #{resource_name}"
69
+ error = "Missing Rhoconnect::Resource #{resource_name}"
70
70
  code = 404
71
71
  # TODO: catch HaltException and Exception here, built-in source adapter will handle them
72
72
  rescue Exception => e
@@ -92,11 +92,11 @@ if defined? Rails
92
92
  class Engine < Rails::Engine; end
93
93
  #end
94
94
 
95
- module Rhosync
95
+ module Rhoconnect
96
96
  class BaseEndpoint
97
97
  def self.call(env)
98
98
  req = Rack::Request.new(env)
99
- Rhosync::EndpointHelpers.send(self.to_s.downcase.split("::")[1].to_sym, req.content_type, req.body.read)
99
+ Rhoconnect::EndpointHelpers.send(self.to_s.downcase.split("::")[1].to_sym, req.content_type, req.body.read)
100
100
  end
101
101
  end
102
102
 
@@ -135,33 +135,33 @@ if defined? Sinatra
135
135
  # require 'rhoconnect-rb'
136
136
  #
137
137
  # class Myapp < Sinatra::Base
138
- # register Sinatra::RhosyncEndpoints
138
+ # register Sinatra::RhoconnectEndpoints
139
139
  # get '/' do
140
140
  # 'hello world'
141
141
  # end
142
142
  # end
143
143
  module Sinatra
144
- module RhosyncHelpers
144
+ module RhoconnectHelpers
145
145
  def call_helper(method,*args)
146
- code, c_type, body = Rhosync::EndpointHelpers.send(method,*args)
146
+ code, c_type, body = Rhoconnect::EndpointHelpers.send(method,*args)
147
147
  content_type c_type['Content-Type']
148
148
  status code
149
149
  body[0]
150
150
  end
151
151
  end
152
152
 
153
- module RhosyncEndpoints
153
+ module RhoconnectEndpoints
154
154
  def self.registered(app)
155
155
  # install our endpoint helpers
156
- app.send(:include, RhosyncHelpers)
156
+ app.send(:include, RhoconnectHelpers)
157
157
 
158
158
  [:authenticate,:query,:create,:update,:delete].each do |endpoint|
159
- app.post "/rhosync/#{endpoint}" do
159
+ app.post "/rhoconnect/#{endpoint}" do
160
160
  call_helper(endpoint, request.env['CONTENT_TYPE'], request.body.read)
161
161
  end
162
162
  end
163
163
  end
164
164
  end
165
- register RhosyncEndpoints
165
+ register RhoconnectEndpoints
166
166
  end
167
167
  end
@@ -1,4 +1,4 @@
1
- module Rhosync
1
+ module Rhoconnect
2
2
  module Resource
3
3
 
4
4
  def self.included(model)
@@ -19,77 +19,77 @@ module Rhosync
19
19
  @partition.is_a?(Proc) ? @partition.call : @partition
20
20
  end
21
21
 
22
- def rhosync_receive_create(partition, attributes)
22
+ def rhoconnect_receive_create(partition, attributes)
23
23
  instance = self.send(:new)
24
- instance.send(:rhosync_apply_attributes, partition, attributes)
25
- instance.skip_rhosync_callbacks = true
24
+ instance.send(:rhoconnect_apply_attributes, partition, attributes)
25
+ instance.skip_rhoconnect_callbacks = true
26
26
  instance.save
27
27
  instance.id #=> return object id
28
28
  end
29
29
 
30
- def rhosync_receive_update(partition, attributes)
30
+ def rhoconnect_receive_update(partition, attributes)
31
31
  object_id = attributes.delete('id')
32
32
  instance = self.send(is_datamapper? ? :get : :find, object_id)
33
- instance.send(:rhosync_apply_attributes, partition, attributes)
34
- instance.skip_rhosync_callbacks = true
33
+ instance.send(:rhoconnect_apply_attributes, partition, attributes)
34
+ instance.skip_rhoconnect_callbacks = true
35
35
  instance.save
36
36
  object_id
37
37
  end
38
38
 
39
- def rhosync_receive_delete(partition, attributes)
39
+ def rhoconnect_receive_delete(partition, attributes)
40
40
  object_id = attributes['id']
41
41
  instance = self.send(is_datamapper? ? :get : :find, object_id)
42
- instance.skip_rhosync_callbacks = true
42
+ instance.skip_rhoconnect_callbacks = true
43
43
  instance.destroy
44
44
  object_id
45
45
  end
46
46
  end
47
47
 
48
48
  module InstanceMethods
49
- attr_accessor :skip_rhosync_callbacks
49
+ attr_accessor :skip_rhoconnect_callbacks
50
50
 
51
- def rhosync_create
51
+ def rhoconnect_create
52
52
  call_client_method(:create)
53
53
  end
54
54
 
55
- def rhosync_destroy
55
+ def rhoconnect_destroy
56
56
  call_client_method(:destroy)
57
57
  end
58
58
 
59
- def rhosync_update
59
+ def rhoconnect_update
60
60
  call_client_method(:update)
61
61
  end
62
62
 
63
- def rhosync_query(partition)
63
+ def rhoconnect_query(partition)
64
64
  #return all objects for this partition
65
65
  end
66
66
 
67
67
  # By default we ignore partition
68
68
  # TODO: Document - this is user-facing function
69
- def rhosync_apply_attributes(partition, attributes)
69
+ def rhoconnect_apply_attributes(partition, attributes)
70
70
  self.attributes = attributes
71
71
  end
72
72
 
73
- # Return Rhosync-friendly attributes list
73
+ # Return Rhoconnect-friendly attributes list
74
74
  def normalized_attributes
75
75
  attribs = self.attributes.dup
76
76
  attribs.each do |key,value|
77
77
  attribs[key] = Time.parse(value.to_s).to_i.to_s if value.is_a?(Time) or value.is_a?(DateTime)
78
- end if Rhosync.configuration.sync_time_as_int
78
+ end if Rhoconnect.configuration.sync_time_as_int
79
79
  attribs
80
80
  end
81
81
 
82
82
  private
83
83
 
84
84
  def call_client_method(action)
85
- unless self.skip_rhosync_callbacks
85
+ unless self.skip_rhoconnect_callbacks
86
86
  attribs = self.normalized_attributes
87
87
  begin
88
- Rhosync::Client.new.send(action, self.class.to_s, self.class.get_partition, attribs)
88
+ Rhoconnect::Client.new.send(action, self.class.to_s, self.class.get_partition, attribs)
89
89
  rescue RestClient::Exception => re
90
- warn "#{self.class.to_s}: rhosync_#{action} returned error: #{re.message} - #{re.http_body}"
90
+ warn "#{self.class.to_s}: rhoconnect_#{action} returned error: #{re.message} - #{re.http_body}"
91
91
  rescue Exception => e
92
- warn "#{self.class.to_s}: rhosync_#{action} returned unexpected error: #{e.message}"
92
+ warn "#{self.class.to_s}: rhoconnect_#{action} returned unexpected error: #{e.message}"
93
93
  end
94
94
  end
95
95
  end
@@ -111,17 +111,17 @@ module Rhosync
111
111
  if is_datamapper?
112
112
  # test for dm-serializer
113
113
  if not is_defined?(DataMapper::Serialize)
114
- raise "Rhosync::Resource requires dm-serializer to work with DataMapper. Install with `gem install dm-serializer` and add to your application."
114
+ raise "Rhoconnect::Resource requires dm-serializer to work with DataMapper. Install with `gem install dm-serializer` and add to your application."
115
115
  end
116
- after :create, :rhosync_create
117
- after :destroy, :rhosync_destroy
118
- after :update, :rhosync_update
116
+ after :create, :rhoconnect_create
117
+ after :destroy, :rhoconnect_destroy
118
+ after :update, :rhoconnect_update
119
119
  elsif is_activerecord?
120
- after_create :rhosync_create
121
- after_destroy :rhosync_destroy
122
- after_update :rhosync_update
120
+ after_create :rhoconnect_create
121
+ after_destroy :rhoconnect_destroy
122
+ after_update :rhoconnect_update
123
123
  else
124
- raise "Rhosync::Resource only supports ActiveRecord or DataMapper at this time."
124
+ raise "Rhoconnect::Resource only supports ActiveRecord or DataMapper at this time."
125
125
  end
126
126
  end
127
127
 
@@ -0,0 +1,3 @@
1
+ module Rhoconnect
2
+ VERSION = "0.2.0"
3
+ end
data/lib/rhoconnect-rb.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  require 'json'
2
2
  require 'rest_client'
3
- require 'rhosync/version'
4
- require 'rhosync/configuration'
5
- require 'rhosync/client'
6
- require 'rhosync/resource'
7
- require 'rhosync/endpoints'
3
+ require 'rhoconnect/version'
4
+ require 'rhoconnect/configuration'
5
+ require 'rhoconnect/client'
6
+ require 'rhoconnect/resource'
7
+ require 'rhoconnect/endpoints'
@@ -1,17 +1,17 @@
1
1
  # -*- encoding: utf-8 -*-
2
2
  $:.push File.expand_path("../lib", __FILE__)
3
- require 'rhosync/version'
3
+ require 'rhoconnect/version'
4
4
 
5
5
  Gem::Specification.new do |s|
6
6
  s.name = "rhoconnect-rb"
7
- s.version = Rhosync::VERSION
7
+ s.version = Rhoconnect::VERSION
8
8
  s.platform = Gem::Platform::RUBY
9
9
  s.authors = ["Rhomobile"]
10
10
  s.date = Time.now.strftime('%Y-%m-%d')
11
11
  s.email = ["support@rhomobile.com"]
12
12
  s.homepage = %q{http://rhomobile.com}
13
- s.summary = %q{RhoSync rails plugin}
14
- s.description = %q{RhoSync rails plugin}
13
+ s.summary = %q{Rhoconnect rails plugin}
14
+ s.description = %q{Rhoconnect rails plugin}
15
15
 
16
16
  s.rubyforge_project = nil
17
17
  s.add_dependency('rest-client', '~>1.6.1')
data/spec/client_spec.rb CHANGED
@@ -1,39 +1,39 @@
1
1
  require File.join(File.dirname(__FILE__), 'spec_helper')
2
2
 
3
- describe Rhosync::Client do
3
+ describe Rhoconnect::Client do
4
4
 
5
5
  context "on initialize" do
6
- it "should initialize with RHOSYNC_URL environment var" do
7
- ENV['RHOSYNC_URL'] = "http://token@test.rhosync.com"
8
- c = Rhosync::Client.new
6
+ it "should initialize with Rhoconnect_URL environment var" do
7
+ ENV['RHOCONNECT_URL'] = "http://token@test.Rhoconnect.com"
8
+ c = Rhoconnect::Client.new
9
9
  c.token.should == 'token'
10
- c.uri.should == 'http://test.rhosync.com'
11
- ENV.delete('RHOSYNC_URL')
10
+ c.uri.should == 'http://test.Rhoconnect.com'
11
+ ENV.delete('Rhoconnect_URL')
12
12
  end
13
13
 
14
14
  it "should initialize with :uri parameter" do
15
- c = Rhosync::Client.new(:uri => "http://token@test.rhosync.com")
15
+ c = Rhoconnect::Client.new(:uri => "http://token@test.Rhoconnect.com")
16
16
  c.token.should == 'token'
17
- c.uri.should == 'http://test.rhosync.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 = Rhosync::Client.new(:uri => "http://test.rhosync.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.rhosync.com'
23
+ c.uri.should == 'http://test.Rhoconnect.com'
24
24
  end
25
25
 
26
26
  it "should initialize with configure block" do
27
- Rhosync.configure do |config|
28
- config.uri = "http://test.rhosync.com"
27
+ Rhoconnect.configure do |config|
28
+ config.uri = "http://test.Rhoconnect.com"
29
29
  config.token = "token"
30
30
  end
31
31
  begin
32
- c = Rhosync::Client.new
32
+ c = Rhoconnect::Client.new
33
33
  c.token.should == 'token'
34
- c.uri.should == 'http://test.rhosync.com'
34
+ c.uri.should == 'http://test.Rhoconnect.com'
35
35
  ensure
36
- Rhosync.configure do |config|
36
+ Rhoconnect.configure do |config|
37
37
  config.uri = nil
38
38
  config.token = nil
39
39
  end
@@ -41,23 +41,24 @@ describe Rhosync::Client do
41
41
  end
42
42
 
43
43
  it "should raise ArgumentError if uri is missing" do
44
- lambda { Rhosync::Client.new }.should raise_error(ArgumentError, "Please provide a :uri or set RHOSYNC_URL")
44
+ ENV['RHOCONNECT_URL'] = nil
45
+ lambda { Rhoconnect::Client.new }.should raise_error(ArgumentError, "Please provide a :uri or set RHOCONNECT_URL")
45
46
  end
46
47
 
47
48
  it "should raise ArugmentError if token is missing" do
48
49
  lambda {
49
- Rhosync::Client.new(:uri => "http://test.rhosync.com")
50
+ Rhoconnect::Client.new(:uri => "http://test.Rhoconnect.com")
50
51
  }.should raise_error(ArgumentError, "Please provide a :token or set it in uri")
51
52
  end
52
53
  end
53
54
 
54
55
  context "on create update destroy" do
55
56
  before(:each) do
56
- @client = Rhosync::Client.new(:uri => "http://token@test.rhosync.com")
57
+ @client = Rhoconnect::Client.new(:uri => "http://token@test.Rhoconnect.com")
57
58
  end
58
59
 
59
60
  it "should create an object" do
60
- stub_request(:post, "http://test.rhosync.com/api/push_objects").with(
61
+ stub_request(:post, "http://test.Rhoconnect.com/api/source/push_objects").with(
61
62
  :headers => {"Content-Type" => "application/json"}
62
63
  ).to_return(:status => 200, :body => "done")
63
64
  resp = @client.create("Person", "user1",
@@ -71,7 +72,7 @@ describe Rhosync::Client do
71
72
  end
72
73
 
73
74
  it "should update an object" do
74
- stub_request(:post, "http://test.rhosync.com/api/push_objects").with(
75
+ stub_request(:post, "http://test.Rhoconnect.com/api/source/push_objects").with(
75
76
  :headers => {"Content-Type" => "application/json"}
76
77
  ).to_return(:status => 200, :body => "done")
77
78
  resp = @client.update("Person", "user1",
@@ -85,7 +86,7 @@ describe Rhosync::Client do
85
86
  end
86
87
 
87
88
  it "should destroy an object" do
88
- stub_request(:post, "http://test.rhosync.com/api/push_deletes").with(
89
+ stub_request(:post, "http://test.Rhoconnect.com/api/source/push_deletes").with(
89
90
  :headers => {"Content-Type" => "application/json"}
90
91
  ).to_return(:status => 200, :body => "done")
91
92
  resp = @client.destroy("Person", "user1",
@@ -101,11 +102,11 @@ describe Rhosync::Client do
101
102
 
102
103
  context "on set callbacks" do
103
104
  before(:each) do
104
- @client = Rhosync::Client.new(:uri => "http://token@test.rhosync.com")
105
+ @client = Rhoconnect::Client.new(:uri => "http://token@test.Rhoconnect.com")
105
106
  end
106
107
 
107
108
  it "should set auth callback" do
108
- stub_request(:post, "http://test.rhosync.com/api/set_auth_callback").with(
109
+ stub_request(:post, "http://test.Rhoconnect.com/api/set_auth_callback").with(
109
110
  :headers => {"Content-Type" => "application/json"}
110
111
  ).to_return(:status => 200, :body => "done")
111
112
  resp = @client.set_auth_callback("http://example.com/callback")
@@ -114,7 +115,7 @@ describe Rhosync::Client do
114
115
  end
115
116
 
116
117
  it "should set query callback" do
117
- stub_request(:post, "http://test.rhosync.com/api/set_query_callback").with(
118
+ stub_request(:post, "http://test.Rhoconnect.com/api/set_query_callback").with(
118
119
  :headers => {"Content-Type" => "application/json"}
119
120
  ).to_return(:status => 200, :body => "done")
120
121
  resp = @client.set_query_callback("Person", "http://example.com/callback")
@@ -1,17 +1,17 @@
1
1
  require File.join(File.dirname(__FILE__), 'spec_helper')
2
2
 
3
- describe Rhosync::EndpointHelpers do
3
+ describe Rhoconnect::EndpointHelpers do
4
4
 
5
5
  # Auth stub class
6
6
  class AuthTest; end
7
7
  class BrokenResource < ActiveRecord::Base
8
- include Rhosync::Resource
8
+ include Rhoconnect::Resource
9
9
  end
10
10
 
11
11
  # Query stub class
12
12
  class Product < ActiveRecord::Base
13
- include Rhosync::Resource
14
- def self.rhosync_query(partition)
13
+ include Rhoconnect::Resource
14
+ def self.rhoconnect_query(partition)
15
15
  [self.new]
16
16
  end
17
17
  end
@@ -20,8 +20,8 @@ describe Rhosync::EndpointHelpers do
20
20
  AuthTest.stub!(:do_auth).and_return(success)
21
21
  AuthTest.should_receive(:do_auth).with(@creds)
22
22
 
23
- Rhosync.configure do |config|
24
- config.uri = "http://test.rhosync.com"
23
+ Rhoconnect.configure do |config|
24
+ config.uri = "http://test.rhoconnect.com"
25
25
  config.token = "token"
26
26
  config.authenticate = lambda {|credentials|
27
27
  AuthTest.do_auth(credentials)
@@ -46,31 +46,31 @@ describe Rhosync::EndpointHelpers do
46
46
 
47
47
  it "should call configured authenticate block" do
48
48
  setup_auth_test(true)
49
- Rhosync::Authenticate.call(@env).should == [
49
+ Rhoconnect::Authenticate.call(@env).should == [
50
50
  200, {'Content-Type' => 'text/plain'}, [""]
51
51
  ]
52
52
  end
53
53
 
54
54
  it "should call configured authenticate block with 401" do
55
55
  setup_auth_test(false)
56
- Rhosync::Authenticate.call(@env).should == [
56
+ Rhoconnect::Authenticate.call(@env).should == [
57
57
  401, {'Content-Type' => 'text/plain'}, [""]
58
58
  ]
59
59
  end
60
60
 
61
61
  it "should return true if no authenticate block exists" do
62
- Rhosync.configure do |config|
63
- config.uri = "http://test.rhosync.com"
62
+ Rhoconnect.configure do |config|
63
+ config.uri = "http://test.Rhoconnect.com"
64
64
  config.token = "token"
65
65
  end
66
- Rhosync.configuration.authenticate.should be_nil
67
- Rhosync::Authenticate.call(@env).should == [
66
+ Rhoconnect.configuration.authenticate.should be_nil
67
+ Rhoconnect::Authenticate.call(@env).should == [
68
68
  200, {'Content-Type' => 'text/plain'}, [""]
69
69
  ]
70
70
  end
71
71
 
72
72
  it "should call authenticate block with empty params" do
73
- Rhosync::EndpointHelpers.authenticate('text/plain', '').should == [
73
+ Rhoconnect::EndpointHelpers.authenticate('text/plain', '').should == [
74
74
  200, {"Content-Type"=>"text/plain"}, [""]
75
75
  ]
76
76
  end
@@ -89,34 +89,34 @@ describe Rhosync::EndpointHelpers do
89
89
  )
90
90
  @env.stub!(:body).and_return(@strio)
91
91
  Rack::Request.stub!(:new).and_return(@env)
92
- code, content_type, body = Rhosync::Query.call(@env)
92
+ code, content_type, body = Rhoconnect::Query.call(@env)
93
93
  code.should == 200
94
94
  content_type.should == { "Content-Type" => "application/json" }
95
95
  JSON.parse(body[0]).should == { '1' => Product.new.normalized_attributes }
96
96
  end
97
97
 
98
- it "should fail on missing Rhosync::Resource" do
98
+ it "should fail on missing Rhoconnect::Resource" do
99
99
  @strio.stub!(:read).and_return(
100
100
  {'partition' => 'testuser', 'resource' => 'Broken'}.to_json
101
101
  )
102
102
  @env.stub!(:body).and_return(@strio)
103
103
  Rack::Request.stub!(:new).and_return(@env)
104
- code, content_type, body = Rhosync::Query.call(@env)
104
+ code, content_type, body = Rhoconnect::Query.call(@env)
105
105
  code.should == 404
106
106
  content_type.should == { "Content-Type" => "text/plain" }
107
- body[0].should == "Missing Rhosync::Resource Broken"
107
+ body[0].should == "Missing Rhoconnect::Resource Broken"
108
108
  end
109
109
 
110
- it "should fail on undefined rhosync_query method" do
110
+ it "should fail on undefined rhoconnect_query method" do
111
111
  @strio.stub!(:read).and_return(
112
112
  {'partition' => 'testuser', 'resource' => 'BrokenResource'}.to_json
113
113
  )
114
114
  @env.stub!(:body).and_return(@strio)
115
115
  Rack::Request.stub!(:new).and_return(@env)
116
- code, content_type, body = Rhosync::Query.call(@env)
116
+ code, content_type, body = Rhoconnect::Query.call(@env)
117
117
  code.should == 404
118
118
  content_type.should == { "Content-Type" => "text/plain" }
119
- body[0].should == "error on method `rhosync_query` for BrokenResource: undefined method `rhosync_query' for BrokenResource:Class"
119
+ body[0].should == "error on method `rhoconnect_query` for BrokenResource: undefined method `rhoconnect_query' for BrokenResource:Class"
120
120
  end
121
121
 
122
122
  it "should fail on unknown exception" do
@@ -125,8 +125,8 @@ describe Rhosync::EndpointHelpers do
125
125
  )
126
126
  @env.stub!(:body).and_return(@strio)
127
127
  Rack::Request.stub!(:new).and_return(@env)
128
- Product.stub!(:rhosync_receive_create).and_return { raise "error in create" }
129
- code, content_type, body = Rhosync::Create.call(@env)
128
+ Product.stub!(:rhoconnect_receive_create).and_return { raise "error in create" }
129
+ code, content_type, body = Rhoconnect::Create.call(@env)
130
130
  code.should == 500
131
131
  content_type.should == { "Content-Type" => "text/plain" }
132
132
  body[0].should == "error in create"
@@ -144,7 +144,7 @@ describe Rhosync::EndpointHelpers do
144
144
  @strio.stub!(:read).and_return(params.to_json)
145
145
  @env.stub!(:body).and_return(@strio)
146
146
  Rack::Request.stub!(:new).and_return(@env)
147
- code, content_type, body = Rhosync::Create.call(@env)
147
+ code, content_type, body = Rhoconnect::Create.call(@env)
148
148
  code.should == 200
149
149
  content_type.should == { "Content-Type" => "text/plain" }
150
150
  body.should == ['1']
@@ -163,7 +163,7 @@ describe Rhosync::EndpointHelpers do
163
163
  @strio.stub!(:read).and_return(params.to_json)
164
164
  @env.stub!(:body).and_return(@strio)
165
165
  Rack::Request.stub!(:new).and_return(@env)
166
- code, content_type, body = Rhosync::Update.call(@env)
166
+ code, content_type, body = Rhoconnect::Update.call(@env)
167
167
  code.should == 200
168
168
  content_type.should == { "Content-Type" => "text/plain" }
169
169
  body.should == ["123"]
@@ -182,7 +182,7 @@ describe Rhosync::EndpointHelpers do
182
182
  @strio.stub!(:read).and_return(params.to_json)
183
183
  @env.stub!(:body).and_return(@strio)
184
184
  Rack::Request.stub!(:new).and_return(@env)
185
- code, content_type, body = Rhosync::Delete.call(@env)
185
+ code, content_type, body = Rhoconnect::Delete.call(@env)
186
186
  code.should == 200
187
187
  content_type.should == { "Content-Type" => "text/plain" }
188
188
  body.should == ["123"]
@@ -192,7 +192,7 @@ describe Rhosync::EndpointHelpers do
192
192
 
193
193
  context "on Sinatra endpoints" do
194
194
  class EndpointTest
195
- include Sinatra::RhosyncHelpers
195
+ include Sinatra::RhoconnectHelpers
196
196
  end
197
197
 
198
198
  it "should register endpoints for authenticate and query" do
@@ -201,15 +201,15 @@ describe Rhosync::EndpointHelpers do
201
201
  req = mock("request")
202
202
  req.stub!(:body).and_return(strio)
203
203
  req.stub!(:env).and_return('CONTENT_TYPE' => 'application/json')
204
- Sinatra::RhosyncEndpoints.stub!(:request).and_return(req)
205
- Sinatra::RhosyncEndpoints.stub!(:params).and_return(@params)
206
- Rhosync::EndpointHelpers.stub!(:query)
204
+ Sinatra::RhoconnectEndpoints.stub!(:request).and_return(req)
205
+ Sinatra::RhoconnectEndpoints.stub!(:params).and_return(@params)
206
+ Rhoconnect::EndpointHelpers.stub!(:query)
207
207
  app = mock("app")
208
208
  app.stub!(:post).and_yield
209
209
  app.should_receive(:post).exactly(5).times
210
- app.should_receive(:include).with(Sinatra::RhosyncHelpers)
211
- Sinatra::RhosyncEndpoints.should_receive(:call_helper).exactly(5).times
212
- Sinatra::RhosyncEndpoints.registered(app)
210
+ app.should_receive(:include).with(Sinatra::RhoconnectHelpers)
211
+ Sinatra::RhoconnectEndpoints.should_receive(:call_helper).exactly(5).times
212
+ Sinatra::RhoconnectEndpoints.registered(app)
213
213
  end
214
214
 
215
215
  it "should call helper for authenticate" do
@@ -1,11 +1,11 @@
1
1
  require File.join(File.dirname(__FILE__), 'spec_helper')
2
2
 
3
- describe Rhosync::Resource do
3
+ describe Rhoconnect::Resource do
4
4
 
5
5
  context "on set partition" do
6
6
  it "should set resource partition to :app" do
7
7
  class TestModel1 < ActiveRecord::Base
8
- include Rhosync::Resource
8
+ include Rhoconnect::Resource
9
9
 
10
10
  partition :app
11
11
  end
@@ -15,7 +15,7 @@ describe Rhosync::Resource do
15
15
 
16
16
  it "should set resource partition with lambda" do
17
17
  class TestModel2 < ActiveRecord::Base
18
- include Rhosync::Resource
18
+ include Rhoconnect::Resource
19
19
 
20
20
  partition lambda{ 'helloworld' }
21
21
  end
@@ -27,41 +27,41 @@ describe Rhosync::Resource do
27
27
  context "on initialize" do
28
28
  it "should raise exception if DataMapper or ActiveRecord::Base are missing" do
29
29
  lambda { class TestModel3
30
- include Rhosync::Resource
30
+ include Rhoconnect::Resource
31
31
  end
32
- }.should raise_error("Rhosync::Resource only supports ActiveRecord or DataMapper at this time.")
32
+ }.should raise_error("Rhoconnect::Resource only supports ActiveRecord or DataMapper at this time.")
33
33
  end
34
34
 
35
35
  it "should register callbacks for ActiveRecord::Base" do
36
36
  class TestModel4 < ActiveRecord::Base
37
- include Rhosync::Resource
37
+ include Rhoconnect::Resource
38
38
  end
39
39
 
40
- TestModel4.create_callback.should == :rhosync_create
41
- TestModel4.destroy_callback.should == :rhosync_destroy
42
- TestModel4.update_callback.should == :rhosync_update
40
+ TestModel4.create_callback.should == :rhoconnect_create
41
+ TestModel4.destroy_callback.should == :rhoconnect_destroy
42
+ TestModel4.update_callback.should == :rhoconnect_update
43
43
  end
44
44
 
45
45
  it "should register callbacks for DataMapper::Resource" do
46
46
  class TestModel5
47
47
  include DataMapper::Resource
48
- include Rhosync::Resource
48
+ include Rhoconnect::Resource
49
49
  end
50
50
 
51
- TestModel5.rhosync_callbacks[:create].should == :rhosync_create
52
- TestModel5.rhosync_callbacks[:destroy].should == :rhosync_destroy
53
- TestModel5.rhosync_callbacks[:update].should == :rhosync_update
51
+ TestModel5.rhoconnect_callbacks[:create].should == :rhoconnect_create
52
+ TestModel5.rhoconnect_callbacks[:destroy].should == :rhoconnect_destroy
53
+ TestModel5.rhoconnect_callbacks[:update].should == :rhoconnect_update
54
54
  end
55
55
 
56
56
  it "should raise exception if dm-serializer is missing" do
57
57
  class TestModel6
58
58
  include DataMapper::Resource
59
- include Rhosync::Resource
59
+ include Rhoconnect::Resource
60
60
  end
61
61
  TestModel6.stub!(:is_defined?).and_return(false)
62
62
  lambda {
63
63
  TestModel6.install_callbacks
64
- }.should raise_error("Rhosync::Resource requires dm-serializer to work with DataMapper. Install with `gem install dm-serializer` and add to your application.")
64
+ }.should raise_error("Rhoconnect::Resource requires dm-serializer to work with DataMapper. Install with `gem install dm-serializer` and add to your application.")
65
65
  end
66
66
  end
67
67
 
@@ -69,50 +69,50 @@ describe Rhosync::Resource do
69
69
 
70
70
  it "should call create update delete hook" do
71
71
  class TestModel7 < ActiveRecord::Base
72
- include Rhosync::Resource
72
+ include Rhoconnect::Resource
73
73
  partition :app
74
74
  end
75
- client = mock('Rhosync::Client')
75
+ client = mock('Rhoconnect::Client')
76
76
  client.stub!(:send)
77
- Rhosync::Client.stub!(:new).and_return(client)
77
+ Rhoconnect::Client.stub!(:new).and_return(client)
78
78
  [:create, :update, :destroy].each do |action|
79
79
  client.should_receive(:send).with(
80
80
  action, "TestModel7", :app, {"name"=>"John", "created_at"=>"1299636666", "updated_at"=>"1299636666", "id"=>1}
81
81
  )
82
- TestModel7.new.send("rhosync_#{action}".to_sym)
82
+ TestModel7.new.send("rhoconnect_#{action}".to_sym)
83
83
  end
84
84
  end
85
85
 
86
86
  it "should warn on RestClient::Exception" do
87
87
  class TestModel8 < ActiveRecord::Base
88
- include Rhosync::Resource
88
+ include Rhoconnect::Resource
89
89
  partition :app
90
90
  end
91
- client = mock('Rhosync::Client')
91
+ client = mock('Rhoconnect::Client')
92
92
  exception = RestClient::Exception.new(
93
93
  RestClient::Response.create("error connecting to server", nil, nil), 500
94
94
  )
95
95
  exception.message = "Internal Server Error"
96
96
  client.stub!(:send).and_return { raise exception }
97
- Rhosync::Client.stub!(:new).and_return(client)
97
+ Rhoconnect::Client.stub!(:new).and_return(client)
98
98
  tm = TestModel8.new
99
99
  tm.should_receive(:warn).with(
100
- "TestModel8: rhosync_create returned error: Internal Server Error - error connecting to server"
100
+ "TestModel8: rhoconnect_create returned error: Internal Server Error - error connecting to server"
101
101
  )
102
- tm.rhosync_create
102
+ tm.rhoconnect_create
103
103
  end
104
104
 
105
105
  it "should warn on Exception" do
106
106
  class TestModel8 < ActiveRecord::Base
107
- include Rhosync::Resource
107
+ include Rhoconnect::Resource
108
108
  partition :app
109
109
  end
110
- client = mock('Rhosync::Client')
110
+ client = mock('Rhoconnect::Client')
111
111
  client.stub!(:send).and_return { raise Exception.new("error connecting to server") }
112
- Rhosync::Client.stub!(:new).and_return(client)
112
+ Rhoconnect::Client.stub!(:new).and_return(client)
113
113
  tm = TestModel8.new
114
- tm.should_receive(:warn).with("TestModel8: rhosync_create returned unexpected error: error connecting to server")
115
- tm.rhosync_create
114
+ tm.should_receive(:warn).with("TestModel8: rhoconnect_create returned unexpected error: error connecting to server")
115
+ tm.rhoconnect_create
116
116
  end
117
117
  end
118
118
  end
data/spec/spec_helper.rb CHANGED
@@ -17,7 +17,7 @@ end
17
17
  # stubs for sinatra
18
18
  module Sinatra
19
19
  def self.register(mod); end
20
- module RhosyncEndpoints
20
+ module RhoconnectEndpoints
21
21
  def self.content_type(c_type); end
22
22
  def self.status(code); end
23
23
  end
@@ -90,11 +90,11 @@ module DataMapper
90
90
  end
91
91
 
92
92
  module ClassMethods
93
- attr_accessor :rhosync_callbacks
93
+ attr_accessor :rhoconnect_callbacks
94
94
 
95
95
  def after(action, callback)
96
- @rhosync_callbacks ||= {}
97
- @rhosync_callbacks[action] = callback
96
+ @rhoconnect_callbacks ||= {}
97
+ @rhoconnect_callbacks[action] = callback
98
98
  end
99
99
  end
100
100
  end
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: 27
4
+ hash: 23
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
- - 1
8
+ - 2
9
9
  - 0
10
- version: 0.1.0
10
+ version: 0.2.0
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-08 00:00:00 Z
18
+ date: 2011-06-27 00:00:00 Z
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
21
  type: :runtime
@@ -49,7 +49,7 @@ dependencies:
49
49
  version_requirements: *id002
50
50
  name: json
51
51
  prerelease: false
52
- description: RhoSync rails plugin
52
+ description: Rhoconnect rails plugin
53
53
  email:
54
54
  - support@rhomobile.com
55
55
  executables: []
@@ -68,11 +68,11 @@ files:
68
68
  - config/routes.rb
69
69
  - init.rb
70
70
  - lib/rhoconnect-rb.rb
71
- - lib/rhosync/client.rb
72
- - lib/rhosync/configuration.rb
73
- - lib/rhosync/endpoints.rb
74
- - lib/rhosync/resource.rb
75
- - lib/rhosync/version.rb
71
+ - lib/rhoconnect/client.rb
72
+ - lib/rhoconnect/configuration.rb
73
+ - lib/rhoconnect/endpoints.rb
74
+ - lib/rhoconnect/resource.rb
75
+ - lib/rhoconnect/version.rb
76
76
  - rhoconnect-rb.gemspec
77
77
  - spec/client_spec.rb
78
78
  - spec/endpoints_spec.rb
@@ -110,7 +110,7 @@ rubyforge_project:
110
110
  rubygems_version: 1.8.5
111
111
  signing_key:
112
112
  specification_version: 3
113
- summary: RhoSync rails plugin
113
+ summary: Rhoconnect rails plugin
114
114
  test_files:
115
115
  - spec/client_spec.rb
116
116
  - spec/endpoints_spec.rb
@@ -1,38 +0,0 @@
1
- module Rhosync
2
- class Configuration
3
- attr_accessor :uri, :token, :authenticate, :sync_time_as_int
4
-
5
- def initialize
6
- @sync_time_as_int = true
7
- end
8
-
9
- end
10
-
11
- class << self
12
- attr_accessor :configuration
13
- end
14
-
15
- # Configure RhoSync in an initializer:
16
- # like config/initializers/rhosync.rb
17
- #
18
- # Setup the RhoSync uri and api token.
19
- # Use rhosync:get_token to get the token value.
20
- #
21
- # config.uri = "http://myrhosync.com"
22
- # config.token = "secrettoken"
23
- # config.authenticate = lambda { |credentials|
24
- # User.authenticate(credentials)
25
- # }
26
- #
27
- # @example
28
- # Rhosync.configure do |config|
29
- # config.uri = "http://myrhosync.com"
30
- # config.token = "secrettoken"
31
- # end
32
- def self.configure
33
- self.configuration = Configuration.new
34
- yield(configuration)
35
- end
36
- end
37
-
38
- Rhosync.configure { }
@@ -1,3 +0,0 @@
1
- module Rhosync
2
- VERSION = "0.1.0"
3
- end