Skip to content

Commit 4d6900d

Browse files
committed
reduced complexity
1 parent c3322df commit 4d6900d

4 files changed

Lines changed: 145 additions & 180 deletions

File tree

.rubocop_todo.yml

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
# This configuration was generated by
22
# `rubocop --auto-gen-config`
3-
# on 2020-12-06 10:56:23 UTC using RuboCop version 1.5.2.
3+
# on 2020-12-06 11:41:05 UTC using RuboCop version 1.5.2.
44
# The point is for the user to remove these configuration records
55
# one by one as the offenses are removed from the code base.
66
# Note that changes in the inspected code, or installation of new
77
# versions of RuboCop, may require this file to be generated again.
88

9-
# Offense count: 18
9+
# Offense count: 17
1010
# Configuration parameters: IgnoredMethods, CountRepeatedAttributes.
1111
Metrics/AbcSize:
12-
Max: 51
12+
Max: 34
1313

1414
# Offense count: 17
1515
# Configuration parameters: CountComments, CountAsOne, ExcludedMethods, IgnoredMethods.
@@ -22,22 +22,17 @@ Metrics/BlockLength:
2222
Metrics/ClassLength:
2323
Max: 187
2424

25-
# Offense count: 4
25+
# Offense count: 3
2626
# Configuration parameters: IgnoredMethods.
2727
Metrics/CyclomaticComplexity:
28-
Max: 29
28+
Max: 16
2929

30-
# Offense count: 19
30+
# Offense count: 18
3131
# Configuration parameters: CountComments, CountAsOne, ExcludedMethods, IgnoredMethods.
3232
Metrics/MethodLength:
33-
Max: 57
34-
35-
# Offense count: 1
36-
# Configuration parameters: CountComments, CountAsOne.
37-
Metrics/ModuleLength:
38-
Max: 163
33+
Max: 29
3934

40-
# Offense count: 4
35+
# Offense count: 3
4136
# Configuration parameters: IgnoredMethods.
4237
Metrics/PerceivedComplexity:
4338
Max: 13
@@ -150,6 +145,7 @@ Rails/HasManyOrHasOneDependent:
150145
Rails/OutputSafety:
151146
Exclude:
152147
- 'app/helpers/application_helper.rb'
148+
- 'app/helpers/rates_helper.rb'
153149

154150
# Offense count: 11
155151
# Configuration parameters: ForbiddenMethods, AllowedMethods.

app/helpers/application_helper.rb

Lines changed: 0 additions & 165 deletions
Original file line numberDiff line numberDiff line change
@@ -1,171 +1,6 @@
11
# frozen_string_literal: true
22

