Skip to content

Commit

Permalink
#65 /withdraw
Browse files Browse the repository at this point in the history
  • Loading branch information
yegor256 committed Feb 5, 2020
1 parent d1d6ea4 commit 3a85ac7
Show file tree
Hide file tree
Showing 19 changed files with 335 additions and 39 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,4 @@ gem 'sprockets', '3.7.2'
gem 'telebot', '0.1.2'
gem 'thin', '1.7.2'
gem 'xcop', '0.6'
gem 'zold-ruby-sdk', '0.2.2'
64 changes: 61 additions & 3 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -36,27 +36,33 @@ GEM
url
coercible (1.0.0)
descendants_tracker (~> 0.0.1)
concurrent-ruby (1.1.5)
concurrent-ruby (1.1.3)
crass (1.0.6)
daemons (1.3.1)
descendants_tracker (0.0.4)
thread_safe (~> 0.3, >= 0.3.1)
diff-lcs (1.3)
differ (0.1.2)
diffy (3.2.1)
docile (1.3.2)
equalizer (0.0.11)
erubi (1.9.0)
eslintrb (2.1.0)
execjs
multi_json (>= 1.3)
rake
ethon (0.12.0)
ffi (>= 1.3.0)
eventmachine (1.2.7)
execjs (2.7.0)
faraday (0.17.3)
multipart-post (>= 1.2, < 3)
faraday_middleware (0.14.0)
faraday (>= 0.7.4, < 1.0)
ffi (1.12.2)
futex (0.8.6)
get_process_mem (0.2.5)
ffi (~> 1.0)
glogin (0.6.0)
base58 (= 0.2.3)
haml (5.0.4)
Expand All @@ -65,17 +71,24 @@ GEM
i18n (1.8.2)
concurrent-ruby (~> 1.0)
ice_nine (0.11.2)
ipaddr (1.2.2)
iri (0.4.1)
jaro_winkler (1.5.4)
json (2.3.0)
json (2.1.0)
listen (3.2.1)
rb-fsevent (~> 0.10, >= 0.10.3)
rb-inotify (~> 0.9, >= 0.9.10)
loofah (2.4.0)
crass (~> 1.0.2)
nokogiri (>= 1.5.9)
loog (0.2.1)
memory_profiler (0.9.12)
method_source (0.9.2)
mimic (0.4.2)
json
plist
rack
sinatra
mini_portile2 (2.4.0)
minitest (5.11.3)
minitest-reporters (1.3.6)
Expand All @@ -89,6 +102,8 @@ GEM
ruby2_keywords (~> 0.0.1)
nokogiri (1.10.7)
mini_portile2 (~> 2.4.0)
openssl (2.1.2)
ipaddr
parallel (1.19.1)
parser (2.7.0.2)
ast (~> 2.4.0)
Expand All @@ -98,6 +113,7 @@ GEM
loog (~> 0.2)
pg (~> 1.1)
random-port (~> 0.3)
plist (3.5.0)
powerpack (0.1.2)
rack (2.0.6)
rack-protection (2.0.4)
Expand Down Expand Up @@ -161,6 +177,7 @@ GEM
sass-listen (4.0.0)
rb-fsevent (~> 0.9, >= 0.9.4)
rb-inotify (~> 0.9, >= 0.9.7)
semantic (1.6.1)
sentry-raven (2.7.4)
faraday (>= 0.7.6, < 1.0)
simplecov (0.18.1)
Expand All @@ -180,10 +197,12 @@ GEM
rack-protection (= 2.0.4)
sinatra (= 2.0.4)
tilt (>= 1.3, < 3)
slop (4.8.0)
slop (4.6.2)
sprockets (3.7.2)
concurrent-ruby (~> 1.0)
rack (> 1, < 3)
sys-proctable (1.2.1)
ffi
telebot (0.1.2)
faraday
faraday_middleware
Expand All @@ -195,11 +214,20 @@ GEM
rack (>= 1, < 3)
thor (1.0.1)
thread_safe (0.3.6)
threads (0.3.0)
backtrace (~> 0)
concurrent-ruby (~> 1.0)
tilt (2.0.10)
total (0.3.0)
typhoeus (1.3.1)
ethon (>= 0.9.0)
tzinfo (1.2.6)
thread_safe (~> 0.1)
unicode-display_width (1.4.1)
url (0.3.2)
usagewatch (0.0.7)
usagewatch_ext (0.2.1)
usagewatch (~> 0.0.6)
virtus (1.0.5)
axiom-types (~> 0.1)
coercible (~> 1.0)
Expand All @@ -210,7 +238,36 @@ GEM
nokogiri (~> 1.8)
rainbow (~> 3.0)
slop (~> 4.4)
zache (0.12.0)
zeitwerk (2.2.2)
zold (0.29.27)
backtrace (>= 0.3)
concurrent-ruby (= 1.1.3)
diffy (= 3.2.1)
futex (>= 0.8.5)
get_process_mem (~> 0.2)
haml (= 5.0.4)
json (= 2.1.0)
memory_profiler (= 0.9.12)
mimic (= 0.4.2)
openssl (= 2.1.2)
rainbow (= 3.0.0)
semantic (= 1.6.1)
sinatra (= 2.0.4)
slop (= 4.6.2)
sys-proctable (= 1.2.1)
thin (= 1.7.2)
threads (>= 0.3)
total (>= 0.3)
typhoeus (= 1.3.1)
usagewatch_ext (= 0.2.1)
zache (>= 0.12)
zold-score (= 0.4.6)
zold-ruby-sdk (0.2.2)
loog (~> 0.2)
typhoeus (= 1.3.1)
zold (~> 0.21)
zold-score (0.4.6)

