Class: Jekyll::CryptoDonations::DonationsTag

Inherits:
Liquid::Tag
  • Object
show all
Defined in:
lib/jekyll-crypto-donations.rb

Overview

describe crypto_donations % tag content

Constant Summary collapse

CRYPTO_CURRENCIES =
{
  btc: { name: "Bitcoin", ticker: "BTC" },
  eth: { name: "Ethereum", ticker: "ETH" },
  usdt: { name: "Tether", ticker: "USDT TRC-20" }
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tag_name, text, tokens) ⇒ DonationsTag

Returns a new instance of DonationsTag.



20
21
22
23
# File 'lib/jekyll-crypto-donations.rb', line 20

def initialize(tag_name, text, tokens)
  super
  @text = text
end

Instance Attribute Details

#btc_addressObject (readonly)

rubocop:disable Metrics/ClassLength



12
13
14
# File 'lib/jekyll-crypto-donations.rb', line 12

def btc_address
  @btc_address
end

#eth_addressObject (readonly)

rubocop:disable Metrics/ClassLength



12
13
14
# File 'lib/jekyll-crypto-donations.rb', line 12

def eth_address
  @eth_address
end

#usdt_addressObject (readonly)

rubocop:disable Metrics/ClassLength



12
13
14
# File 'lib/jekyll-crypto-donations.rb', line 12

def usdt_address
  @usdt_address
end

Instance Method Details

#btc_htmlObject



94
95
96
97
98
# File 'lib/jekyll-crypto-donations.rb', line 94

def btc_html
  return "" unless btc_address

  currency_html(:btc, btc_address)
end

#btc_jsObject



112
113
114
115
116
117
118
119
120
# File 'lib/jekyll-crypto-donations.rb', line 112

def btc_js
  return "" unless btc_address

  total_id = "#{@instance_id}_btc-total"
  <<~JS
    const btcDonations = await getDonations('btc', '#{btc_address}');
    document.getElementById('#{total_id}').innerText = `Total received: ${btcDonations} BTC`;
  JS
end

#closing_htmlObject



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/jekyll-crypto-donations.rb', line 158

def closing_html
  <<~HTML
    </div>
    <script type="module">
      import { getDonations } from '/assets/js/crypto-donations/crypto-donations.js';
      document.addEventListener('DOMContentLoaded', function () {
        document.querySelectorAll('.jekyll-crypto-donations__item').forEach((item) => {
          item.querySelector('.jekyll-crypto-donations__crypto-btn-copy').addEventListener('click', () => {
            const code = item.querySelector('.jekyll-crypto-donations__crypto-code');
            navigator.clipboard.writeText(code.innerText);
            code.classList.add('jekyll-crypto-donations__crypto-code--is-copied');
            setTimeout(() => {
              code.classList.remove('jekyll-crypto-donations__crypto-code--is-copied');
            }, 1500);
          });

          item.querySelector('.jekyll-crypto-donations__crypto-btn-qr').addEventListener('click', () => {
            item.querySelector('.jekyll-crypto-donations__crypto').classList.toggle('jekyll-crypto-donations__crypto--show-qr');
          });
        });
      });

      document.addEventListener('DOMContentLoaded', async () => {
  HTML
end

#closing_jsObject



184
185
186
187
188
189
# File 'lib/jekyll-crypto-donations.rb', line 184

def closing_js
  <<~JS
      });
    </script>
  JS
end

#currency_html(currency_ticker, address) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/jekyll-crypto-donations.rb', line 25

