liberty_reserve_link 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,21 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ /.rvmrc
19
+ /.ruby-version
20
+ /.ruby-gemset
21
+ /.rspec
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in liberty_reserve_link.gemspec
4
+ gemspec
data/Guardfile ADDED
@@ -0,0 +1,24 @@
1
+ # A sample Guardfile
2
+ # More info at https://github.com/guard/guard#readme
3
+
4
+ guard :rspec do
5
+ watch(%r{^spec/.+_spec\.rb$})
6
+ watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
7
+ watch('spec/spec_helper.rb') { "spec" }
8
+
9
+ # Rails example
10
+ watch(%r{^app/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
11
+ watch(%r{^app/(.*)(\.erb|\.haml)$}) { |m| "spec/#{m[1]}#{m[2]}_spec.rb" }
12
+ watch(%r{^app/controllers/(.+)_(controller)\.rb$}) { |m| ["spec/routing/#{m[1]}_routing_spec.rb", "spec/#{m[2]}s/#{m[1]}_#{m[2]}_spec.rb", "spec/acceptance/#{m[1]}_spec.rb"] }
13
+ watch(%r{^spec/support/(.+)\.rb$}) { "spec" }
14
+ watch('config/routes.rb') { "spec/routing" }
15
+ watch('app/controllers/application_controller.rb') { "spec/controllers" }
16
+
17
+ # Capybara features specs
18
+ watch(%r{^app/views/(.+)/.*\.(erb|haml)$}) { |m| "spec/features/#{m[1]}_spec.rb" }
19
+
20
+ # Turnip features and steps
21
+ watch(%r{^spec/acceptance/(.+)\.feature$})
22
+ watch(%r{^spec/acceptance/steps/(.+)_steps\.rb$}) { |m| Dir[File.join("**/#{m[1]}.feature")][0] || 'spec/acceptance' }
23
+ end
24
+
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Oleg Bovykin
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # LibertyReserveLink
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'liberty_reserve_link'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install liberty_reserve_link
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,135 @@
1
+ # coding: utf-8
2
+
3
+ require 'digest'
4
+ require 'bigdecimal'
5
+ require 'httparty'
6
+ require 'json'
7
+
8
+ module LibertyReserveLink
9
+
10
+ class Client
11
+
12
+ class InvalidCredentialException < Exception; end
13
+
14
+ FORMAT = :json
15
+ API_HOST = "https://api2.libertyreserve.com"
16
+
17
+ include HTTParty
18
+ format FORMAT
19
+ base_uri API_HOST
20
+
21
+ attr_reader :credential
22
+
23
+ def initialize(credential)
24
+ @credential = credential
25
+ check_credential
26
+ @token = LibertyReserveLink::Token.new @credential
27
+ end
28
+
29
+ def check_credential
30
+ raise InvalidCredentialException unless @credential.valid?
31
+ end
32
+
33
+ def api_path(operation)
34
+ "/json/#{operation}"
35
+ end
36
+
37
+ def common_json
38
+ {:account => @credential.account, :api => @credential.name}
39
+ end
40
+
41
+ def parse_balance(currency, resp)
42
+ if resp["Balance"]
43
+ if currency
44
+ resp["Balance"][currency]
45
+ else
46
+ resp["Balance"]
47
+ end
48
+ else
49
+ nil
50
+ end
51
+ end
52
+
53
+ def balance(currency=nil)
54
+ id = random_id
55
+ parse_balance currency, Client.post(api_path("balance"), :query => common_json.merge({:id => id, :token => @token.balance(id)}))
56
+ end
57
+
58
+ def transfer(receiver, amount, currency, memo)
59
+ id = random_id
60
+ query = common_json.merge({
61
+ :id => id,
62
+ :token => @token.transfer(id, receiver, currency, amount),
63
+ :reference => "Reference",
64
+ :type => "transfer",
65
+ :payee => receiver,
66
+ :currency => currency,
67
+ :amount => (amount.is_a?(String) ? amount.to_f.round(2) : amount),
68
+ :memo => memo,
69
+ :purpose => "service"
70
+ })
71
+ JSON.parse Client.post(api_path("transfer"), :query => query).to_json
72
+ end
73
+
74
+ def transaction(transaction_id)
75
+ id = random_id
76
+ JSON.parse Client.post(api_path("findtransaction"), :query => common_json.merge({:token => @token.find_transaction(id, transaction_id), :id => id, :batch => transaction_id.to_s})).to_json
77
+ end
78
+
79
+ # auth error here
80
+ def name(account)
81
+ id = random_id
82
+ JSON.parse Client.post(api_path("accountname"), :query => common_json.merge({:id => id, :token => @token.balance(account), :search => account})).to_json
83
+ end
84
+
85
+ # Returns the history for an account given a currency as an array of transactions,
86
+ # see +get_transaction+ for the transaction format.
87
+ # Direction
88
+ # +direction+ can be any of :incoming, :outgoing, :any
89
+ def get_history(currency, till = DateTime.now, from = DateTime.now.advance(days: -14), options = {})
90
+ defaults = {
91
+ direction: 'any',
92
+ page_size: 20,
93
+ page_number: 1
94
+ }
95
+
96
+ opts = defaults.merge(options)
97
+
98
+ raise ArgumentError unless [:any, :outgoing, :incoming].include?(opts[:direction].to_sym)
99
+
100
+ r = send_request("history") do |xml|
101
+ xml.HistoryRequest :id => random_id do
102
+ authentication_block(xml)
103
+ xml.History do
104
+ xml.CurrencyId currency
105
+ xml.From from.strftime("%Y-%d-%m 00:00:00")
106
+ xml.Till till.strftime("%Y-%d-%m 23:59:59")
107
+ xml.CorrespondingAccountId opts[:corresponding_account_id] if opts[:corresponding_account_id]
108
+ xml.TransferType opts[:transfer_type] if opts[:transfer_type]
109
+ xml.Source opts[:source] if opts[:source]
110
+ xml.Direction opts[:direction].to_s
111
+ xml.AccountId @account
112
+ xml.Pager do |pager|
113
+ pager.PageSize opts[:page_size]
114
+ pager.PageNumber opts[:page_number]
115
+ end
116
+ end
117
+ end
118
+ end
119
+
120
+ if r["HistoryResponse"]["Pager"]["TotalCount"] != "0"
121
+ [r["HistoryResponse"]["Receipt"]].flatten.map { |t| format_transaction(t) }.compact
122
+ else
123
+ []
124
+ end
125
+ end
126
+
127
+
128
+ private
129
+
130
+ def random_id
131
+ (rand * 10 ** 9).to_i
132
+ end
133
+ end
134
+ end
135
+
@@ -0,0 +1,13 @@
1
+ # coding: utf-8
2
+
3
+ module LibertyReserveLink
4
+ class Credential < Struct.new(:account, :secret, :name)
5
+ def valid?
6
+ valid_field?(account) && valid_field?(secret) && valid_field?(name)
7
+ end
8
+
9
+ def valid_field? field
10
+ !field.nil? && !field.empty?
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,48 @@
1
+ # coding: utf-8
2
+
3
+ class LibertyReserveLink::Token
4
+ def initialize(credential)
5
+ @credential = credential
6
+ end
7
+
8
+ def timestamp
9
+ Time.now.utc.strftime("%Y%m%d:%H")
10
+ end
11
+
12
+ def secret
13
+ @credential.secret
14
+ end
15
+
16
+ def balance(id)
17
+ token = "#{secret}:#{id}:#{timestamp}"
18
+ hash token
19
+ end
20
+
21
+ def hash(token)
22
+ Digest::SHA2.hexdigest(token).upcase
23
+ end
24
+
25
+ def transfer(id, receiver, currency, amount)
26
+ token = "#{secret}:#{id}:Reference:#{receiver}:#{currency.downcase}:#{amount.is_a?(String) ? amount.to_f.round(2) : amount}:#{timestamp}"
27
+ hash token
28
+ end
29
+
30
+ def convert_date(date)
31
+ Time.utc(date.year, date.month, date.day, 0, 0, 0).strftime("%Y-%m-%d %H:%M:%S")
32
+ end
33
+
34
+ def history(id, from, till)
35
+ token = "#{secret}:#{convert_date(from)}:#{convert_date(till)}:#{timestamp}"
36
+ hash token
37
+ end
38
+
39
+ def find_transaction(id, transaction_id)
40
+ token = "#{secret}:#{id}:#{transaction_id}:#{timestamp}"
41
+ hash token
42
+ end
43
+
44
+ def confirm_transfer(id, transfer_id, confirmation_code)
45
+ token = "#{secret}:#{id}:#{transfer_id}:#{confirmation_code}:#{timestamp}"
46
+ hash token
47
+ end
48
+ end
@@ -0,0 +1,3 @@
1
+ module LibertyReserveLink
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,8 @@
1
+ require "liberty_reserve_link/version"
2
+ require "liberty_reserve_link/client"
3
+ require "liberty_reserve_link/credential"
4
+ require "liberty_reserve_link/token"
5
+
6
+ module LibertyReserveLink
7
+ # Your code goes here...
8
+ end
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'liberty_reserve_link/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "liberty_reserve_link"
8
+ spec.version = LibertyReserveLink::VERSION
9
+ spec.authors = ["Oleg Bovykin"]
10
+ spec.email = ["oleg.bovykin@gmail.com"]
11
+ spec.description = %q{Library for communicating with LibertyReserve API and SCI}
12
+ spec.summary = %q{Library for communicating with LibertyReserve API and SCI}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "rspec"
24
+ spec.add_development_dependency "guard-rspec"
25
+
26
+ spec.add_dependency "json"
27
+ spec.add_dependency "nokogiri"
28
+ spec.add_dependency "httparty"
29
+ end
@@ -0,0 +1,14 @@
1
+ # coding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ describe LibertyReserveLink::Client do
6
+ let(:credential) { LibertyReserveLink::Credential.new("U343", "secret", "name") }
7
+ let(:lr) { LibertyReserveLink::Client.new credential }
8
+ let(:common_json) { lr.common_json }
9
+
10
+ it "generates valid common json" do
11
+ expect(common_json[:api]).to eq "name"
12
+ expect(common_json[:account]).to eq "U343"
13
+ end
14
+ end
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ describe LibertyReserveLink::Credential do
6
+ context "valid?" do
7
+ let(:valid_credential) { LibertyReserveLink::Credential.new("account", "secret", "name").valid? }
8
+ let(:no_name) { LibertyReserveLink::Credential.new("account", "secret", nil).valid? }
9
+ let(:no_secret) { LibertyReserveLink::Credential.new("account", nil, "name").valid? }
10
+ let(:no_secret_and_account) { LibertyReserveLink::Credential.new(nil, nil, "name").valid? }
11
+
12
+ it "returns true if all fields present" do
13
+ expect(valid_credential).to eq true
14
+ end
15
+
16
+ it "returns false is no name" do
17
+ expect(no_name).to eq false
18
+ end
19
+
20
+ it "returns false is no secret" do
21
+ expect(no_secret).to eq false
22
+ end
23
+
24
+ it "returns false is no secret and account" do
25
+ expect(no_secret_and_account).to eq false
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ describe LibertyReserveLink::Token do
6
+ let(:credential) { LibertyReserveLink::Credential.new("account", "secret", "name") }
7
+ let(:token) { LibertyReserveLink::Token.new(credential) }
8
+
9
+ it "returns valid hash for balance" do
10
+ expect(token.balance("123").size).to eq 64
11
+ end
12
+
13
+ it "returns valid hash for transfer" do
14
+ expect(token.transfer("123", "U343", "usd", "10.0").size).to eq 64
15
+ end
16
+
17
+ it "returns valid hash for find_transaction" do
18
+ expect(token.find_transaction("123", "343").size).to eq 64
19
+ end
20
+
21
+ it "returns valid hash for history" do
22
+ expect(token.history("123", Date.today, Date.civil(2013, 1, 1)).size).to eq 64
23
+ end
24
+
25
+ it "returns valid hash for confirm_transfer" do
26
+ expect(token.confirm_transfer("id", "transfer_id", "confirmation_code").size).to eq 64
27
+ end
28
+ end
@@ -0,0 +1,6 @@
1
+ require 'rspec'
2
+ require 'liberty_reserve_link'
3
+
4
+ RSpec.configure do |c|
5
+ c.mock_with :rspec
6
+ end
metadata ADDED
@@ -0,0 +1,178 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: liberty_reserve_link
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Oleg Bovykin
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-05-21 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '1.3'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '1.3'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rake
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: rspec
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: guard-rspec
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ - !ruby/object:Gem::Dependency
79
+ name: json
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ type: :runtime
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ - !ruby/object:Gem::Dependency
95
+ name: nokogiri
96
+ requirement: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ! '>='
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ type: :runtime
103
+ prerelease: false
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ! '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ - !ruby/object:Gem::Dependency
111
+ name: httparty
112
+ requirement: !ruby/object:Gem::Requirement
113
+ none: false
114
+ requirements:
115
+ - - ! '>='
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :runtime
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ none: false
122
+ requirements:
123
+ - - ! '>='
124
+ - !ruby/object:Gem::Version
125
+ version: '0'
126
+ description: Library for communicating with LibertyReserve API and SCI
127
+ email:
128
+ - oleg.bovykin@gmail.com
129
+ executables: []
130
+ extensions: []
131
+ extra_rdoc_files: []
132
+ files:
133
+ - .gitignore
134
+ - Gemfile
135
+ - Guardfile
136
+ - LICENSE.txt
137
+ - README.md
138
+ - Rakefile
139
+ - lib/liberty_reserve_link.rb
140
+ - lib/liberty_reserve_link/client.rb
141
+ - lib/liberty_reserve_link/credential.rb
142
+ - lib/liberty_reserve_link/token.rb
143
+ - lib/liberty_reserve_link/version.rb
144
+ - liberty_reserve_link.gemspec
145
+ - spec/models/client_spec.rb
146
+ - spec/models/credential_spec.rb
147
+ - spec/models/token_spec.rb
148
+ - spec/spec_helper.rb
149
+ homepage: ''
150
+ licenses:
151
+ - MIT
152
+ post_install_message:
153
+ rdoc_options: []
154
+ require_paths:
155
+ - lib
156
+ required_ruby_version: !ruby/object:Gem::Requirement
157
+ none: false
158
+ requirements:
159
+ - - ! '>='
160
+ - !ruby/object:Gem::Version
161
+ version: '0'
162
+ required_rubygems_version: !ruby/object:Gem::Requirement
163
+ none: false
164
+ requirements:
165
+ - - ! '>='
166
+ - !ruby/object:Gem::Version
167
+ version: '0'
168
+ requirements: []
169
+ rubyforge_project:
170
+ rubygems_version: 1.8.25
171
+ signing_key:
172
+ specification_version: 3
173
+ summary: Library for communicating with LibertyReserve API and SCI
174
+ test_files:
175
+ - spec/models/client_spec.rb
176
+ - spec/models/credential_spec.rb
177
+ - spec/models/token_spec.rb
178
+ - spec/spec_helper.rb