Skip to content

Commit

Permalink
settings can be empty
Browse files Browse the repository at this point in the history
  • Loading branch information
madmatvey committed Jun 7, 2024
1 parent c661029 commit cf1007c
Show file tree
Hide file tree
Showing 7 changed files with 220 additions and 35 deletions.
12 changes: 11 additions & 1 deletion .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,14 @@ Layout/LineLength:
Max: 120

Naming/FileName:
Enabled: false
Enabled: false

Metrics/AbcSize:
Enabled: false

Metrics/MethodLength:
Max: 20

Metrics/BlockLength:
Exclude:
- spec/*.rb
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
## [Unreleased]
# Changelog

## [0.1.1] - 2024-06-07

- add conditional view of donation crypto currences

## [0.1.0] - 2024-06-06

Expand Down
4 changes: 3 additions & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
jekyll-crypto-donations (0.1.0)
jekyll-crypto-donations (0.1.1)
jekyll (~> 4.3)

GEM
Expand Down Expand Up @@ -101,6 +101,8 @@ GEM
sass-embedded (1.58.3)
google-protobuf (~> 3.21)
rake (>= 10.0.0)
sass-embedded (1.58.3-x86_64-darwin)
google-protobuf (~> 3.21)
strscan (3.1.0)
terminal-table (3.0.2)
unicode-display_width (>= 1.1.1, < 3)
Expand Down
132 changes: 101 additions & 31 deletions lib/jekyll-crypto-donations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,57 +8,127 @@ module Jekyll
module CryptoDonations
# describe {% crypto_donations %} tag content
class DonationsTag < Liquid::Tag
attr_reader :btc_address, :eth_address, :usdt_address

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

def render(context)
def btc_html
return "" unless btc_address

<<~HTML
<div>
<h3>Bitcoin (BTC)</h3>
<p>Address: #{btc_address}</p>
<p id="btc-donations">Loading...</p>
</div>
HTML
end

def eth_html
return "" unless eth_address

<<~HTML
<div>
<h3>Ethereum (ETH)</h3>
<p>Address: #{eth_address}</p>
<p id="eth-donations">Loading...</p>
</div>
HTML
end

def usdt_html
return "" unless usdt_address

<<~HTML
<div>
<h3>USDT (TRC-20)</h3>
<p>Address: #{usdt_address}</p>
<p id="usdt-donations">Loading...</p>
</div>
HTML
end

def btc_js
return "" unless btc_address

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

def eth_js
return "" unless eth_address

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

def usdt_js
return "" unless usdt_address

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

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")
@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

def opening_html
<<~HTML
<div id="crypto-donations">
<h2>Support Us with Crypto Donations</h2>
<p>#{@text}</p>
<div>
<h3>Bitcoin (BTC)</h3>
<p id="btc-address">#{btc_address}</p>
<p id="btc-donations">Loading...</p>
</div>
<div>
<h3>Ethereum (ETH)</h3>
<p id="eth-address">#{eth_address}</p>
<p id="eth-donations">Loading...</p>
</div>
<div>
<h3>USDT (TRC-20)</h3>
<p id="usdt-address">Address: #{usdt_address}</p>
<p id="usdt-donations">Loading...</p>
</div>
HTML
end

def closing_html
<<~HTML
</div>
<script type="module">
import { getDonations } from '/assets/js/crypto-donations/crypto-donations.js';
document.addEventListener('DOMContentLoaded', async () => {
const btcAddress = '#{btc_address}';
const ethAddress = '#{eth_address}';
const usdtAddress = '#{usdt_address}'
HTML
end

const usdtDonations = await getDonations('usdt', usdtAddress);
document.getElementById('usdt-donations').innerText = `Total received: ${usdtDonations} USDT`;
def closing_js
<<~JS
});
</script>
JS
end

const btcDonations = await getDonations('btc', btcAddress);
document.getElementById('btc-donations').innerText = `Total received: ${btcDonations} BTC`;
def render(context)
site_config(context)
return if btc_address.nil? && eth_address.nil? && usdt_address.nil?

const ethDonations = await getDonations('eth', ethAddress);
document.getElementById('eth-donations').innerText = `Total received: ${ethDonations} ETH`;
content = opening_html

});
</script>
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
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/jekyll-crypto-donations/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

module Jekyll
module CryptoDonations
VERSION = "0.1.0"
VERSION = "0.1.1"
end
end
85 changes: 85 additions & 0 deletions spec/jekyll_crypto_donations_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
# frozen_string_literal: true

require "spec_helper"

RSpec.describe Jekyll::CryptoDonations::DonationsTag do
let(:site_config) do
{
"btc_address" => "btc_test_address",
"eth_address" => "eth_test_address",
"usdt_address" => "usdt_test_address"
}
end

let(:context) do
make_context(site_config)
end
let(:tag) { Liquid::Template.parse("{% crypto_donations Additional text %}").render(context) }

describe "#render" do
it "returns the HTML with all donation addresses" do
expect(tag).to include("Support Us with Crypto Donations")
expect(tag).to include("Additional text")
expect(tag).to include("btc_test_address")
expect(tag).to include("eth_test_address")
expect(tag).to include("usdt_test_address")
expect(tag).to include("getDonations('btc', 'btc_test_address')")
expect(tag).to include("getDonations('eth', 'eth_test_address')")
expect(tag).to include("getDonations('usdt', 'usdt_test_address')")
end

context "when config not includes BTC address" do
let(:site_config) do
{
"eth_address" => "eth_test_address",
"usdt_address" => "usdt_test_address"
}
end

it "does not include BTC section" do
expect(tag).not_to include("Bitcoin (BTC)")
expect(tag).not_to include("btc_test_address")
expect(tag).not_to include("getDonations('btc', 'btc_test_address')")
end
end

context "when config not includes ETH address" do
let(:site_config) do
{
"btc_address" => "btc_test_address",
"usdt_address" => "usdt_test_address"
}
end

it "does not include ETH section" do
expect(tag).not_to include("Ethereum (ETH)")
expect(tag).not_to include("eth_test_address")
expect(tag).not_to include("getDonations('eth', 'eth_test_address')")
end
end

context "when config not includes USDT address" do
let(:site_config) do
{
"btc_address" => "btc_test_address",
"eth_address" => "eth_test_address"
}
end

it "does not include USDT section" do
expect(tag).not_to include("USDT (TRC-20)")
expect(tag).not_to include("usdt_test_address")
expect(tag).not_to include("getDonations('usdt', 'usdt_test_address')")
end
end

context "when no donation addresses are provided" do
let(:site_config) do
{}
end
it "returns empty string" do
expect(tag).to be_empty
end
end
end
end
14 changes: 14 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,17 @@
c.syntax = :expect
end
end

require "jekyll"

Jekyll.logger = Logger.new(StringIO.new)

def make_site
Jekyll::Site.new(Jekyll.configuration)
end

def make_context(value = nil)
site = make_site
site.config["crypto_donations"] = value
Liquid::Context.new({}, {}, site: site)
end

0 comments on commit cf1007c

Please sign in to comment.