apibanca-client 0.0.1 → 0.0.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8675cb95bc5d494b86e15a41228a64319b874cec
4
- data.tar.gz: 4cf002c3f6709a47fbd01aa0a4c743ed04a28317
3
+ metadata.gz: a05aac810bc32fe182122fb2cd8ef1961d683928
4
+ data.tar.gz: f2775c0ade0e6ec278975bcd0cc5cab7cc05dcf4
5
5
  SHA512:
6
- metadata.gz: f886239319af97a59f1e46970b68fe358a446ae0cd044b0589ac3ac6950da84acfcc652024ba1feb64ece67f890a65504f3e8af2cbb9b2b8300a374c69fb778f
7
- data.tar.gz: b506ef352697aa3d6e580a1d2b31a0063e917c7057c633da8bf099c89d723e891e27397a824335bb48d983d1d1a12b2499110d10bb3fbdd56976a88ec24be0f6
6
+ metadata.gz: 4235bcaabe647464ebfe812462eb381b3a0fefcc63426193a48609fb82b8d92520c466bdfa88f50e8ed18f7abfec1c5947a29ad9e6048ed9600bccd372880596
7
+ data.tar.gz: fc953a05aeae0d3f9cd95451bcce25f5d4e77909093375a26578e92b5c81b40a97c17e28d9b993b50b834672773a7efae438539f6f53c48565c602d0a5567681
data/CHANGELOG.md ADDED
@@ -0,0 +1,23 @@
1
+ # Bitácora de cambios
2
+
3
+ ## 0.0.2
4
+
5
+ ### Configuración del cliente
6
+ El cliente ya no guarda el secreto y la URI de manera estática. En vez de eso, (a) dichos parámetros son argumentos del constructor, y (b) el cliente debe ser pasado como argumento en los métodos estáticos de `Apibanca::Bank`.
7
+
8
+ ```ruby
9
+ # a)
10
+ # Antes: Apibanca::Client.configure { |c| c.secret = "API_KEY..."}
11
+ client = Apibanca::Client.new("API_KEY...")
12
+
13
+ # b)
14
+ # Antes: bank = Apibanca::Bank.create(params)
15
+ bank = Apibanca::Bank.create(client, params)
16
+ ```
17
+
18
+ ### Búsqueda de bancos
19
+ La función `Bank.index` ahora recibe un hash de parámetros opcionales. Ese hash soporta `name` y `user`.
20
+
21
+ ```ruby
22
+ banks = Apibanca::Bank.index(client, name: "BICE") # retorna todos los bancos con name == BICE
23
+ ```
data/README.md CHANGED
@@ -20,3 +20,26 @@ O bien instálala tú mismo:
20
20
 
21
21
  $ gem install apibanca-client
22
22
 
23
+ ## Uso
24
+
25
+ ### Instanciar el cliente
26
+
27
+ Para realizar cualquier operación desde la gema, hay que autentificarse con la API-Key obtenida en http://api-banca.herokuapp.com.
28
+
29
+ ```ruby
30
+ # Obtenida desde http://api-banca.herokuapp.com
31
+ API_KEY = "b3e0f65c-dea6-4003-d9d0-9a8745d9988"
32
+ client = Apibanca::Client.new(API_KEY)
33
+ ```
34
+
35
+ ### Crear un banco
36
+
37
+ Los bancos pueden ser creados a través de la interfaz web o desde esta gema. Para crear un banco desde la gema, hay que proveer todos los parámetros solicitados por la clase `Apibanca::Bank::BankCreationParams`.
38
+
39
+ ```ruby
40
+ params = Apibanca::Bank::BankCreationParams.new(name: "BICE", user: "<rut del usuario>", pass: "<password para la web del banco>", account: "<número de cuenta>"
41
+ # => #<Apibanca::Bank::BankCreationParams account="..." name="BICE" pass="..." user="...">
42
+ banco = Apibanca::Bank.create(client, params)
43
+ # -> POST http://api-banca.herokuapp.com/api/2013-11-4/banks/ [{:bank=>{"name"=>"BICE", "user"=>"...", "pass"=>"...", "account"=>"..."}} params]
44
+ # Banco BICE / 157768441 / unica
45
+ ```
@@ -8,17 +8,16 @@ require "faraday/response/apibanca_errors"
8
8
 
9
9
  module Apibanca
10
10
  class Client
11
-
12
- class << self
11
+ module Http
13
12
  def get uri, params=nil
14
- Apibanca::Client.conn_url.get do |req|
13
+ conn_url.get do |req|
15
14
  req.url uri, params
16
15
  req.headers['bc-auth-token'] = @secret
17
16
  end
18
17
  end
19
18
 
20
19
  def post uri, body=nil
21
- Apibanca::Client.conn_form.post do |req|
20
+ conn_form.post do |req|
22
21
  req.url uri