def currency_html(currency_ticker, address)
  currency = CRYPTO_CURRENCIES[currency_ticker]
  total_id = "#{@instance_id}_#{currency_ticker}-total"
  <<~HTML
    <div class="jekyll-crypto-donations__item">
      <span class="jekyll-crypto-donations__crypto-name">#{currency[:name]} (#{currency[:ticker]})</span>
      <div class="jekyll-crypto-donations__crypto">
        <div class="jekyll-crypto-donations__crypto-value">
          <div class="jekyll-crypto-donations__crypto-code">
            <span>#{address}</span>
          </div>
          <div class="jekyll-crypto-donations__crypto-buttons">
            <button class="jekyll-crypto-donations__crypto-btn-copy" type="button" aria-label="Copy #{currency[:ticker]} address">
              <svg fill="#ededed" stroke="#ededed" width="20" height="20" version="1.1" viewBox="144 144 512 512" xmlns="http://www.w3.org/2000/svg" stroke-width="20" aria-hidden="true">
                <g stroke-width="0"></g>
                <g stroke-linecap="round" stroke-linejoin="round" stroke="#CCCCCC" stroke-width="0">
                  <g xmlns="http://www.w3.org/2000/svg">
                    <path d="m458.74 466.8h123.13 17.23c5.4414 0 10.078-4.6367 10.078-10.078v-86.957-139.15-31.84c0-5.4414-4.6367-10.078-10.078-10.078h-87.359-138.95-31.641c-5.4414 0-10.078 4.6367-10.078 10.078v126.66 17.938c0 13 20.152 13 20.152 0v-126.66-17.938c-3.3242 3.3242-6.75 6.75-10.078 10.078h87.359 138.95 31.641c-3.3242-3.3242-6.75-6.75-10.078-10.078v86.957 139.15 31.84l10.078-10.078h-123.13-17.23c-12.992 0.003906-12.992 20.156 0.003907 20.156z"></path>
                    <path d="m458.74 591.14h-86.957-139.15-31.84l10.078 10.078v-86.957-139.15-31.84c-3.3242 3.3242-6.75 6.75-10.078 10.078h86.957 139.15 31.84c-3.3242-3.3242-6.75-6.75-10.078-10.078v86.957 139.15 31.84c0 13 20.152 13 20.152 0v-86.957-139.15-31.84c0-5.4414-4.6367-10.078-10.078-10.078h-86.957-139.15-31.84c-5.4414 0-10.078 4.6367-10.078 10.078v86.957 139.15 31.84c0 5.4414 4.6367 10.078 10.078 10.078h86.957 139.15 31.84c13.004-0.003906 13.004-20.156 0.003907-20.156z"></path>
                  </g>
                </g>
                <g>
                  <g xmlns="http://www.w3.org/2000/svg">
                    <path d="m458.74 466.8h123.13 17.23c5.4414 0 10.078-4.6367 10.078-10.078v-86.957-139.15-31.84c0-5.4414-4.6367-10.078-10.078-10.078h-87.359-138.95-31.641c-5.4414 0-10.078 4.6367-10.078 10.078v126.66 17.938c0 13 20.152 13 20.152 0v-126.66-17.938c-3.3242 3.3242-6.75 6.75-10.078 10.078h87.359 138.95 31.641c-3.3242-3.3242-6.75-6.75-10.078-10.078v86.957 139.15 31.84l10.078-10.078h-123.13-17.23c-12.992 0.003906-12.992 20.156 0.003907 20.156z"></path>
                    <path d="m458.74 591.14h-86.957-139.15-31.84l10.078 10.078v-86.957-139.15-31.84c-3.3242 3.3242-6.75 6.75-10.078 10.078h86.957 139.15 31.84c-3.3242-3.3242-6.75-6.75-10.078-10.078v86.957 139.15 31.84c0 13 20.152 13 20.152 0v-86.957-139.15-31.84c0-5.4414-4.6367-10.078-10.078-10.078h-86.957-139.15-31.84c-5.4414 0-10.078 4.6367-10.078 10.078v86.957 139.15 31.84c0 5.4414 4.6367 10.078 10.078 10.078h86.957 139.15 31.84c13.004-0.003906 13.004-20.156 0.003907-20.156z"></path>
                  </g>
                </g>
              </svg>
            </button>
            <button class="jekyll-crypto-donations__crypto-btn-qr" type="button" aria-label="Show #{currency[:ticker]} QR code">
              <svg fill="#ededed" stroke="#ededed" width="20" height="20" version="1.1" viewBox="144 144 512 512" xmlns="http://www.w3.org/2000/svg" stroke-width="0" aria-hidden="true">
                <g stroke-width="0"></g>
                <g stroke-linecap="round" stroke-linejoin="round" stroke="#CCCCCC" stroke-width="0">
                  <g xmlns="http://www.w3.org/2000/svg">
                    <path d="m379.01 588.93h-167.94v-167.93h167.94zm-125.95-41.984h83.969v-83.965h-83.969z"></path>
                    <path d="m588.93 379.01h-167.93v-167.94h167.94zm-125.95-41.984h83.969l-0.003906-83.969h-83.965z"></path>
                    <path d="m379.01 379.01h-167.94v-167.94h167.94zm-125.95-41.984h83.969v-83.969h-83.969z"></path>
                    <path d="m588.93 588.93h-83.969v-41.984h41.984v-41.984h41.984z"></path>
                    <path d="m462.98 588.93h-41.984v-83.969h41.984v-41.98h41.98v83.965h-41.98z"></path>
                    <path d="m420.99 420.99h41.984v41.984h-41.984z"></path>
                    <path d="m504.96 420.99h83.969v41.984h-83.969z"></path>
                  </g>
                </g>
                <g>
                  <g xmlns="http://www.w3.org/2000/svg">
                    <path d="m379.01 588.93h-167.94v-167.93h167.94zm-125.95-41.984h83.969v-83.965h-83.969z"></path>
                    <path d="m588.93 379.01h-167.93v-167.94h167.94zm-125.95-41.984h83.969l-0.003906-83.969h-83.965z"></path>
                    <path d="m379.01 379.01h-167.94v-167.94h167.94zm-125.95-41.984h83.969v-83.969h-83.969z"></path>
                    <path d="m588.93 588.93h-83.969v-41.984h41.984v-41.984h41.984z"></path>
                    <path d="m462.98 588.93h-41.984v-83.969h41.984v-41.98h41.98v83.965h-41.98z"></path>
                    <path d="m420.99 420.99h41.984v41.984h-41.984z"></path>
                    <path d="m504.96 420.99h83.969v41.984h-83.969z"></path>
                  </g>
                </g>
              </svg>
            </button>
            <div class="jekyll-crypto-donations__crypto-total-received">
              <span id="#{total_id}">Loading...</span>
            </div>
          </div>
        </div>
        <div class="jekyll-crypto-donations__crypto-image-qr">
          <img alt="#{currency[:name]} donation QR code" width="150" height="150" src="https://api.qrserver.com/v1/create-qr-code/?size=150x150&amp;data=#{address}">
        </div>
      </div>
    </div>
  HTML