33
module ApplicationHelper
4-
def btc_human(amount, options = {})
5-
amount ||= 0
6-
nobr = options.key?(:nobr) ? options[:nobr] : true
7-
denom = if options.key?(:denom)
8-
options[:denom]
9-
else
10-
(try(:current_user) ? current_user.denom : 0)
11-
end
12-
case denom
13-
when 0
14-
btc = to_btc(amount)
15-
when 1
16-
btc = to_mbtc(amount)
17-
when 2
18-
btc = to_ubtc(amount)
19-
when 3
20-
btc = to_satoshi(amount)
21-
when 4
22-
btc = to_usd(amount)
23-
when 5
24-
btc = to_eur(amount)
25-
when 6
26-
btc = to_aud(amount)
27-
when 7
28-
btc = to_brl(amount)
29-
when 8
30-
btc = to_cad(amount)
31-
when 9
32-
btc = to_cny(amount)
33-
when 10
34-
btc = to_gbp(amount)
35-
when 11
36-
btc = to_idr(amount)
37-
when 12
38-
btc = to_ils(amount)
39-
when 13
40-
btc = to_jpy(amount)
41-
when 14
42-
btc = to_mxn(amount)
43-
when 15
44-
btc = to_nok(amount)
45-
when 16
46-
btc = to_nzd(amount)
47-
when 17
48-
btc = to_pln(amount)
49-
when 18
50-
btc = to_ron(amount)
51-
when 19
52-
btc = to_rub(amount)
53-
when 20
54-
btc = to_sek(amount)
55-
when 21
56-
btc = to_sgd(amount)
57-
when 22
58-
btc = to_zar(amount)
59-
end
60-
btc = "<nobr>#{btc}</nobr>" if nobr
61-
btc.html_safe
62-
end
63-
64-
def to_btc(satoshies)
65-
format('%.8f Ƀ', (1.0 * satoshies.to_i / 1e8))
66-
end
67-
68-
def to_mbtc(satoshies)
69-
format('%.5f mɃ', (1.0 * satoshies.to_i / 1e5))
70-
end
71-
72-
def to_ubtc(satoshies)
73-
format('%.2f μɃ', (1.0 * satoshies.to_i / 1e2))
74-
end
75-
76-
def to_satoshi(satoshies)
77-
format('%.0f Satoshi', satoshies)
78-
end
79-
80-
def to_usd(satoshies)
81-
format('$%.2f', rate('USD', satoshies))
82-
end
83-
84-
def to_aud(satoshies)
85-
format('$%.2f', rate('AUD', satoshies))
86-
end
87-
88-
def to_eur(satoshies)
89-
format('%.2f€', rate('EUR', satoshies))
90-
end
91-
92-
def to_brl(satoshies)
93-
format('R$%.2f', rate('BRL', satoshies))
94-
end
95-
96-
def to_cad(satoshies)
97-
format('$%.2f', rate('CAD', satoshies))
98-
end
99-
100-
def to_cny(satoshies)
101-
format('%.2f¥', rate('CNY', satoshies))
102-
end
103-
104-
def to_gbp(satoshies)
105-
format('%.2f£', rate('GBP', satoshies))
106-
end
107-
108-
def to_idr(satoshies)
109-
format('%.2f Rp', rate('IDR', satoshies))
110-
end
111-
112-
def to_ils(satoshies)
113-
format('%.2f₪', rate('ILS', satoshies))
114-
end
115-
116-
def to_jpy(satoshies)
117-
format('%.2f¥', rate('JPY', satoshies))
118-
end
119-
120-
def to_mxn(satoshies)
121-
format('%.2f MXN', rate('MXN', satoshies))
122-
end
123-
124-
def to_nok(satoshies)
125-
format('%.2f kr', rate('NOK', satoshies))
126-
end
127-
128-
def to_nzd(satoshies)
129-
format('$%.2f', rate('NZD', satoshies))
130-
end
131-
132-
def to_pln(satoshies)
133-
format('%.2f zł', rate('PLN', satoshies))
134-
end
135-
136-
def to_ron(satoshies)
137-
format('%.2f lei', rate('RON', satoshies))
138-
end
139-
140-
def to_rub(satoshies)
141-
format('%.2f₽', rate('RUB', satoshies))
142-
end
143-
144-
def to_sek(satoshies)
145-
format('%.2f kr', rate('SEK', satoshies))
146-
end
147-
148-
def to_sgd(satoshies)
149-
format('%.2f S$', rate('SGD', satoshies))
150-
end
151-
152-
def to_zar(satoshies)
153-
format('%.2f R', rate('ZAR', satoshies))
154-
end
155-
156-
def rate(currency, satoshies)
157-
satoshies * 0.00000001 * get_rate(currency)
158-
end
159-
160-
def get_rate(currency)
161-
Rails.cache.fetch("####{currency}", expires_in: 1.hour) do
162-
uri = URI("https://api.coindesk.com/v1/bpi/currentprice/#{currency}.json")
163-
response = Net::HTTP.get_response(uri)
164-
hash = JSON.parse(response.body)
165-
hash['bpi'][currency]['rate_float'].to_f
166-
end
167-
end
168-
1694
def render_flash_messages
1705
html = []
1716
flash.each do |type, message|