23
22
  req.headers['bc-auth-token'] = @secret
24
23
  req.body = body if body
@@ -26,7 +25,7 @@ module Apibanca
26
25
  end
27
26
 
28
27
  def patch uri, body=nil
29
- Apibanca::Client.conn_form.patch do |req|
28
+ conn_form.patch do |req|
30
29
  req.url uri
31
30
  req.headers['bc-auth-token'] = @secret
32
31
  req.body = body if body
@@ -34,7 +33,7 @@ module Apibanca
34
33
  end
35
34
 
36
35
  def delete uri
37
- Apibanca::Client.conn_form.delete do |req|
36
+ conn_form.delete do |req|
38
37
  req.url uri
39
38
  req.headers['bc-auth-token'] = @secret
40
39
  end
@@ -42,7 +41,7 @@ module Apibanca
42
41
 
43
42
  def conn_form
44
43
  check_requirements!
45
- @conn ||= Faraday.new(:url => @base_uri) do |f|
44
+ @conn_form ||= Faraday.new(:url => @base_uri) do |f|
46
45
  f.request :apibanca_request_logger
47
46
  f.request :json
48
47
  f.response :apibanca_errors
@@ -54,7 +53,7 @@ module Apibanca
54
53
 
55
54
  def conn_url
56
55
  check_requirements!
57
- @conn ||= Faraday.new(:url => @base_uri) do |f|
56
+ @conn_url ||= Faraday.new(:url => @base_uri) do |f|
58
57
  f.request :apibanca_request_logger
59
58
  f.request :url_encoded
60
59
  f.response :apibanca_errors
@@ -63,33 +62,19 @@ module Apibanca
63
62
  f.adapter Faraday.default_adapter
64
63
  end
65
64
  end
65
+ end
66
+ include Http
66
67
 
67
- def configure &block
68
- raise ArgumentError, "El bloque debe recibir un argumento" unless block.arity == 1
69
- yield self
70
- end
71
-
72
- def secret= value
73
- @secret = value
74
- end
75
-
76
- def secret
77
- @secret
78
- end
79
-
80
- def base_uri= value
81
- @base_uri = value
82
- end
83
-
84
- def base_uri
85
- @base_uri ||= "http://localhost:3000/api/2013-11-4"
86
- end
68
+ def initialize secret, api_uri = "http://api-banca.herokuapp.com/api/2013-11-4"
69
+ raise ArgumentError, "Debe indicar el secreto para acceder a la API" unless secret
70
+ @secret = secret
71
+ @base_uri = api_uri
72
+ end
87
73
 
88
- private
89
- def check_requirements!
90
- raise ArgumentError, "Debe indicar el secreto" unless secret
91
- raise ArgumentError, "Debe indicar la URI" unless base_uri
92
- end
74
+ private
75
+ def check_requirements!
76
+ raise ArgumentError, "Debe indicar el secreto" unless @secret
77
+ raise ArgumentError, "Debe indicar la URI" unless @base_uri
93
78
  end
94
79
  end
95
80
  end
@@ -3,26 +3,29 @@ class Apibanca::Bank < Apibanca::ProxyBase
3
3
  set_relative_url "banks"
4
4
 
5
5
  class << self
6
- def create bank_params
6
+ def create client, bank_params
7
7
  raise ArgumentError, "Los parámetros deben ser ApiBanca::Bank::BankCreationParams" unless bank_params.is_a? Apibanca::Bank::BankCreationParams
8
- r = Apibanca::Client.post url, { bank: bank_params.to_hash }
8
+ r = client.post url, { bank: bank_params.to_hash }
9
9
  bank = Apibanca::Bank.new(r.body)
10
+ bank.obj_client = client
10
11
  bank.routines.map! { |r| Apibanca::Routine.new(r) }
11
12
  bank
12
13
  end
13
14
 
14
- def load id, recursive=true
15
- r = Apibanca::Client.get url("#{id}")
15
+ def load client, id, recursive=true
16
+ r = client.get url("#{id}")
16
17
  bank = Apibanca::Bank.new(r.body)
18
+ bank.obj_client = client
17
19
  bank.routines.map! { |r| Apibanca::Routine.new(r) }
18
20
  bank.routines.each { |r| r.refresh! } if recursive
19
21
  bank
20
22
  end
21
23
 
22
- def index recursive=false
23
- r = Apibanca::Client.get url
24
+ def index client, params=nil, recursive=false
25
+ r = client.get url, params
24
26
  r.body.map do |raw|
25
27
  bank = Apibanca::Bank.new(raw)
28
+ bank.obj_client = client
26
29
  bank.routines.map! { |r| Apibanca::Routine.new(r) }
27
30
  bank.routines.each { |r| r.refresh! } if recursive