PLATFORMS
ruby
Expand Down Expand Up @@ -244,6 +301,7 @@ DEPENDENCIES
telebot (= 0.1.2)
thin (= 1.7.2)
xcop (= 0.6)
zold-ruby-sdk (= 0.2.2)

RUBY VERSION
ruby 2.6.0p0
Expand Down
14 changes: 10 additions & 4 deletions codexia.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@
'client_secret' => '?',
'encryption_secret' => ''
},
'zold' => {
'token' => '?',
'keygap' => '?'
},
'sentry' => ''
}
config = YAML.safe_load(File.open(File.join(File.dirname(__FILE__), 'config.yml'))) unless ENV['RACK_ENV'] == 'test'
Expand All @@ -62,11 +66,12 @@
c.release = Xia::VERSION
end
end
disable :show_exceptions
disable :raise_errors
enable :logging
set :bind, '0.0.0.0'
set :server, :thin
set :show_exceptions, false
set :raise_errors, false
set :dump_errors, false
set :dump_errors, ENV['RACK_ENV'] == 'test'
set :config, config
set :logging, true
set :log, Loog::REGULAR
Expand Down Expand Up @@ -119,7 +124,7 @@
def the_author
redirect '/welcome' unless @locals[:author]
require_relative 'objects/authors'
Xia::Authors.new(settings.pgsql).named(@locals[:author][:login].downcase)
Xia::Authors.new(settings.pgsql, log: settings.log).named(@locals[:author][:login].downcase)
end

def iri
Expand All @@ -130,3 +135,4 @@ def iri
require_relative 'front/front_login.rb'
require_relative 'front/front_helpers.rb'
require_relative 'front/front_project.rb'
require_relative 'front/front_karma.rb'
39 changes: 39 additions & 0 deletions front/front_karma.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# frozen_string_literal: true

# Copyright (c) 2020 Yegor Bugayenko
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the 'Software'), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

get '/karma' do
haml :karma, layout: :layout, locals: merged(
title: '/karma',
withdrawals: the_author.withdrawals.recent(limit: 25)
)
end

post '/karma/withdraw' do
wallet = params[:wallet].strip
points = params[:points].to_i
id = the_author.withdrawals.pay(
wallet, points,
Zold::WTS.new(settings.config['zold']['token'], log: settings.log),
settings.config['zold']['keygap']
)
flash(iri.cut('/karma'), "We sent #{points} USD to your Zold wallet, payment ID is ##{id}")
end
2 changes: 1 addition & 1 deletion front/front_misc.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
Raven.capture_exception(e)
haml(
:error,
layout: :layout,
layout: nil,
locals: merged(
title: 'error',
error: "#{e.message}\n\t#{e.backtrace.join("\n\t")}"
Expand Down
37 changes: 37 additions & 0 deletions liquibase/2020/005-withdrawal.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?xml version="1.0"?>
<!--
(The MIT License)
Copyright (c) 2020 Yegor Bugayenko
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the 'Software'), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
-->
<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-2.0.xsd" logicalFilePath="001-initial-schema.xml">
<changeSet id="005" author="yegor256">
<sql>
CREATE TABLE withdrawal (
id SERIAL PRIMARY KEY,
author INT NOT NULL REFERENCES author(id),
wallet VARCHAR(64) NOT NULL,
zents BIGINT NOT NULL,
created TIMESTAMPTZ DEFAULT now() NOT NULL
);
</sql>
</changeSet>
</databaseChangeLog>
20 changes: 12 additions & 8 deletions objects/author.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

require 'loog'
require_relative 'xia'
require_relative 'urror'
require_relative 'projects'
require_relative 'withdrawals'

# One author.
# Author:: Yegor Bugayenko (yegor256@gmail.com)
Expand All @@ -31,9 +33,14 @@
class Xia::Author
attr_reader :id

def initialize(pgsql, id)
def initialize(pgsql, id, log: Loog::NULL)
@pgsql = pgsql
@id = id
@log = log
end

def login
row['login']
end

def karma
Expand Down Expand Up @@ -62,20 +69,17 @@ def karma
end

def projects
Xia::Projects.new(@pgsql, self)
Xia::Projects.new(@pgsql, self, log: @log)
end

def login
row['login']
def withdrawals
Xia::Withdrawals.new(@pgsql, self, log: @log)
end

private

def row
row = @pgsql.exec(
'SELECT * FROM author WHERE id=$1',
[@id]
)[0]
row = @pgsql.exec('SELECT * FROM author WHERE id=$1', [@id])[0]
raise Xia::Urror, "Author @#{@login} not found in the database" if row.nil?
row
end
Expand Down
6 changes: 4 additions & 2 deletions objects/authors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

require 'loog'
require_relative 'xia'
require_relative 'author'

Expand All @@ -28,15 +29,16 @@
# Copyright:: Copyright (c) 2020 Yegor Bugayenko
# License:: MIT
class Xia::Authors
def initialize(pgsql)
def initialize(pgsql, log: Loog::NULL)
@pgsql = pgsql
@log = log
end

def named(login)
id = @pgsql.exec(
'INSERT INTO author (login) VALUES ($1) ON CONFLICT DO NOTHING RETURNING id',
[login]
)[0]['id'].to_i
Xia::Author.new(@pgsql, id)
Xia::Author.new(@pgsql, id, log: @log)
end
end
4 changes: 3 additions & 1 deletion objects/badge.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

require 'loog'
require_relative 'xia'

# Badges.
Expand All @@ -29,10 +30,11 @@
class Xia::Badge
attr_reader :id

def initialize(pgsql, project, id)
def initialize(pgsql, project, id, log: Loog::NULL)
@pgsql = pgsql
@project = project
@id = id
@log = log
end

def detach
Expand Down
Loading

0 comments on commit 3a85ac7

Please sign in to comment.