diff --git a/.rubocop.yml b/.rubocop.yml
index 8826647..dfb6d83 100644
--- a/.rubocop.yml
+++ b/.rubocop.yml
@@ -14,4 +14,14 @@ Layout/LineLength:
Max: 120
Naming/FileName:
- Enabled: false
\ No newline at end of file
+ Enabled: false
+
+Metrics/AbcSize:
+ Enabled: false
+
+Metrics/MethodLength:
+ Max: 20
+
+Metrics/BlockLength:
+ Exclude:
+ - spec/*.rb
\ No newline at end of file
diff --git a/CHANGELOG.md b/CHANGELOG.md
index add520a..fd5d5a1 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,4 +1,9 @@
-## [Unreleased]
+# Changelog
+
+## [0.1.1] - 2024-06-07
+
+- add conditional view of donation crypto currences
+- cover with tests
## [0.1.0] - 2024-06-06
diff --git a/Gemfile.lock b/Gemfile.lock
index 7e6bcb3..8120398 100644
--- a/Gemfile.lock
+++ b/Gemfile.lock
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
- jekyll-crypto-donations (0.1.0)
+ jekyll-crypto-donations (0.1.1)
jekyll (~> 4.3)
GEM
@@ -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)
diff --git a/lib/jekyll-crypto-donations.rb b/lib/jekyll-crypto-donations.rb
index 622a573..a0bf2c5 100644
--- a/lib/jekyll-crypto-donations.rb
+++ b/lib/jekyll-crypto-donations.rb
@@ -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
+
+
Bitcoin (BTC)
+
Address: #{btc_address}
+
Loading...
+
+ HTML
+ end
+
+ def eth_html
+ return "" unless eth_address
+
+ <<~HTML
+
+
Ethereum (ETH)
+
Address: #{eth_address}
+
Loading...
+
+ HTML
+ end
+
+ def usdt_html
+ return "" unless usdt_address
+
+ <<~HTML
+
+
USDT (TRC-20)
+
Address: #{usdt_address}
+
Loading...
+
+ 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
Support Us with Crypto Donations
#{@text}
-
-
Bitcoin (BTC)
-
#{btc_address}
-
Loading...
-
-
-
Ethereum (ETH)
-
#{eth_address}
-
Loading...
-
-
-
USDT (TRC-20)
-
Address: #{usdt_address}
-
Loading...
-
+ HTML
+ end
+
+ def closing_html
+ <<~HTML
+ 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
- });
-
- 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
diff --git a/lib/jekyll-crypto-donations/version.rb b/lib/jekyll-crypto-donations/version.rb
index 549bf11..03ded8d 100644
--- a/lib/jekyll-crypto-donations/version.rb
+++ b/lib/jekyll-crypto-donations/version.rb
@@ -2,6 +2,6 @@
module Jekyll
module CryptoDonations
- VERSION = "0.1.0"
+ VERSION = "0.1.1"
end
end
diff --git a/spec/jekyll_crypto_donations_spec.rb b/spec/jekyll_crypto_donations_spec.rb
new file mode 100644
index 0000000..474d62c
--- /dev/null
+++ b/spec/jekyll_crypto_donations_spec.rb
@@ -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
diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb
index 8dfd334..7df9f87 100644
--- a/spec/spec_helper.rb
+++ b/spec/spec_helper.rb
@@ -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