social_wallet 1.0.0

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: fcf79bfb96e061da99dba5a68d94db841efc1993
4
+ data.tar.gz: d899934a4ea7a70bb917f8dfaa674e83bfd411e3
5
+ SHA512:
6
+ metadata.gz: 8182ffc96f927f45819f16084f7babc51a31864d65d8ce6542bd8b16d7eba42187977144ca151d96357b911ab3161906797d230ec37b62b95a47295c97c99ba5
7
+ data.tar.gz: 7fcccc9aa740fac79a6fdbf1e05d3df4aeb7065ac42c0bcfa2b8e12fbda000b7ffef3bab023f0059c4ca69354fb5997283c5e4409b0b9fbbba8c220f6844a05e
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ /test_env.yml
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,176 @@
1
+ # require: rubocop-rspec
2
+
3
+ AllCops:
4
+ Include:
5
+ - Rakefile
6
+ - config.ru
7
+ - lib/**/*.rake
8
+ - rubocop-rspec
9
+ Exclude:
10
+ - db/schema.rb
11
+ - node_modules
12
+
13
+ Rails:
14
+ Enabled: true
15
+
16
+ # Commonly used screens these days easily fit more than 80 characters.
17
+ Metrics/LineLength:
18
+ Max: 120
19
+
20
+ # Too short methods lead to extraction of single-use methods, which can make
21
+ # the code easier to read (by naming things), but can also clutter the class
22
+ Metrics/MethodLength:
23
+ Max: 20
24
+
25
+ # The guiding principle of classes is SRP, SRP can't be accurately measured by LoC
26
+ Metrics/ClassLength:
27
+ Max: 1500
28
+
29
+ Metrics/BlockLength:
30
+ Exclude:
31
+ - spec/**/*
32
+
33
+ Rails/SaveBang:
34
+ Description: 'Identifies possible cases where Active Record save! or related should be used.'
35
+ StyleGuide: 'https://github.com/bbatsov/rails-style-guide#save-bang'
36
+ Enabled: false
37
+
38
+ Style/AutoResourceCleanup:
39
+ Description: 'Suggests the usage of an auto resource cleanup version of a method (if available).'
40
+ Enabled: false
41
+
42
+ # No space makes the method definition shorter and differentiates
43
+ # from a regular assignment.
44
+ Layout/SpaceAroundEqualsInParameterDefault:
45
+ EnforcedStyle: no_space
46
+
47
+ # Single quotes being faster is hardly measurable and only affects parse time.
48
+ # Enforcing double quotes reduces the times where you need to change them
49
+ # when introducing an interpolation. Use single quotes only if their semantics
50
+ # are needed.
51
+ Style/StringLiterals:
52
+ EnforcedStyle: single_quotes
53
+
54
+ # We do not need to support Ruby 1.9, so this is good to use.
55
+ Style/SymbolArray:
56
+ Enabled: true
57
+
58
+ # Most readable form.
59
+ Layout/AlignHash:
60
+ EnforcedHashRocketStyle: table
61
+ EnforcedColonStyle: table
62
+
63
+ # Mixing the styles looks just silly.
64
+ Style/HashSyntax:
65
+ EnforcedStyle: ruby19_no_mixed_keys
66
+
67
+ # has_key? and has_value? are far more readable than key? and value?
68
+ Style/PreferredHashMethods:
69
+ Enabled: false
70
+
71
+ # String#% is by far the least verbose and only object oriented variant.
72
+ Style/FormatString:
73
+ EnforcedStyle: percent
74
+
75
+ Style/CollectionMethods:
76
+ Enabled: true
77
+ PreferredMethods:
78
+ # inject seems more common in the community.
79
+ reduce: "inject"
80
+
81
+
82
+ # Either allow this style or don't. Marking it as safe with parenthesis
83
+ # is silly. Let's try to live without them for now.
84
+ Style/ParenthesesAroundCondition:
85
+ AllowSafeAssignment: false
86
+ Lint/AssignmentInCondition:
87
+ AllowSafeAssignment: false
88
+
89
+ # A specialized exception class will take one or more arguments and construct the message from it.
90
+ # So both variants make sense.
91
+ Style/RaiseArgs:
92
+ Enabled: false
93
+
94
+ # Indenting the chained dots beneath each other is not supported by this cop,
95
+ # see https://github.com/bbatsov/rubocop/issues/1633
96
+ Layout/MultilineOperationIndentation:
97
+ Enabled: false
98
+
99
+ # Fail is an alias of raise. Avoid aliases, it's more cognitive load for no gain.
100
+ # The argument that fail should be used to abort the program is wrong too,
101
+ # there's Kernel#abort for that.
102
+ Style/SignalException:
103
+ EnforcedStyle: only_raise
104
+
105
+ # Suppressing exceptions can be perfectly fine, and be it to avoid to
106
+ # explicitly type nil into the rescue since that's what you want to return,
107
+ # or suppressing LoadError for optional dependencies
108
+ Lint/HandleExceptions:
109
+ Enabled: false
110
+
111
+ Layout/SpaceInsideBlockBraces:
112
+ SpaceBeforeBlockParameters: true
113
+
114
+ # No trailing space differentiates better from the block:
115
+ # foo} means hash, foo } means block.
116
+ Layout/SpaceInsideHashLiteralBraces:
117
+ EnforcedStyle: space
118
+
119
+ # { ... } for multi-line blocks is okay, follow Weirichs rule instead:
120
+ # https://web.archive.org/web/20140221124509/http://onestepback.org/index.cgi/Tech/Ruby/BraceVsDoEnd.rdoc
121
+ Style/BlockDelimiters:
122
+ Enabled: false
123
+
124
+ # do / end blocks should be used for side effects,
125
+ # methods that run a block for side effects and have
126
+ # a useful return value are rare, assign the return
127
+ # value to a local variable for those cases.
128
+ Style/MethodCalledOnDoEndBlock:
129
+ Enabled: true
130
+
131
+ # Enforcing the names of variables? To single letter ones? Just no.
132
+ Style/SingleLineBlockParams:
133
+ Enabled: false
134
+
135
+ # Shadowing outer local variables with block parameters is often useful
136
+ # to not reinvent a new name for the same thing, it highlights the relation
137
+ # between the outer variable and the parameter. The cases where it's actually
138
+ # confusing are rare, and usually bad for other reasons already, for example
139
+ # because the method is too long.
140
+ Lint/ShadowingOuterLocalVariable:
141
+ Enabled: false
142
+
143
+ # Check with yard instead.
144
+ Style/Documentation:
145
+ Enabled: false
146
+
147
+ # This is just silly. Calling the argument `other` in all cases makes no sense.
148
+ Naming/BinaryOperatorParameterName:
149
+ Enabled: false
150
+
151
+ # There are valid cases, for example debugging Cucumber steps,
152
+ # also they'll fail CI anyway
153
+ Lint/Debugger:
154
+ Enabled: false
155
+
156
+ # Style preference
157
+ Style/MethodDefParentheses:
158
+ Enabled: false
159
+
160
+ # Allow me to write quantità
161
+ Style/AsciiComments:
162
+ Enabled: false
163
+
164
+ # This will have to be true with Ruby 3.0+
165
+ Style/FrozenStringLiteralComment:
166
+ Enabled: false
167
+
168
+ # I want
169
+ Style/RegexpLiteral:
170
+ EnforcedStyle: slashes
171
+
172
+ Style/GlobalVars:
173
+ Exclude:
174
+ - 'config/initializers/redis.rb'
175
+ - 'features/support/hooks.rb'
176
+ - 'node_modules/node-sass/src/libsass/extconf.rb'
@@ -0,0 +1 @@
1
+ 2.3.3
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.1
5
+ before_install: gem install bundler -v 1.13.6
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in social_wallet.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2018 Commonfare.net
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,263 @@
1
+ # SocialWallet
2
+
3
+ A simple ruby client for the [Social Wallet API](https://github.com/Commonfare-net/social-wallet-api). It supports both the backends of the SWAPI: the *database* (e.g. `mongo`), for local transactions within the wallet, and the *blockchain* (e.g. `faircoin`), for transactions between the wallet addresses and public addresses on a blockchain.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'social_wallet'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install social_wallet
20
+
21
+ ## Usage
22
+
23
+ ### Create the client
24
+
25
+ The default client uses the *database* `mongo` as backend.
26
+
27
+ ```ruby
28
+ client = SocialWallet::Client.new(api_endpoint: 'http://example.com/wallet/v1')
29
+ ```
30
+
31
+ If you want to use the API on a *blockchain* just specify it like this:
32
+
33
+ ```ruby
34
+ client = SocialWallet::Client.new(api_endpoint: 'http://example.com/wallet/v1', blockchain: 'faircoin')
35
+ ```
36
+
37
+ ### List of tags
38
+
39
+ Retrieve the list of tags of the *database* backend.
40
+
41
+ ```ruby
42
+ client.tags.list
43
+ #=> { "tags" => [{ "tag"=>"something", "count"=>1, "amount"=>0.1, "created-by"=>"test-1", "created"=>"2018-02-01T10:58:10.728" }, ...]
44
+ ```
45
+
46
+ ### Transactions
47
+
48
+ Always use `account_id` when managing transactions.
49
+
50
+ #### New
51
+
52
+ This call is *database-only*, for transactions on the blockchain please refer to [Withdraws](#withdraws) and [Deposits](#deposits).
53
+
54
+ Move some amount from one account (`paolo`) to another account (`aaron`)
55
+
56
+ ```ruby
57
+ client.transactions.new(from_id: 'paolo', to_id: 'aaron', amount: 10, tags: ['tag1', 'tag2'])
58
+ ```
59
+
60
+ #### List
61
+
62
+ Retrieve the list of the transactions of a specific account.
63
+
64
+ ```ruby
65
+ client.transactions(account_id: 'pietro').list
66
+ ```
67
+
68
+ Retrieve the list of the transactions of the default account.
69
+
70
+ ```ruby
71
+ client.transactions(account_id: '').list
72
+ ```
73
+
74
+ **IMPORTANT**: the format of the response varies according to the backend used (e.g. `faircoin` or `mongo`).
75
+
76
+ #### Get
77
+
78
+ Retrieve info about a specific transaction.
79
+
80
+ ```ruby
81
+ client.transactions.get(transaction_id: 'transaction_id')
82
+ ```
83
+
84
+ **Response**
85
+
86
+ The schema of the response varies according to the backend used (e.g. `faircoin` or `mongo`).
87
+
88
+ On *database*:
89
+
90
+ ```ruby
91
+ client.transactions.get transaction_id: 'xqc4cvhr...pCPfju5inCO'
92
+ #=>
93
+ {
94
+ 'tags'=>['tag1', 'tag2'],
95
+ 'timestamp'=>'2018-02-23T20:10:01.331',
96
+ 'from-id'=>'pietro',
97
+ 'to-id'=>'aaron',
98
+ 'amount'=>10,
99
+ 'transaction-id'=>'xqc4cvhr...pCPfju5inCO',
100
+ 'currency'=>'MONGO'
101
+ }
102
+ ```
103
+
104
+ On *blockchain*
105
+
106
+ ```ruby
107
+ client.transactions.get transaction_id: 'f747a7870ce82385802705...bf2cc219cfe08'
108
+ #=>
109
+ {
110
+ 'confirmations'=>10452,
111
+ 'hex'=>
112
+ '010000000...a88acc6790100',
113
+ 'walletconflicts'=>[],
114
+ 'blockhash'=>'132f223a466...dfa5c0c1d02c876ab4e1',
115
+ 'time'=>1517832065,
116
+ 'amount'=>0.1,
117
+ 'details'=>[
118
+ {'account'=>'',
119
+ 'address'=>'fEGhnSZWB...84yCsvEbS4',
120
+ 'category'=>'receive',
121
+ 'amount'=>0.1,
122
+ 'label'=>'',
123
+ 'vout'=>0}
124
+ ],
125
+ 'bip125-replaceable'=>'no',
126
+ 'blocktime'=>1517832033,
127
+ 'timereceived'=>1517832065,
128
+ 'blockindex'=>1,
129
+ 'txid'=>'f747a7870ce82385802705...c7d93969f4edbf2cc219cfe08'
130
+ }
131
+ ```
132
+
133
+ ### Balance
134
+
135
+ Balance of a specific account:
136
+
137
+ ```ruby
138
+ client.balance(account_id: 'pietro')
139
+ #=> { 'amount' => 42 }
140
+ ```
141
+
142
+ Balance of the default account:
143
+
144
+ ```ruby
145
+ client.balance(account_id: '')
146
+ #=> { 'amount' => -84.24 }
147
+ ```
148
+
149
+ Total balance of the wallet:
150
+
151
+ ```ruby
152
+ client.balance
153
+ #=> { 'amount' => -42.24 }
154
+ ```
155
+
156
+ ### Label
157
+
158
+ Retrieve the label of the currency for the client's backend
159
+
160
+ ```ruby
161
+ client.label
162
+ #=> { 'currency' => 'MONGO' }
163
+ ```
164
+
165
+ ### Address
166
+
167
+ Retrieve a list of addresses for a specific account.
168
+
169
+ ```ruby
170
+ client.address(account_id: account_id)
171
+ ```
172
+
173
+ Retrieve a list of addresses for the default account.
174
+
175
+ ```ruby
176
+ client.address
177
+ ```
178
+
179
+ <span id="withdraws"></span>
180
+
181
+ ### Withdraws
182
+
183
+ This call withdraws an amount from the default account `''` or optionally a given `from_wallet_account` to a provided *blockchain* address (`to_address`). Also a transaction on the *database* will be registered. If fees apply for this transaction those fees will be added to the amount on the *database* when the transaction reaches the required amount of confirmations (configured in the wallet).
184
+
185
+ `Withdraws` are *blockchain-only*.
186
+
187
+ ```ruby
188
+ client.withdraws.new(
189
+ from_id: '',
190
+ from_wallet_account: '',
191
+ to_address: '',
192
+ amount: 10,
193
+ tags: ['tag1', 'tag2'],
194
+ comment: '',
195
+ comment_to: ''
196
+ )
197
+ ```
198
+
199
+ **Some details**
200
+
201
+ * `from_wallet_account` is the (optional) *blockchain* address among those that exist inside the wallet. When set to `''`, the default address of the wallet is used.
202
+ * `from_id` is the (optional) `account_id` inside the wallet from which the transaction originates. Once the withdraw is confirmed, the transaction registered on the database has this `from_id`.
203
+
204
+ <span id="deposits"></span>
205
+
206
+ ### Deposits
207
+
208
+ `Deposits` are *blockchain-only*.
209
+
210
+ #### New
211
+
212
+ Returns an address onto which the coins should be received. The `to_id` is the (optional) `account_id` within the wallet which receives the coins.
213
+
214
+ ```ruby
215
+ client.deposits.new(to_id: '', to_wallet_id: '', tags: ['tag1', 'tag2'])
216
+ ```
217
+
218
+ When `to_wallet_id` is used it will create the address for a particular account in the wallet and the default otherwise. If the account is not found, the address will be created on the default account.
219
+
220
+ #### Check
221
+
222
+ Check the status of the deposit to the specified `address`.
223
+
224
+ ```ruby
225
+ client.deposits.check(address: 'address')
226
+ ```
227
+
228
+ ### Summary
229
+
230
+ This table summarizes which are the available methods for the different backends.
231
+
232
+ | method | database | blockchain |
233
+ |--------------------------|:--------:|:----------:|
234
+ | `tags.list` | ✅ | 🚫 |
235
+ | `transactions(...).new` | ✅ | 🚫 |
236
+ | `transactions(...).list` | ✅ | ✅ |
237
+ | `transactions(...).get` | ✅ | ✅ |
238
+ | `balance` | ✅ | ✅ |
239
+ | `label` | ✅ | ✅ |
240
+ | `address` | 🚫 | ✅ |
241
+ | `withdraws.new` | 🚫 | ✅ |
242
+ | `deposits.new` | 🚫 | ✅ |
243
+ | `deposits.check` | 🚫 | ✅ |
244
+
245
+ Using a method on the wrong backend will raise a `SocialWallet::Error`.
246
+
247
+ ## Development
248
+
249
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
250
+
251
+ Before running tests, copy the file `test_env.yml.example` to `test_env.yml` and fill that file with the appropriate values.
252
+
253
+ <!-- To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org). -->
254
+
255
+
256
+ ## Acknowledgements
257
+
258
+ The Social Wallet gem is Free and Open Source research and development activity funded by the European Commission in the context of the [Collective Awareness Platforms for Sustainability and Social Innovation (CAPSSI)](https://ec.europa.eu/digital-single-market/en/collective-awareness) program. Social Wallet gem uses the [Social Wallet API](https://github.com/Commonfare-net/social-wallet-api) and it has been adopted as a component of the [Commonfare platform](https://commonfare.net) being developed for
259
+ the [Commonfare - PIE News project](https://pieproject.eu) (grant nr. 687922).
260
+
261
+ ## Contributing
262
+
263
+ Bug reports and pull requests are welcome on GitHub at https://github.com/Commonfare-net/social_wallet_ruby.
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "social_wallet"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,9 @@
1
+ require 'faraday'
2
+ require 'multi_json'
3
+
4
+ require 'social_wallet/version'
5
+ require 'social_wallet/error'
6
+ require 'social_wallet/client'
7
+
8
+ module SocialWallet
9
+ end
@@ -0,0 +1,203 @@
1
+ module SocialWallet
2
+ class Client
3
+ attr_accessor :api_endpoint, :blockchain, :request_data
4
+
5
+ SW_ERROR_STATUSES = [404, 503].freeze
6
+
7
+ def initialize(api_endpoint: nil, blockchain: 'mongo')
8
+ @api_endpoint = api_endpoint
9
+ @blockchain = blockchain
10
+ @request_data = { blockchain: blockchain }
11
+ @path_parts = []
12
+ end
13
+
14
+ # def method_missing(method, *args)
15
+ # # To support underscores, we replace them with hyphens when calling the API
16
+ # @path_parts << method.to_s.gsub("_", "-").downcase
17
+ # @path_parts << args if args.length > 0
18
+ # @path_parts.flatten!
19
+ # self
20
+ # end
21
+
22
+ # def respond_to_missing?(method_name, include_private = false)
23
+ # true
24
+ # end
25
+
26
+ protected
27
+
28
+ def ___transactions(account_id: nil)
29
+ @account_id = account_id
30
+ self
31
+ end
32
+
33
+ def ___tags
34
+ self
35
+ end
36
+
37
+ def ___withdraws
38
+ self
39
+ end
40
+
41
+ def ___deposits
42
+ self
43
+ end
44
+
45
+ def ___balance(account_id: nil)
46
+ conn = Faraday.new(url: api_endpoint + '/' + path)
47
+ response = conn.post do |req|
48
+ req.headers['Content-Type'] = 'application/json'
49
+ request_data['account-id'] = account_id if account_id
50
+ req.body = MultiJson.dump(request_data)
51
+ end
52
+ format_response(response)
53
+ ensure
54
+ reset
55
+ end
56
+
57
+ def ___label
58
+ conn = Faraday.new(url: api_endpoint + '/' + path)
59
+ response = conn.post do |req|
60
+ req.headers['Content-Type'] = 'application/json'
61
+ req.body = MultiJson.dump(request_data)
62
+ end
63
+ format_response(response)
64
+ ensure
65
+ reset
66
+ end
67
+
68
+ def ___address(account_id: '')
69
+ conn = Faraday.new(url: api_endpoint + '/' + path)
70
+ response = conn.post do |req|
71
+ req.headers['Content-Type'] = 'application/json'
72
+ request_data['account-id'] = account_id
73
+ req.body = MultiJson.dump(request_data)
74
+ end
75
+ format_response(response)
76
+ ensure
77
+ reset
78
+ end
79
+
80
+ # def move(from_id: nil, to_id: nil, amount: 0, tags: [])
81
+ # @path_parts << 'move'
82
+ # conn = Faraday.new(url: api_endpoint + '/' + path)
83
+ # response = conn.post do |req|
84
+ # req.headers['Content-Type'] = 'application/json'
85
+ # request_data['from-id'] = (from_id ||= @account_id)
86
+ # request_data['to-id'] = to_id
87
+ # request_data[:amount] = amount
88
+ # request_data[:tags] = tags
89
+ # req.body = MultiJson.dump(request_data)
90
+ # end
91
+ # format_response(response)
92
+ # ensure
93
+ # reset
94
+ # end
95
+
96
+ def ___new(
97
+ from_id: nil,
98
+ to_id: nil,
99
+ amount: 0,
100
+ from_wallet_account: '',
101
+ to_wallet_id: '',
102
+ to_address: '',
103
+ comment: '',
104
+ comment_to: '',
105
+ tags: []
106
+ )
107
+ conn = Faraday.new(url: api_endpoint + '/' + path)
108
+ response = conn.post do |req|
109
+ req.headers['Content-Type'] = 'application/json'
110
+ request_data[:amount] = amount
111
+ request_data[:tags] = tags
112
+ # TODO: find a better way to handle this
113
+ if @path_parts.include? 'withdraws'
114
+ request_data['from-id'] = from_id
115
+ request_data['from-wallet-account'] = from_wallet_account
116
+ request_data['to-address'] = to_address
117
+ request_data['comment'] = comment
118
+ request_data['commentto'] = comment_to
119
+ elsif @path_parts.include? 'deposits'
120
+ request_data['to-id'] = to_id
121
+ request_data['to-wallet-id'] = to_wallet_id
122
+ elsif @path_parts.include? 'transactions'
123
+ request_data['from-id'] = from_id
124
+ request_data['to-id'] = to_id
125
+ end
126
+ req.body = MultiJson.dump(request_data)
127
+ end
128
+ format_response(response)
129
+ ensure
130
+ reset
131
+ end
132
+
133
+ def ___list
134
+ conn = Faraday.new(url: api_endpoint + '/' + path)
135
+ response = conn.post do |req|
136
+ req.headers['Content-Type'] = 'application/json'
137
+ request_data['account-id'] = @account_id if @path_parts.include?('transactions')
138
+ req.body = MultiJson.dump(request_data)
139
+ end
140
+ format_response(response)
141
+ ensure
142
+ reset
143
+ end
144
+
145
+ def ___get(transaction_id: nil)
146
+ conn = Faraday.new(url: api_endpoint + '/' + path)
147
+ response = conn.post do |req|
148
+ req.headers['Content-Type'] = 'application/json'
149
+ request_data[:txid] = transaction_id
150
+ req.body = MultiJson.dump(request_data)
151
+ end
152
+ format_response(response)
153
+ ensure
154
+ reset
155
+ end
156
+
157
+ def ___check(address: '')
158
+ conn = Faraday.new(url: api_endpoint + '/' + path)
159
+ response = conn.post do |req|
160
+ req.headers['Content-Type'] = 'application/json'
161
+ request_data[:address] = address
162
+ req.body = MultiJson.dump(request_data)
163
+ end
164
+ format_response(response)
165
+ ensure
166
+ reset
167
+ end
168
+
169
+ public
170
+
171
+ # This creates the public methods and builds the path
172
+ protected_instance_methods.each do |protected_method_name|
173
+ next unless protected_method_name.to_s.start_with?('___')
174
+ public_method_name = protected_method_name.to_s.gsub('___', '')
175
+ define_method(public_method_name.to_sym) do |*args|
176
+ @path_parts << public_method_name.to_s
177
+ send protected_method_name, *args
178
+ end
179
+ end
180
+
181
+ private
182
+
183
+ def path
184
+ @path_parts.join('/')
185
+ end
186
+
187
+ def reset
188
+ @path_parts = []
189
+ @request_data = { blockchain: blockchain }
190
+ end
191
+
192
+ def format_response(response)
193
+ if response.status == 200
194
+ MultiJson.load(response.body)
195
+ elsif SW_ERROR_STATUSES.include? response.status
196
+ err_msg = MultiJson.load(response.body)['error']
197
+ raise SocialWallet::Error.new(err_msg)
198
+ else
199
+ raise SocialWallet::Error.new("API Error: #{response.body}")
200
+ end
201
+ end
202
+ end
203
+ end
@@ -0,0 +1,3 @@
1
+ module SocialWallet
2
+ class Error < StandardError; end
3
+ end
@@ -0,0 +1,3 @@
1
+ module SocialWallet
2
+ VERSION = "1.0.0"
3
+ end
@@ -0,0 +1,43 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'social_wallet/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'social_wallet'
8
+ spec.version = SocialWallet::VERSION
9
+ spec.authors = ['pbmolini']
10
+ spec.email = ['pbmolini@fbk.eu']
11
+
12
+ spec.summary = %q{A simple client for the Social Wallet API}
13
+ spec.description = %q{A simple client for the Social Wallet API}
14
+ spec.homepage = 'https://github.com/Commonfare-net/social_wallet_ruby'
15
+
16
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
17
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
18
+ if spec.respond_to?(:metadata)
19
+ spec.metadata['allowed_push_host'] = "https://rubygems.org"
20
+ else
21
+ raise "RubyGems 2.0 or newer is required to protect against " \
22
+ "public gem pushes."
23
+ end
24
+
25
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
26
+ f.match(%r{^(test|spec|features)/})
27
+ end
28
+ spec.bindir = 'exe'
29
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
30
+ spec.require_paths = ['lib']
31
+
32
+ spec.add_dependency('faraday', '>= 0.9.1')
33
+ spec.add_dependency('multi_json', '>= 1.11.0')
34
+
35
+ spec.add_development_dependency 'bundler', '~> 1.13'
36
+ spec.add_development_dependency 'rake', '~> 10.0'
37
+ spec.add_development_dependency 'rspec', '~> 3.0'
38
+
39
+ spec.add_development_dependency 'pry'
40
+
41
+ spec.add_development_dependency 'rubocop'
42
+ spec.add_development_dependency 'rubocop-rspec'
43
+ end
@@ -0,0 +1,4 @@
1
+ # Before running tests, copy this file to `test_env.yml`
2
+ # and fill that file with the appropriate values
3
+ TEST_API_ENDPOINT: http://example.com/wallet/v1
4
+ TEST_API_KEY: your_api_key # not used in this release
metadata ADDED
@@ -0,0 +1,173 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: social_wallet
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - pbmolini
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-02-27 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: faraday
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 0.9.1
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 0.9.1
27
+ - !ruby/object:Gem::Dependency
28
+ name: multi_json
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 1.11.0
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: 1.11.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.13'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.13'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: pry
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rubocop
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: rubocop-rspec
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ description: A simple client for the Social Wallet API
126
+ email:
127
+ - pbmolini@fbk.eu
128
+ executables: []
129
+ extensions: []
130
+ extra_rdoc_files: []
131
+ files:
132
+ - ".gitignore"
133
+ - ".rspec"
134
+ - ".rubocop.yml"
135
+ - ".ruby-version"
136
+ - ".travis.yml"
137
+ - Gemfile
138
+ - LICENSE
139
+ - README.md
140
+ - Rakefile
141
+ - bin/console
142
+ - bin/setup
143
+ - lib/social_wallet.rb
144
+ - lib/social_wallet/client.rb
145
+ - lib/social_wallet/error.rb
146
+ - lib/social_wallet/version.rb
147
+ - social_wallet.gemspec
148
+ - test_env.yml.example
149
+ homepage: https://github.com/Commonfare-net/social_wallet_ruby
150
+ licenses: []
151
+ metadata:
152
+ allowed_push_host: https://rubygems.org
153
+ post_install_message:
154
+ rdoc_options: []
155
+ require_paths:
156
+ - lib
157
+ required_ruby_version: !ruby/object:Gem::Requirement
158
+ requirements:
159
+ - - ">="
160
+ - !ruby/object:Gem::Version
161
+ version: '0'
162
+ required_rubygems_version: !ruby/object:Gem::Requirement
163
+ requirements:
164
+ - - ">="
165
+ - !ruby/object:Gem::Version
166
+ version: '0'
167
+ requirements: []
168
+ rubyforge_project:
169
+ rubygems_version: 2.5.2
170
+ signing_key:
171
+ specification_version: 4
172
+ summary: A simple client for the Social Wallet API
173
+ test_files: []