safecrow 0.1.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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: d687db09d7b23752c09c08b63dd237ffb4ab5ba778b682a72d76c4783d4b61b9
4
+ data.tar.gz: cddb226c3be1939c4957d663b0c4cd07a80740f119b1bd82a1f766815372d9af
5
+ SHA512:
6
+ metadata.gz: 6c23e958609ee71ec0b512e27d8e27ee7b2b4468e3771932ed06b3211b7cac55461c301307f57b94af31489bbae2ef79f7bbeda7d6f3fe173361c8492b1d6617
7
+ data.tar.gz: 14f558f1f281b6c78e5a652decba2f3b4503d94922caa74a2cb476f5ff0f1796f15144e1c53bc953a2b6f4d3a26d1e4ab81b39bbb8b340a84b0d8f836085e227
data/.gitignore ADDED
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
12
+ .idea
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.4.0
7
+ before_install: gem install bundler -v 1.16.3
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in safecrow.gemspec
6
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,41 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ safecrow (0.1.0)
5
+ curb
6
+ multi_json
7
+ oj
8
+
9
+ GEM
10
+ remote: https://rubygems.org/
11
+ specs:
12
+ curb (0.9.6)
13
+ diff-lcs (1.3)
14
+ multi_json (1.13.1)
15
+ oj (3.6.6)
16
+ rake (10.5.0)
17
+ rspec (3.8.0)
18
+ rspec-core (~> 3.8.0)
19
+ rspec-expectations (~> 3.8.0)
20
+ rspec-mocks (~> 3.8.0)
21
+ rspec-core (3.8.0)
22
+ rspec-support (~> 3.8.0)
23
+ rspec-expectations (3.8.1)
24
+ diff-lcs (>= 1.2.0, < 2.0)
25
+ rspec-support (~> 3.8.0)
26
+ rspec-mocks (3.8.0)
27
+ diff-lcs (>= 1.2.0, < 2.0)
28
+ rspec-support (~> 3.8.0)
29
+ rspec-support (3.8.0)
30
+
31
+ PLATFORMS
32
+ ruby
33
+
34
+ DEPENDENCIES
35
+ bundler (~> 1.16)
36
+ rake (~> 10.0)
37
+ rspec (~> 3.2)
38
+ safecrow!
39
+
40
+ BUNDLED WITH
41
+ 1.16.3
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 McAlex
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
13
+ all 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
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,43 @@
1
+ # Safecrow
2
+
3
+ This is SDK for Safecrow API
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'safecrow'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install safecrow
20
+
21
+ ## Usage
22
+
23
+ First, configure api-key and api-secret in config/initializers/safecrow.rb
24
+
25
+ ```ruby
26
+ Safecrow.method_name(params).
27
+ ```
28
+ To all availible methods see lib/safecrow.rb and https://github.com/safecrow/docs-apiv3
29
+
30
+
31
+ ## Development
32
+
33
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `URL='url' APIKEY='apikey' APISECRET='apisecret' PREFIX='/api/v3' rspec` to run the tests.
34
+
35
+ 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).
36
+
37
+ ## Contributing
38
+
39
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/safecrow. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
40
+
41
+ ## License
42
+
43
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -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
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "safecrow"
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(__FILE__)
data/bin/setup ADDED
@@ -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,14 @@
1
+ require 'rails/generators/base'
2
+ require 'rails/generators/active_record'
3
+
4
+ module Safecrow
5
+ module Generators
6
+ class InstallGenerator < Rails::Generators::Base
7
+ source_root File.expand_path('../templates', __FILE__)
8
+
9
+ def create_safecrow_initializer
10
+ copy_file 'safecrow.rb', 'config/initializers/safecrow.rb'
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,6 @@
1
+ Safecrow.configure do |config|
2
+ config.url = "https://dev.safecrow.ru"
3
+ config.apikey = "INSERT YOUR API KEY HERE"
4
+ config.apisecret = "INSERT YOUR API SECRET HERE"
5
+ config.prefix = "/api/v3"
6
+ end
@@ -0,0 +1,14 @@
1
+ module Safecrow
2
+ class Configuration
3
+ attr_accessor :url, :apikey, :apisecret, :prefix
4
+
5
+ def initialize
6
+ @url = nil
7
+ @apikey = nil
8
+ @apisecret = nil
9
+ @prefix = nil
10
+ end
11
+ end
12
+ end
13
+
14
+ #URL='https://dev.safecrow.ru' APIKEY='414b7881-cf46-4af5-8c08-66e1f383cfa3' APISECRET='238f922e7bbbae58cec3d4774611a2fe70de31bec3068c0c5d6784800ee88c67' PREFIX='/api/v3' rspec
@@ -0,0 +1,3 @@
1
+ module Safecrow
2
+ VERSION = "0.1.0"
3
+ end
data/lib/safecrow.rb ADDED
@@ -0,0 +1,212 @@
1
+ require 'json'
2
+ require 'curb'
3
+ require 'openssl'
4
+ require "safecrow/version"
5
+ require "safecrow/configuration"
6
+
7
+ module Safecrow
8
+ class << self
9
+ attr_accessor :configuration
10
+ end
11
+
12
+ def self.configuration
13
+ @configuration ||= Configuration.new
14
+ end
15
+
16
+ def self.reset
17
+ @configuration = Configuration.new
18
+ end
19
+
20
+ def self.configure
21
+ yield(configuration)
22
+ end
23
+
24
+ def self.set_settings(url)
25
+ endpoint = '/settings'
26
+ payload = {callback_url: url}
27
+ make_request(endpoint, payload, 'POST')
28
+ end
29
+
30
+ # return id
31
+ def self.create_user(email, phone, name)
32
+ endpoint = '/users'
33
+ payload = { email: email, phone: phone.delete('(').delete(')').delete('-').delete(' '), name: name }
34
+ make_request(endpoint, payload, 'POST')['id']
35
+ end
36
+
37
+ # return id
38
+ def self.find_user(email)
39
+ endpoint = "/users?email=#{email}"
40
+ (make_request(endpoint, {}, 'GET').first || {})['id']
41
+ end
42
+
43
+ def self.get_user_by_id(sc_user_id)
44
+ endpoint = "/users/#{sc_user_id}"
45
+ make_request(endpoint, {}, 'GET')
46
+ end
47
+
48
+ # return id
49
+ def self.edit_user(sc_id, phone, name)
50
+ endpoint = "/users/#{sc_id}"
51
+ payload = { phone: phone.delete('(').delete(')').delete('-').delete(' '),
52
+ name: name }
53
+ make_request(endpoint, payload, 'POST')['id']
54
+ end
55
+
56
+ # return "redirect_url"
57
+ def self.add_card(user_id, url)
58
+ endpoint = "/users/#{user_id}/cards"
59
+ payload = { redirect_url: url }
60
+ make_request(endpoint, payload, 'POST')['redirect_url']
61
+ end
62
+
63
+ # return [{ number: '', id: '' }]
64
+ def self.get_user_cards(user_id)
65
+ endpoint = "/users/#{user_id}/cards"
66
+ make_request(endpoint, {}, 'GET').map do |card|
67
+ { number: card['card_number'], id: card['id'] }
68
+ end
69
+ end
70
+
71
+ # return {"price"=>123, "supplier_service_cost"=>4, "consumer_service_cost"=>0}
72
+ def self.calculate(price)
73
+ endpoint = '/calculate'
74
+ payload = {
75
+ price: price,
76
+ service_cost_payer: 'supplier',
77
+ #consumer_cancellation_cost: 0
78
+ }
79
+ make_request(endpoint, payload, 'POST')
80
+ end
81
+
82
+ # return id
83
+ def self.create_order(consumer_id, supplier_id, price, delivery_cost, delivery_cost_payer = 'consumer')
84
+ endpoint = '/orders'
85
+ payload = {
86
+ consumer_id: consumer_id,
87
+ supplier_id: supplier_id,
88
+ price: price*100, # в копьях
89
+ description: 'description',
90
+ service_cost_payer: 'supplier',
91
+ delivery_cost: delivery_cost*100,
92
+ delivery_cost_payer: delivery_cost_payer,
93
+ }
94
+ make_request(endpoint, payload, 'POST')['id']
95
+ end
96
+ # return {"id"=>9529, "consumer_id"=>4315, "supplier_id"=>4315, "price"=>45000, "consumer_service_cost"=>0, "supplier_service_cost"=>1800, "consumer_delivery_cost"=>0, "supplier_delivery_cost"=>0, "consumer_cancellation_cost"=>0, "discount"=>0, "description"=>"description", "status"=>"pending", "supplier_payout_method_id"=>1748, "supplier_payout_method_type"=>"CreditCard", "created_at"=>"2018-07-13T11:18:25+03:00", "updated_at"=>"2018-07-13T11:21:59+03:00", "extra"=>{}}
97
+ def self.get_order(order_id)
98
+ endpoint = "/orders/#{order_id}"
99
+ make_request(endpoint, {}, 'GET')
100
+ end
101
+
102
+ # return some array with hashes with ids and etc...
103
+ def self.get_orders(user_id)
104
+ endpoint = "/users/#{user_id}/orders"
105
+ make_request(endpoint, {}, 'GET')
106
+ end
107
+
108
+ # return some hash with ids and etc...
109
+ def self.annul_order(order_id, reason = 'Some reason')
110
+ endpoint = "/orders/#{order_id}/annul"
111
+ payload = { reason: reason }
112
+ make_request(endpoint, payload, 'POST')
113
+ end
114
+
115
+ # return payment_url
116
+ def self.pay_order(order_id, redirect_url)
117
+ endpoint = "/orders/#{order_id}/pay"
118
+ payload = { redirect_url: redirect_url }
119
+ make_request(endpoint, payload, 'POST')['payment_url']
120
+ end
121
+
122
+ # return "redirect_url"
123
+ def self.payout_card(user_id, order_id, supplier_payout_card_id)
124
+ endpoint = "/users/#{user_id}/orders/#{order_id}"
125
+ payload = { supplier_payout_card_id: supplier_payout_card_id }
126
+ make_request(endpoint, payload, 'POST')
127
+ end
128
+
129
+ # return some hash with ids and etc...
130
+ def self.cancel_order(order_id, reason = 'Some reason')
131
+ endpoint = "/orders/#{order_id}/cancel"
132
+ payload = { reason: reason }
133
+ make_request(endpoint, payload, 'POST')
134
+ end
135
+
136
+ # return some hash with ids and etc...
137
+ def self.close_order(order_id, reason = 'Some reason', discount = 0)
138
+ endpoint = "/orders/#{order_id}/close"
139
+ payload = {
140
+ reason: reason,
141
+ discount: discount*100
142
+ }
143
+ make_request(endpoint, payload, 'POST')
144
+ end
145
+
146
+ # return some hash with ids and etc...
147
+ def self.escalate_order(order_id, reason = 'Some reason')
148
+ endpoint = "/orders/#{order_id}/escalate"
149
+ payload = { reason: reason }
150
+ make_request(endpoint, payload, 'POST')
151
+ end
152
+
153
+ # return {"redirect_url"=>"", "consumer_pay"=>10000}
154
+ def self.preauth_order(order_id, redirect_url)
155
+ endpoint = "/orders/#{order_id}/preauth"
156
+ payload = { redirect_url: redirect_url }
157
+ make_request(endpoint, payload, 'POST')
158
+ end
159
+
160
+ def self.confirm_preauth(order_id, reason = 'Some reason')
161
+ endpoint = "/orders/#{order_id}/preauth/confirm"
162
+ payload = { reason: reason }
163
+ make_request(endpoint, payload, 'POST')
164
+ end
165
+
166
+ def self.release_preauth(order_id, reason = 'Some reason')
167
+ endpoint = "/orders/#{order_id}/preauth/release"
168
+ payload = { reason: reason }
169
+ make_request(endpoint, payload, 'POST')
170
+ end
171
+
172
+ private
173
+
174
+ def self.log(data)
175
+ puts data
176
+ end
177
+
178
+ def self.auth(endpoint, data)
179
+ c = Curl::Easy.new(Safecrow.configuration.url + Safecrow.configuration.prefix + endpoint)
180
+ c.headers['Content-Type'] = 'application/json'
181
+ c.http_auth_types = :basic
182
+ c.username = Safecrow.configuration.apikey
183
+ c.password = OpenSSL::HMAC.hexdigest('SHA256', Safecrow.configuration.apisecret, data)
184
+ c
185
+ end
186
+
187
+ def self.make_request(endpoint, payload, method)
188
+ data = Safecrow.configuration.apikey + method + Safecrow.configuration.prefix + endpoint + proceed_payload(payload)
189
+ c = auth(endpoint, data)
190
+ case method
191
+ when 'GET'
192
+ c.perform
193
+ when 'POST'
194
+ c.post(proceed_payload(payload))
195
+ else
196
+ raise ArgumentError, "undefined method #{method}"
197
+ end
198
+ log(endpoint)
199
+ log(c.status)
200
+ log(c.body)
201
+ raise c.status + c.body if c.status != '200 OK'
202
+ JSON.parse(c.body)
203
+ end
204
+
205
+ def self.proceed_payload(payload)
206
+ if payload.empty?
207
+ ''
208
+ else
209
+ payload.to_json
210
+ end
211
+ end
212
+ end
data/safecrow.gemspec ADDED
@@ -0,0 +1,31 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "safecrow/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "safecrow"
8
+ spec.version = Safecrow::VERSION
9
+ spec.authors = ["McAlex"]
10
+ spec.email = ["mcalexvrn@yandex.ru"]
11
+
12
+ spec.summary = %q{SDK for Safecrow API v3 }
13
+ spec.homepage = "https://github.com/dcrtit/safecrow"
14
+ spec.license = "MIT"
15
+
16
+ # Specify which files should be added to the gem when it is released.
17
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
18
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
19
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
20
+ end
21
+ spec.bindir = "exe"
22
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
23
+ spec.require_paths = ["lib"]
24
+
25
+ spec.add_development_dependency "bundler", "~> 1.16"
26
+ spec.add_development_dependency "rake", "~> 10.0"
27
+ spec.add_development_dependency "rspec", "~> 3.2"
28
+ spec.add_dependency "curb"
29
+ spec.add_dependency "multi_json"
30
+ spec.add_dependency "oj"
31
+ end
metadata ADDED
@@ -0,0 +1,144 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: safecrow
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - McAlex
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-08-20 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.16'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.16'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.2'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.2'
55
+ - !ruby/object:Gem::Dependency
56
+ name: curb
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: multi_json
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: oj
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ description:
98
+ email:
99
+ - mcalexvrn@yandex.ru
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - ".gitignore"
105
+ - ".rspec"
106
+ - ".travis.yml"
107
+ - Gemfile
108
+ - Gemfile.lock
109
+ - LICENSE.txt
110
+ - README.md
111
+ - Rakefile
112
+ - bin/console
113
+ - bin/setup
114
+ - lib/generators/safecrow/install/install_generator.rb
115
+ - lib/generators/safecrow/install/templates/safecrow.rb
116
+ - lib/safecrow.rb
117
+ - lib/safecrow/configuration.rb
118
+ - lib/safecrow/version.rb
119
+ - safecrow.gemspec
120
+ homepage: https://github.com/dcrtit/safecrow
121
+ licenses:
122
+ - MIT
123
+ metadata: {}
124
+ post_install_message:
125
+ rdoc_options: []
126
+ require_paths:
127
+ - lib
128
+ required_ruby_version: !ruby/object:Gem::Requirement
129
+ requirements:
130
+ - - ">="
131
+ - !ruby/object:Gem::Version
132
+ version: '0'
133
+ required_rubygems_version: !ruby/object:Gem::Requirement
134
+ requirements:
135
+ - - ">="
136
+ - !ruby/object:Gem::Version
137
+ version: '0'
138
+ requirements: []
139
+ rubyforge_project:
140
+ rubygems_version: 2.7.7
141
+ signing_key:
142
+ specification_version: 4
143
+ summary: SDK for Safecrow API v3
144
+ test_files: []