28
31
  bank
@@ -31,7 +34,7 @@ class Apibanca::Bank < Apibanca::ProxyBase
31
34
  end
32
35
 
33
36
  def refresh! recursive=true
34
- r = Apibanca::Client.get url
37
+ r = obj_client.get url
35
38
  old_routines = self.routines
36
39
  self.merge! r.body
37
40
  self.routines = old_routines
@@ -40,29 +43,38 @@ class Apibanca::Bank < Apibanca::ProxyBase
40
43
  end
41
44
 
42
45
  def change_password new_pass
43
- r = Apibanca::Client.patch url("change_password"), pass: new_pass
46
+ r = obj_client.patch url("change_password"), pass: new_pass
44
47
  true
45
48
  end
46
49
 
47
50
  def add_routine routine_params
48
51
  raise ArgumentError, "Los parámetros deben ser ApiBanca::Bank::RoutineCreationParams" unless routine_params.is_a? Apibanca::Bank::RoutineCreationParams
49
- r = Apibanca::Client.post url("add_routine"), { routine: routine_params.to_hash }
52
+ r = obj_client.post url("add_routine"), { routine: routine_params.to_hash }
50
53
  r.body.routines.each do |routine|
51
54
  new_routine = Apibanca::Routine.new(routine) unless self.routines.map { |i| i.id }.include?(routine.id)
52
55
  next unless new_routine
56
+ new_routine.obj_client = self.obj_client
53
57
  new_routine.refresh!
54
58
  self.routines << new_routine
55
59
  end
56
60
  end
57
61
 
58
62
  def delete
59
- r = Apibanca::Client.delete url
63
+ r = obj_client.delete url
60
64
  true
61
65
  end
62
66
 
63
67
  def load_deposits params=nil
64
- r = Apibanca::Client.get url("deposits"), params
65
- self.deposits = r.body.map { |d| nd = Apibanca::Deposit.new(d); nd.obj_bank = self; nd }
68
+ r = obj_client.get url("deposits"), params
69
+ self.deposits = r.body.map { |d| nd = Apibanca::Deposit.new(d); nd.obj_bank = self; nd.obj_client = obj_client; nd }
70
+ end
71
+
72
+ def to_s
73
+ "Banco #{name} / #{user} / #{account}"
74
+ end
75
+
76
+ def inspect
77
+ to_s
66
78
  end
67
79
 
68
80
  class BankCreationParams < Hashie::Dash
@@ -1,6 +1,6 @@
1
1
  class Apibanca::Deposit < Apibanca::ProxyBase
2
2
  def load_history
3
- h = Apibanca::Client.get obj_bank.url("deposits/#{self.id}/history")
3
+ h = obj_client.get obj_bank.url("deposits/#{self.id}/history")
4
4
  @history = h.body.map { |d| dv = Apibanca::DepositVersion.new(d); dv.obj_deposit = self; dv }
5
5
  end
6
6
 
@@ -3,28 +3,28 @@ class Apibanca::Routine < Apibanca::ProxyBase
3
3
  set_relative_url "routines"
4
4
 
5
5
  def refresh!
6
- r = Apibanca::Client.get url
6
+ r = obj_client.get url
7
7
  self.merge! r.body
8
8
  end
9
9
 
10
10
  def turn_on
11
- r = Apibanca::Client.patch url("turn_on")
11
+ r = obj_client.patch url("turn_on")
12
12
  self.merge! r.body
13
13
  end
14
14
 
15
15
  def turn_off
16
- r = Apibanca::Client.patch url("turn_off")
16
+ r = obj_client.patch url("turn_off")
17
17
  self.merge! r.body
18
18
  end
19
19
 
20
20
  def schedule params
21
21
  raise ArgumentError, "Los parámetros deben ser ApiBanca::Routine::ScheduleParams" unless params.is_a? Apibanca::Routine::ScheduleParams
22
- r = Apibanca::Client.patch url("schedule"), params.to_hash
22
+ r = obj_client.patch url("schedule"), params.to_hash
23
23
  self.merge! r.body
24
24
  end
25
25
 
26
26
  def load_tasks
27
- r = Apibanca::Client.get url("tasks")
27
+ r = obj_client.get url("tasks")
28
28
  self.tasks = r.body
29
29
  end
30
30
 
@@ -1,5 +1,5 @@
1
1
  module Apibanca
2
2
  class Client
3
- VERSION = "0.0.1"
3
+ VERSION = "0.0.2"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: apibanca-client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Pablo Marambio
@@ -104,6 +104,7 @@ files:
104
104
  - .gitignore
105
105
  - .rvmrc
106
106
  - .travis.yml
107
+ - CHANGELOG.md
107
108
  - Gemfile
108
109
  - LICENSE.txt
109
110
  - README.md