ribbon-flow 0.0.2 → 0.0.3

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: 0acb27cf945e51d9babe8e9b0c2b8a9ee79377ae
4
- data.tar.gz: 3d5ca2b56cc3e536e06e7198fe4c47495cd330b9
3
+ metadata.gz: 44ba94d1d8e135b478e29ef77866a1dc8413d333
4
+ data.tar.gz: bd3c21b4efc1cb6c2b3f3f72dba1eaa9113ab952
5
5
  SHA512:
6
- metadata.gz: 3ef82e5b73b6d6f7e48ed645b125397a98fabe5433ca992a7ff40b59478baeebb072df48712228a9adc8f27398507ac91ecdfebd5b04675291037cfefea5fa0c
7
- data.tar.gz: db8b304d1973f08ff90fe76792b7f4ac15d5856f31649c2a78b45b405ab94beb30d586646bdbe6f7718482cc282c118b10ed1e170e78beadd6eafd1efdd05559
6
+ metadata.gz: 8feeaa2cbfb64d1e286bb21f9e8d08395c266942bfb54096181ee9d465b5d197fd447134b59f06a4755699e37fe35b897e82ee860f18fbf19042439efc0363a9
7
+ data.tar.gz: 39fcdf01e5e00df76c624a450148803b30371699693e92ed93c851978395485dbe74cf8521495e3406bec83029255a1cc0214a180e94aed0884878527e254b88
Binary file
data.tar.gz.sig CHANGED
Binary file
@@ -13,9 +13,31 @@ module Flow
13
13
  end
14
14
 
15
15
  def self.connection
16
- @__connection_cache ||= Flow::Connection.new(config.api_key, config.url)
16
+ @__connection_cache ||= Flow::Connection.new(api_key, url)
17
17
  end
18
18
 
19
+ ##################################################
20
+ # Config helpers
21
+ ##################################################
22
+ def self.url
23
+ config.url
24
+ end
25
+
26
+ def self.url=(url)
27
+ config.url = url
28
+ end
29
+
30
+ def self.api_key
31
+ config.api_key
32
+ end
33
+
34
+ def self.api_key=(api_key)
35
+ config.api_key = api_key
36
+ end
37
+
38
+ ##################################################
39
+ # Load helpers
40
+ ##################################################
19
41
  def self.pool(pool_token)
20
42
  Flow::Models::Pool.load(connection, pool_token)
21
43
  end
@@ -27,6 +27,7 @@ module Flow
27
27
  end
28
28
 
29
29
  def url=(url)
30
+ raise Flow::Errors::ConfigError, "URL must be defined" unless url
30
31
  url = url[0..-2] if url[-1] == '/' # No trailing forward-slash
31
32
  _set(:url, url)
32
33
  end
@@ -40,7 +41,7 @@ module Flow
40
41
  def _set(key, value)
41
42
  # Changing the config value should require calling the
42
43
  # _set method again.
43
- return @config[key.to_sym] = value.dup.freeze
44
+ return @config[key.to_sym] = value.nil? ? nil : value.dup.freeze
44
45
  end
45
46
 
46
47
  def _get(key)
@@ -15,11 +15,15 @@ module Flow
15
15
  attr_reader :port
16
16
 
17
17
  def initialize(api_key, url='https://flow.ribbon.co', version=1)
18
+ raise Flow::Errors::ConnectionError, "API Key must be defined." unless api_key
19
+ raise Flow::Errors::ConnectionError, "URL must be defined." unless url
20
+ raise Flow::Errors::ConnectionError, "Version must be defined." unless version
21
+
18
22
  @version = version
19
23
 
20
24
  @url = (url[-1] == '/' ? url[0..-2] : url).dup.freeze # Remove trailing slash.
21
-
22
- if /^([A-Za-z0-9]{20})\.([A-Za-z0-9]{43})$/.match(api_key)
25
+
26
+ if /^([A-Za-z0-9]{20})\.([A-Za-z0-9]{43})$/.match(api_key.to_s)
23
27
  @access_key = $1.dup.freeze
24
28
  @signing_key = $2.dup.freeze
25
29
  else
@@ -7,6 +7,8 @@ module Flow
7
7
  class ConnectionError < FlowError; end
8
8
  class ResponseError < FlowError; end
9
9
  class LoadError < FlowError; end
10
+ class BuildError < FlowError; end
11
+ class ConfigError < FlowError; end
10
12
 
11
13
  end
12
14
  end
@@ -1,4 +1,5 @@
1
1
  require 'flow/models/model'
2
2
  require 'flow/models/pool'
3
3
  require 'flow/models/card'
4
- require 'flow/models/transaction'
4
+ require 'flow/models/transaction'
5
+ require 'flow/models/credit'
@@ -2,14 +2,6 @@ module Flow
2
2
  module Models
3
3
  class Card < Model
4
4
 
5
- def self.load(connection, card_token)
6
- r = connection.get("cards/#{card_token}")
7
-
8
- raise Flow::Errors::LoadError.new(r.message) unless r.success?
9
-
10
- return r.card
11
- end
12
-
13
5
  def ready?
14
6
  self.status == 'ready'
15
7
  end
@@ -30,7 +22,7 @@ module Flow
30
22
  end
31
23
 
32
24
  raise Flow::Errors::ResponseError.new(r.message) unless r.success?
33
-
25
+
34
26
  r.transaction
35
27
  end
36
28
 
@@ -0,0 +1,27 @@
1
+ module Flow
2
+ module Models
3
+ class Credit < Transaction
4
+
5
+ def pending?
6
+ self.status == 'pending'
7
+ end
8
+
9
+ def scheduled?
10
+ self.status == 'scheduled'
11
+ end
12
+
13
+ def cleared?
14
+ self.status == 'cleared'
15
+ end
16
+
17
+ def failed?
18
+ self.status == 'failed'
19
+ end
20
+
21
+ def error?
22
+ self.status == 'error'
23
+ end
24
+
25
+ end
26
+ end
27
+ end
@@ -11,6 +11,16 @@ module Flow
11
11
  @model_hash = model_hash
12
12
  end
13
13
 
14
+ def reload
15
+ self.class.load(connection, self.token)
16
+ end
17
+
18
+ def reload!
19
+ updated_version = self.reload
20
+ @model_hash = updated_version.model_hash
21
+ self
22
+ end
23
+
14
24
  def method_missing(meth, *args, &block)
15
25
  if model_hash.key?(meth.to_s)
16
26
  return Model.load_model(meth, connection, model_hash[meth.to_s])
@@ -19,22 +29,52 @@ module Flow
19
29
  super
20
30
  end
21
31
 
22
- def self.load_model(name, connection, model_hash)
23
- name = name.to_sym
24
-
25
- case name
26
- when :pool
27
- return Flow::Models::Pool.new(connection, model_hash)
28
- when :card
29
- return Flow::Models::Card.new(connection, model_hash)
30
- when :transaction
31
- return Flow::Models::Transaction.new(connection, model_hash)
32
- else
33
- return model_hash
32
+ ############################################################
33
+ # Class methods
34
+ ############################################################
35
+ class << self
36
+ def __load_path
37
+ # Really wish I had Rail's pluralize here, but this
38
+ # fits all of our cases for now and should fit most
39
+ # if not all of our cases going forward.
40
+ "#{__model_name}s"
41
+ end
42
+
43
+ def __model_name
44
+ model_name = self.name.split('::').last.downcase
45
+ raise "should not call this for abstract model" if model_name == 'model'
46
+ model_name
34
47
  end
35
- end
36
48
 