end

#eth_htmlObject



100
101
102
103
104
# File 'lib/jekyll-crypto-donations.rb', line 100

def eth_html
  return "" unless eth_address

  currency_html(:eth, eth_address)
end

#eth_jsObject



122
123
124
125
126
127
128
129
130
# File 'lib/jekyll-crypto-donations.rb', line 122

def eth_js
  return "" unless eth_address

  total_id = "#{@instance_id}_eth-total"
  <<~JS
    const ethDonations = await getDonations('eth', '#{eth_address}');
    document.getElementById('#{total_id}').innerText = `Total received: ${ethDonations} ETH`;
  JS
end

#opening_htmlObject



149
150
151
152
153
154
155
156
# File 'lib/jekyll-crypto-donations.rb', line 149

def opening_html
  <<~HTML
    <div id="#{@instance_id}" class="jekyll-crypto-donations">
      <div class="jekyll-crypto-donations__description">
        <span>#{@text}</span>
      </div>
  HTML
end

#render(context) ⇒ Object



191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/jekyll-crypto-donations.rb', line 191

def render(context)
  @instance_id = "jekyll-crypto-donations-#{SecureRandom.hex(4)}"
  site_config(context)
  return if btc_address.nil? && eth_address.nil? && usdt_address.nil?

  content = opening_html

  content += btc_html
  content += eth_html
  content += usdt_html

  content += closing_html

  content += btc_js
  content += eth_js
  content += usdt_js

  content += closing_js

  content
end

#site_config(context) ⇒ Object



142
143
144
145
146
147
# File 'lib/jekyll-crypto-donations.rb', line 142

def site_config(context)
  site_config = context.registers.fetch(:site).config
  @btc_address = site_config.dig("crypto_donations", "btc_address")
  @eth_address = site_config.dig("crypto_donations", "eth_address")
  @usdt_address = site_config.dig("crypto_donations", "usdt_address")
end

#usdt_htmlObject



106
107
108
109
110
# File 'lib/jekyll-crypto-donations.rb', line 106

def usdt_html
  return "" unless usdt_address

  currency_html(:usdt, usdt_address)
end

#usdt_jsObject



132
133
134
135
136
137
138
139
140
# File 'lib/jekyll-crypto-donations.rb', line 132

def usdt_js
  return "" unless usdt_address

  total_id = "#{@instance_id}_usdt-total"
  <<~JS
    const usdtDonations = await getDonations('usdt', '#{usdt_address}');
    document.getElementById('#{total_id}').innerText = `Total received: ${usdtDonations} USDT`;
  JS
end