finance_sync 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -6,7 +6,7 @@ require 'finance_sync'
6
6
 
7
7
  Instancie a classe FinanceSync::Client passando a url do servidor e a versão dos dados que estão na máquina cliente:
8
8
 
9
- @client = FinanceSync::Client.new(:services_url=>"https://services.myfreecomm.com.br/, :version=>22)
9
+ @client = FinanceSync::Client.new(:services_url => "services.myfreecomm.com.br", :version => 22, :user => "foo", :password => "bar")
10
10
 
11
11
  Para sincronizar, você deve mandar um hash com os dados que você alterou desde a última versão:
12
12
 
data/lib/finance_sync.rb CHANGED
@@ -9,12 +9,14 @@ module FinanceSync
9
9
 
10
10
  class Client
11
11
  attr_reader :version, :entities, :conflicts, :error
12
+ attr_accessor :auth_type
12
13
 
13
14
  def initialize(params)
14
15
  @services_url = params[:services_url]
15
16
  @version = params[:version]
16
17
  @user = params[:user]
17
18
  @password = params[:password]
19
+ @auth_type = params[:auth_type] || :new
18
20
  raise ArgumentError if @user.nil? or @password.nil?
19
21
  raise TypeError unless @version.is_a? Fixnum
20
22
 
@@ -22,7 +24,7 @@ module FinanceSync
22
24
  @conflicts = []
23
25
  end
24
26
 
25
- def synchronize(data)
27
+ def synchronize(data={})
26
28
  data = request_sync(data)
27
29
  return false if data == false
28
30
  @version = data['version']
@@ -42,7 +44,9 @@ module FinanceSync
42
44
  errors = {"400"=>"bad data", "500"=>"sync server error",
43
45
  "423"=>"locked", "401"=> "login error"}
44
46
 
45
- put = Net::HTTP::Put.new("/financedesktop/sync/synchronize")
47
+ path = "/financedesktop/sync/synchronize"
48
+ path += "?auth=myfc_id" if @auth_type == :new
49
+ put = Net::HTTP::Put.new(path)
46
50
  put.basic_auth(@user, @password)
47
51
  put.content_type = 'application/json'
48
52
  begin
@@ -1,3 +1,3 @@
1
1
  module FinanceSync
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -2,20 +2,20 @@
2
2
  require 'spec_helper'
3
3
 
4
4
  ENTITIES = [
5
- {"entity"=>
6
- {"currency"=> "BRL", "payee"=> nil, "amount"=> "100.00000000",
7
- "imported"=> false, "description"=> "Novo lançamento",
8
- "payer"=> "Rendimento aplicação", "base_date"=> "2010-03-16T11=>29=>02.354014",
9
- "document_number"=> 1, "guid"=> "ffffffffffffffffffffffffffffffff"
10
- }
11
- }
12
- ]
5
+ {"entity"=>
6
+ {"currency"=> "BRL", "payee"=> nil, "amount"=> "100.00000000",
7
+ "imported"=> false, "description"=> "Novo lançamento",
8
+ "payer"=> "Rendimento aplicação", "base_date"=> "2010-03-16T11=>29=>02.354014",
9
+ "document_number"=> 1, "guid"=> "ffffffffffffffffffffffffffffffff"
10
+ }
11
+ }
12
+ ]
13
13
 
14
14
 
15
15
  UPDATED_VERSION = '{"conflicts": [], "version": 49, "data": [{"entity": {"imported": "true",
16
- "description": "roupa", "document-number": 111, "amount": "200.00000000",
17
- "date": "2010-10-11", "guid": "22a648b0-2fe1-4888-96a4-caa27a4c27d7",
18
- "type": "transaction"}}]}'
16
+ "description": "roupa", "document-number": 111, "amount": "200.00000000",
17
+ "date": "2010-10-11", "guid": "22a648b0-2fe1-4888-96a4-caa27a4c27d7",
18
+ "type": "transaction"}}]}'
19
19
 
20
20
  USER = 'jalim'
21
21
  PASSWORD = 'habei'
@@ -29,20 +29,25 @@ describe FinanceSync::Client do
29
29
  credentials = ""
30
30
  end
31
31
  FakeWeb.register_uri(
32
- :put,
33
- "http://#{credentials}services.myfreecomm.com.br/financedesktop/sync/synchronize",
34
- response)
32
+ :put,
33
+ "http://#{credentials}services.myfreecomm.com.br/financedesktop/sync/synchronize?auth=myfc_id",
34
+ response
35
+ )
35
36
  end
36
37
 
37
38
  before(:each) do
38
39
  @services_url = "services.myfreecomm.com.br"
39
- @finance_sync = FinanceSync::Client.new(:services_url => @services_url, :version => 2,
40
- :user=>USER, :password=>PASSWORD)
40
+ @finance_sync = FinanceSync::Client.new(
41
+ :services_url => @services_url, :version => 2,
42
+ :user => USER, :password => PASSWORD
43
+ )
41
44
  end
42
45
 
43
46
  it "should have .version" do
44
- @finance_sync = FinanceSync::Client.new(:services_url=>@services_url, :version => 4,
45
- :user=>USER, :password=>PASSWORD)
47
+ @finance_sync = FinanceSync::Client.new(
48
+ :services_url=>@services_url, :version => 4,
49
+ :user => USER, :password => PASSWORD
50
+ )
46
51
  @finance_sync.version.should == 4
47
52
  end
48
53
 
@@ -50,35 +55,72 @@ describe FinanceSync::Client do
50
55
  it "should store version" do
51
56
  @finance_sync.instance_variable_get(:@version).should == 2
52
57
  end
53
-
54
58
  it "should store services_url" do
55
59
  @finance_sync.instance_variable_get(:@services_url).should == 'services.myfreecomm.com.br'
56
60
  end
61
+ it "should store default auth_type" do
62
+ @finance_sync.instance_variable_get(:@auth_type).should == :new
63
+ end
57
64
  end
58
65
 
59
-
60
66
  describe "synchronizer" do
61
- it "should send the params formed correctly" do
62
- @finance_sync = FinanceSync::Client.new(:services_url => @services_url, :version => 48,
63
- :user=>USER, :password=>PASSWORD)
67
+ it "should authenticate using the old method (drupal accounts)" do
68
+ @finance_sync = FinanceSync::Client.new(
69
+ :services_url => @services_url, :version => 48,
70
+ :user=>USER, :password=>PASSWORD, :auth_type => :old
71
+ )
64
72
  correct_json = {"version"=>@finance_sync.version, "data"=>ENTITIES}.to_json
65
73
 
66
74
  put = mock(Net::HTTP::Put)
67
75
  Net::HTTP::Put.should_receive(:new).with("/financedesktop/sync/synchronize").once.and_return(put)
68
76
  put.should_receive(:basic_auth).with(USER, PASSWORD)
69
77
  put.should_receive(:content_type=).with('application/json')
70
-
71
78
  http = mock()
72
79
  http.should_receive(:request).with(put, correct_json)
73
-
74
80
  http_success_mock = mock(Net::HTTPSuccess)
75
81
  http_success_mock.stub!(:code).and_return("200")
76
82
  http_success_mock.stub!(:body).and_return(UPDATED_VERSION)
77
-
78
83
  Net::HTTP.should_receive(:start).with(@services_url).and_yield(http).and_return(http_success_mock)
79
- @finance_sync.synchronize(ENTITIES)
84
+ @finance_sync.synchronize(ENTITIES)
80
85
  end
81
-
86
+ it "should authenticate using the new method (passaporte web accounts)" do
87
+ @finance_sync = FinanceSync::Client.new(
88
+ :services_url => @services_url, :version => 48,
89
+ :user=>USER, :password=>PASSWORD, :auth_type => :new
90
+ )
91
+ correct_json = {"version"=>@finance_sync.version, "data"=>ENTITIES}.to_json
92
+ put = mock(Net::HTTP::Put)
93
+ Net::HTTP::Put.should_receive(:new).with("/financedesktop/sync/synchronize?auth=myfc_id").once.and_return(put)
94
+ put.should_receive(:basic_auth).with(USER, PASSWORD)
95
+ put.should_receive(:content_type=).with('application/json')
96
+ http = mock()
97
+ http.should_receive(:request).with(put, correct_json)
98
+ http_success_mock = mock(Net::HTTPSuccess)
99
+ http_success_mock.stub!(:code).and_return("200")
100
+ http_success_mock.stub!(:body).and_return(UPDATED_VERSION)
101
+ Net::HTTP.should_receive(:start).with(@services_url).and_yield(http).and_return(http_success_mock)
102
+ @finance_sync.synchronize(ENTITIES)
103
+ end
104
+ it "should change authentication methods via attr_writer" do
105
+ @finance_sync = FinanceSync::Client.new(
106
+ :services_url => @services_url, :version => 48,
107
+ :user=>USER, :password=>PASSWORD, :auth_type => :old
108
+ )
109
+ @finance_sync.auth_type = :new
110
+ correct_json = {"version"=>@finance_sync.version, "data"=>ENTITIES}.to_json
111
+ put = mock(Net::HTTP::Put)
112
+ Net::HTTP::Put.should_receive(:new).with("/financedesktop/sync/synchronize?auth=myfc_id").once.and_return(put)
113
+ put.should_receive(:basic_auth).with(USER, PASSWORD)
114
+ put.should_receive(:content_type=).with('application/json')
115
+ http = mock()
116
+ http.should_receive(:request).with(put, correct_json)
117
+ http_success_mock = mock(Net::HTTPSuccess)
118
+ http_success_mock.stub!(:code).and_return("200")
119
+ http_success_mock.stub!(:body).and_return(UPDATED_VERSION)
120
+ Net::HTTP.should_receive(:start).with(@services_url).and_yield(http).and_return(http_success_mock)
121
+ @finance_sync.synchronize(ENTITIES)
122
+ end
123
+
82
124
  describe "with not found url" do
83
125
  before(:each) do
84
126
  @invalid_url = 'invalid_url'
@@ -87,7 +129,7 @@ describe FinanceSync::Client do
87
129
  @correct_json = {"version"=>@finance_sync.version, "data"=>ENTITIES}.to_json
88
130
 
89
131
  @put = mock(Net::HTTP::Put)
90
- Net::HTTP::Put.should_receive(:new).with("/financedesktop/sync/synchronize").once.and_return(@put)
132
+ Net::HTTP::Put.should_receive(:new).with("/financedesktop/sync/synchronize?auth=myfc_id").once.and_return(@put)
91
133
  @put.should_receive(:basic_auth).with(USER, PASSWORD)
92
134
  @put.should_receive(:content_type=).with('application/json')
93
135
  http = mock()
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: finance_sync
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -10,12 +10,12 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2011-06-08 00:00:00.000000000 -03:00
13
+ date: 2011-07-20 00:00:00.000000000 -03:00
14
14
  default_executable:
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
17
17
  name: rspec
18
- requirement: &2157289460 !ruby/object:Gem::Requirement
18
+ requirement: &2171450960 !ruby/object:Gem::Requirement
19
19
  none: false
20
20
  requirements:
21
21
  - - ~>
@@ -23,10 +23,10 @@ dependencies:
23
23
  version: 2.6.0
24
24
  type: :development
25
25
  prerelease: false
26
- version_requirements: *2157289460
26
+ version_requirements: *2171450960
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: fakeweb
29
- requirement: &2157288960 !ruby/object:Gem::Requirement
29
+ requirement: &2171450360 !ruby/object:Gem::Requirement
30
30
  none: false
31
31
  requirements:
32
32
  - - ~>
@@ -34,7 +34,7 @@ dependencies:
34
34
  version: 1.3.0
35
35
  type: :development
36
36
  prerelease: false
37
- version_requirements: *2157288960
37
+ version_requirements: *2171450360
38
38
  description: Ruby client to the FinanceSync API
39
39
  email:
40
40
  - vanderson.mota@myfreecomm.com.br