app/helpers/rates_helper.rb

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
# frozen_string_literal: true
2+
3+
module RatesHelper
4+
DENOMINATIONS = %w[
5+
BTC mBTC μBTC Satoshi USD EUR AUD BRL CAD CNY GBP IDR ILS JPY MXN NOK NZD PLN RON RUB SEK SGD ZAR
6+
].freeze
7+
8+
def denom_options_for_select
9+
# [["BTC", "0"], ["mBTC", "1"], ...
10+
DENOMINATIONS.each_with_index.map { |label, index| [label, index.to_s] }
11+
end
12+
13+
def btc_human(amount, options = {})
14+
nobr = options.key?(:nobr) ? options[:nobr] : true
15+
denom = options.key?(:denom) ? options[:denom] : current_user&.denom || 0
16+
17+
btc = to_denom(denom, amount)
18+
btc = "<nobr>#{btc}</nobr>" if nobr
19+
btc.html_safe
20+
end
21+
22+
private
23+
24+
def to_denom(denom, amount)
25+
amount ||= 0
26+
convert_method_name = "to_#{DENOMINATIONS[denom].gsub('μ', 'u').downcase}"
27+
send(convert_method_name, amount)
28+
end
29+
30+
def to_btc(satoshies)
31+
format('%.8f Ƀ', (1.0 * satoshies.to_i / 1e8))
32+
end
33+
34+
def to_mbtc(satoshies)
35+
format('%.5f mɃ', (1.0 * satoshies.to_i / 1e5))
36+
end
37+
38+
def to_ubtc(satoshies)
39+
format('%.2f μɃ', (1.0 * satoshies.to_i / 1e2))
40+
end
41+
42+
def to_satoshi(satoshies)
43+
format('%.0f Satoshi', satoshies)
44+
end
45+
46+
def to_usd(satoshies)
47+
format('$%.2f', rate('USD', satoshies))
48+
end
49+
50+
def to_aud(satoshies)
51+
format('$%.2f', rate('AUD', satoshies))
52+
end
53+
54+
def to_eur(satoshies)
55+
format('%.2f€', rate('EUR', satoshies))
56+
end
57+
58+
def to_brl(satoshies)
59+
format('R$%.2f', rate('BRL', satoshies))
60+
end
61+
62+
def to_cad(satoshies)
63+
format('$%.2f', rate('CAD', satoshies))
64+
end
65+
66+
def to_cny(satoshies)
67+
format('%.2f¥', rate('CNY', satoshies))
68+
end
69+
70+
def to_gbp(satoshies)
71+
format('%.2f£', rate('GBP', satoshies))
72+
end
73+
74+
def to_idr(satoshies)
75+
format('%.2f Rp', rate('IDR', satoshies))
76+
end
77+
78+
def to_ils(satoshies)
79+
format('%.2f₪', rate('ILS', satoshies))
80+
end
81+
82+
def to_jpy(satoshies)
83+
format('%.2f¥', rate('JPY', satoshies))
84+
end
85+
86+
def to_mxn(satoshies)
87+
format('%.2f MXN', rate('MXN', satoshies))
88+
end
89+
90+
def to_nok(satoshies)
91+
format('%.2f kr', rate('NOK', satoshies))
92+
end
93+
94+
def to_nzd(satoshies)
95+
format('$%.2f', rate('NZD', satoshies))
96+
end
97+
98+
def to_pln(satoshies)
99+
format('%.2f zł', rate('PLN', satoshies))
100+
end
101+
102+
def to_ron(satoshies)
103+
format('%.2f lei', rate('RON', satoshies))
104+
end
105+
106+
def to_rub(satoshies)
107+
format('%.2f₽', rate('RUB', satoshies))
108+
end
109+
110+
def to_sek(satoshies)
111+
format('%.2f kr', rate('SEK', satoshies))
112+
end
113+
114+
def to_sgd(satoshies)
115+
format('%.2f S$', rate('SGD', satoshies))
116+
end
117+
118+
def to_zar(satoshies)
119+
format('%.2f R', rate('ZAR', satoshies))
120+
end
121+
122+
def rate(currency, satoshies)
123+
satoshies * 0.00000001 * get_rate(currency)
124+
end
125+
126+
def get_rate(currency)
127+
Rails.cache.fetch("####{currency}", expires_in: 1.hour) do
128+
uri = URI("https://api.coindesk.com/v1/bpi/currentprice/#{currency}.json")
129+
response = Net::HTTP.get_response(uri)
130+
hash = JSON.parse(response.body)
131+
hash['bpi'][currency]['rate_float'].to_f
132+
end
133+
end
134+
end

app/views/users/show.html.haml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
= btc_human @user.balance
66
= form_for(@user) do |f|
77
= f.select :denom,
8-
options_for_select([["BTC", "0"], ["mBTC", "1"], ["μBTC", "2"], ["Satoshi", "3"], ["USD", "4"], ["EUR", "5"], ["AUD", "6"], ["BRL", "7"], ["CAD", "8"], ["CNY", "9"], ["GBP", "10"], ["IDR", "11"], ["ILS", "12"], ["JPY", "13"], ["MXN", "14"], ["NOK", "15"], ["NZD", "16"], ["PLN", "17"], ["RON", "18"], ["RUB", "19"], ["SEK", "20"], ["SGD", "21"], ["ZAR", "22"]], selected: @user.denom)
8+
options_for_select(denom_options_for_select, selected: @user.denom)
99
= f.submit "save"
1010
%p
1111
%small= raw t('.threshold', threshold: btc_human(CONFIG["min_payout"]))
@@ -53,4 +53,4 @@
5353
= bootstrap_form_for @user, html: {class: (params[:delete_user] ? '' : 'collapse'), id: 'delete_user_form', method: 'DELETE'} do |f|
5454
%p= t('.delete_account_notice')
5555
= f.text_field :email, value: '', autocomplete: "off"
56-
= f.submit t('.delete_account_confirm'), class: 'btn btn-danger'
56+
= f.submit t('.delete_account_confirm'), class: 'btn btn-danger'

0 commit comments

Comments
 (0)