mintchip 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.
@@ -1,50 +1,141 @@
1
+ require 'openssl'
2
+ require 'net/http'
3
+ require 'net/https'
4
+ require 'uri'
5
+ require 'json'
6
+ require 'active_support/all'
7
+
1
8
  require "mintchip/version"
2
9
 
3
10
  module Mintchip
4
-
5
- CURRENCY_CODES = {
6
- CAD: 1
7
- }
11
+ CURRENCY_CODES = { CAD: 1 }
8
12
 
9
13
  class InvalidCurrency < StandardError; end
14
+ class Error < StandardError ; end
15
+ class SystemError < Error ; end
16
+ class CryptoError < Error ; end
17
+ class FormatError < Error ; end
18
+ class MintchipError < Error ; end
19
+
10
20
 
11
21
  def self.currency_code(name)
12
- CURRENCY_CODES(name.to_sym) or raise InvalidCurrency
22
+ CURRENCY_CODES[name.to_sym] or raise InvalidCurrency
13
23
  end
14
24
 
15
- # GET /mintchip/info/{responseformat}
16
- def info
17
- get "/mintchip/info/json"
18
- end
25
+ class Hosted
26
+ RESOURCE_BASE = "https://remote.mintchipchallenge.com/mintchip"
19
27
 
20
- # POST /mintchip/receipts
21
- def load_value(vm)
22
- post "/mintchip/receipts", {"Value Message" => vm}, {"Content-Type" => "application/vnd.scg.ecn-message"}
23
- end
28
+ def initialize(key, password)
29
+ @p12 = OpenSSL::PKCS12.new(key, password)
30
+ end
24
31
 
25
- # POST /mintchip/payments/request
26
- def create_value1(vrm)
27
- post "/mintchip/payments/request", {"Value Request Message" => vrm}, {"Content-Type" => "application/vnd.scg.ecn-request"}
28
- end
32
+ # GET /info/{responseformat}
33
+ def info
34
+ @info ||= Info.new get "/info/json"
35
+ end
29
36
 
30
- def create_value2(payee_id, currency_name, amount_in_cents)
31
- currency_code = MintChip.currency_code(currency_name)
32
- post "/mintchip/payments/#{payee_id}/#{currency_code}/#{amount_in_cents}"
33
- end
37
+ class Info
38
+ ATTRIBUTES = %w(id currencyCode balance creditLogCount debitLogCount) +
39
+ %w(creditLogCountRemaining debitLogCountRemaining maxCreditAllowed maxDebitAllowed version)
34
40
 
35
- # GET /mintchip/payments/lastdebit
36
- def last_debit
37
- get "/mintchip/payments/lastdebit"
38
- end
41
+ attr_reader *ATTRIBUTES.map(&:underscore).map(&:to_sym)
39
42
 