37
- end
49
+ def build(connection, model_hash)
50
+ # Default behavior is just to create an instance
51
+ # of the class.
52
+ self.new(connection, model_hash)
53
+ end
54
+
55
+ def load(connection, token)
56
+ r = connection.get("#{__load_path}/#{token}")
57
+ raise Flow::Errors::LoadError.new(r.message) unless r.success?
58
+
59
+ return r.send(__model_name)
60
+ end
61
+
62
+ def load_model(name, connection, model_hash)
63
+ name = name.to_sym
64
+
65
+ case name
66
+ when :pool
67
+ return Flow::Models::Pool.build(connection, model_hash)
68
+ when :card
69
+ return Flow::Models::Card.build(connection, model_hash)
70
+ when :transaction
71
+ return Flow::Models::Transaction.build(connection, model_hash)
72
+ else
73
+ return model_hash
74
+ end
75
+ end
76
+ end # class << self
38
77
 
39
- end
40
- end
78
+ end # Model
79
+ end # Models
80
+ end # Flow
@@ -2,12 +2,12 @@ module Flow
2
2
  module Models
3
3
  class Pool < Model
4
4
 
5
- def self.load(connection, pool_token)
6
- r = connection.get("pools/#{pool_token}")
7
-
8
- raise Flow::Errors::LoadError.new(r.message) unless r.success?
9
-
10
- return r.pool
5
+ def test?
6
+ self.type == 'test'
7
+ end
8
+
9
+ def production?
10
+ self.type == 'production'
11
11
  end
12
12
 
13
13
  end
@@ -2,34 +2,26 @@ module Flow
2
2
  module Models
3
3
  class Transaction < Model
4
4
 
5
- def self.load(connection, tx_token)
6
- r = connection.get("transactions/#{tx_token}")
7
-
8
- raise Flow::Errors::LoadError.new(r.message) unless r.success?
9
-
10
- return r.transaction
11
- end
12
-
13
- def pending?
14
- self.status == 'pending'
5
+ def self.build(connection, model_hash)
6
+ case model_hash['type'].to_sym
7
+ when :credit
8
+ return Credit.new(connection, model_hash)
9
+ else
10
+ raise Flow::Errors::BuildError.new("Invalid Transaction type: #{model_hash['type']}.")
11
+ end
15
12
  end
16
13
 
17
- def scheduled?
18
- self.status == 'scheduled'
14
+ # Override inherited behavior.
15
+ # This causes subclasses of transactions to continue
16
+ # to be treated as transactions at the API level.
17
+ def self.__model_name
18
+ 'transaction'
19
19
  end
20
20
 
21
- def cleared?
22
- self.status == 'cleared'
21
+ def credit?
22
+ self.type == 'credit'
23
23
  end
24
24
 
25
- def failed?
26
- self.status == 'failed'
27
- end
28
-
29
- def error?
30
- self.status == 'error'
31
- end
32
-
33
25
  end
34
26
  end
35
27
  end
@@ -1,4 +1,4 @@
1
1
 
2
2
  module Flow
3
- VERSION = '0.0.2'
3
+ VERSION = '0.0.3'
4
4
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ribbon-flow
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Robert Honer
@@ -30,7 +30,7 @@ cert_chain:
30
30
  grVI0T63q4/t9yjlCPkbK1VTObseWNPA2/7twecdkCVN3VaWEml4xf2KiOwnKDfk
31
31
  aZOXvndbbL+k3uaxs/Fpfsi0AD02HiceGjSOZFd9Wyk=
32
32
  -----END CERTIFICATE-----
33
- date: 2014-06-09 00:00:00.000000000 Z
33
+ date: 2014-06-10 00:00:00.000000000 Z
34
34
  dependencies:
35
35
  - !ruby/object:Gem::Dependency
36
36
  name: rest-client
@@ -58,6 +58,7 @@ files:
58
58
  - lib/flow/connection.rb
59
59
  - lib/flow/errors.rb
60
60
  - lib/flow/models/card.rb
61
+ - lib/flow/models/credit.rb
61
62
  - lib/flow/models/model.rb
62
63
  - lib/flow/models/pool.rb
63
64
  - lib/flow/models/transaction.rb
metadata.gz.sig CHANGED
Binary file