40
- # GET /mintchip/payments/{startindex}/{stopindex}/{responseformat}
41
- def debit_log()
42
- get "/mintchip/payments/#{start}/#{stop}/json"
43
- end
43
+ def initialize(str)
44
+ attrs = JSON.parse(str)
45
+ ATTRIBUTES.each do |attr|
46
+ instance_variable_set("@#{attr.underscore}", attrs[attr])
47
+ end
48
+ end
49
+ end
50
+
51
+ # POST /receipts
52
+ def load_value(vm)
53
+ res = post "/receipts", vm, "application/vnd.scg.ecn-message"
54
+ # body is an empty string on success, which is gross.
55
+ res == ""
56
+ end
57
+
58
+ # POST /payments/request
59
+ def create_value1(vrm)
60
+ post "/payments/request", vrm, "application/vnd.scg.ecn-request"
61
+ end
62
+
63
+ def create_value2(payee_id, currency_name, amount_in_cents)
64
+ currency_code = Mintchip.currency_code(currency_name)
65
+ post "/payments/#{payee_id}/#{currency_code}/#{amount_in_cents}"
66
+ end
67
+
68
+ # GET /payments/lastdebit
69
+ def last_debit
70
+ get "/payments/lastdebit"
71
+ end
72
+
73
+ # GET /payments/{startindex}/{stopindex}/{responseformat}
74
+ def debit_log(start, stop)
75
+ list = JSON.parse get "/payments/#{start}/#{stop}/json"
76
+ list.map{|item| TransactionLogEntry.new item}
77
+ end
78
+
79
+ # GET /receipts/{startindex}/{stopindex}/{responseformat}
80
+ def credit_log(start, stop)
81
+ list = JSON.parse get "/receipts/#{start}/#{stop}/json"
82
+ list.map{|item| TransactionLogEntry.new item}
83
+ end
84
+
85
+ class TransactionLogEntry
86
+ ATTRIBUTES = %w(amount challenge index logType payerId payeeId transactionTime currencyCode)
87
+
88
+ attr_reader *ATTRIBUTES.map(&:underscore).map(&:to_sym)
89
+
90
+ def initialize(attrs)
91
+ ATTRIBUTES.each do |attr|
92
+ instance_variable_set("@#{attr.underscore}", attrs[attr])
93
+ end
94
+ end
95
+ end
96
+
97
+ private
98
+
99
+ def connection
100
+ uri = URI.parse RESOURCE_BASE
101
+ https = Net::HTTP.new(uri.host, uri.port)
102
+ https.use_ssl = true
103
+ https.cert = @p12.certificate
104
+ https.key = @p12.key
105
+ https.ca_file = File.expand_path("../../mintchip.pem", __FILE__)
106
+ https.verify_mode = OpenSSL::SSL::VERIFY_PEER
107
+
108
+ https
109
+ end
110
+
111
+ def get(path)
112
+ uri = URI.parse(RESOURCE_BASE + path)
113
+ req = Net::HTTP::Get.new(uri.path)
114
+ handle_response connection.start { |cx| cx.request(req) }
115
+ end
116
+
117
+ def post(path, data = {}, content_type = nil)
118
+ uri = URI.parse(RESOURCE_BASE + path)
119
+ req = Net::HTTP::Post.new(uri.path)
120
+
121
+ Hash === data ? req.set_form_data(data) : req.body = data
122
+ req.content_type = content_type if content_type
123
+
124
+ handle_response connection.start { |cx| cx.request(req) }
125
+ end
126
+
127
+ def handle_response(resp)
128
+ case resp.code.to_i
129
+ when 200 ; resp.body
130
+ when 452 ; raise Mintchip::SystemError, resp.msg
131
+ when 453 ; raise Mintchip::CryptoError, resp.msg
132
+ when 454 ; raise Mintchip::FormatError, resp.msg
133
+ when 455 ; raise Mintchip::MintchipError, resp.msg
134
+ else ; raise Mintchip::Error, resp.msg
135
+ end
136
+ end
44
137
 
45
- # GET /mintchip/receipts/{startindex}/{stopindex}/{responseformat}
46
- def ASDF
47
- get "/mintchip/receipts/#{start}/#{stop}/json"
48
138
  end
49
139
 
50
140
  end
141
+
@@ -1,3 +1,3 @@
1
1
  module Mintchip
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -0,0 +1,22 @@
1
+ -----BEGIN CERTIFICATE-----
2
+ MIIDoTCCAomgAwIBAgIBATANBgkqhkiG9w0BAQUFADB1MS4wLAYDVQQDDCVSZW1v
3
+ dGUgTWludENoaXAgQ2VydG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ywwnTtqQFp2IZO
19
+ K8/j0aAvUp1xuikOTA499xhy/OEFQUXQhKDVlPYKQT8LBnoiRRSMVhksrOq7gw3X
20
+ bO2uF4ik+R3enLBNxJj9Ltoc8Lw3mdUqnE7aU6gH0d7B3w3IbkUAQN4v4M389yoS
21
+ k8rKpuEvJAPvndrJBUFnnXGz2oi/
22
+ -----END CERTIFICATE-----
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mintchip
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:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-04-04 00:00:00.000000000 Z
12
+ date: 2012-04-11 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: ! 'Pre-Alpha: API for RCM MintChip'
15
15
  email:
@@ -26,6 +26,7 @@ files:
26
26
  - lib/mintchip.rb
27
27
  - lib/mintchip/version.rb
28
28
  - mintchip.gemspec
29
+ - mintchip.pem
29
30
  homepage: ''
30
31
  licenses: []
31
32
  post_install_message: