commit 4b5f7906d72ffe256f3cefe27b9b46d2bab047be Author: Benjamin ter Kuile Date: Mon Sep 15 15:55:58 2025 -0500 Initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b04a8c8 --- /dev/null +++ b/.gitignore @@ -0,0 +1,11 @@ +/.bundle/ +/.yardoc +/_yardoc/ +/coverage/ +/doc/ +/pkg/ +/spec/reports/ +/tmp/ + +# rspec failure tracking +.rspec_status diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml new file mode 100644 index 0000000..cbbd44c --- /dev/null +++ b/.gitlab-ci.yml @@ -0,0 +1,10 @@ +default: + image: ruby:3.4.1 + + before_script: + - gem install bundler -v 2.6.2 + - bundle install + +example_job: + script: + - bundle exec rake diff --git a/.rspec b/.rspec new file mode 100644 index 0000000..34c5164 --- /dev/null +++ b/.rspec @@ -0,0 +1,3 @@ +--format documentation +--color +--require spec_helper diff --git a/.rubocop.yml b/.rubocop.yml new file mode 100644 index 0000000..b493c8d --- /dev/null +++ b/.rubocop.yml @@ -0,0 +1,15 @@ +AllCops: + TargetRubyVersion: 3.1 + +Style/StringLiterals: + EnforcedStyle: single_quotes + +Style/StringLiteralsInInterpolation: + EnforcedStyle: double_quotes + +Style/Documentation: + Enabled: false + +Metrics/BlockLength: + Exclude: + - 'spec/**/*' diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..522d1f6 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,5 @@ +## [Unreleased] + +## [0.1.0] - 2025-08-15 + +- Initial release diff --git a/Gemfile b/Gemfile new file mode 100644 index 0000000..4cc0e6c --- /dev/null +++ b/Gemfile @@ -0,0 +1,16 @@ +# frozen_string_literal: true + +source 'https://rubygems.org' + +# Specify your gem's dependencies in strike_me-api.gemspec +gemspec + +gem 'rake', '~> 13.0' + +gem 'rspec', '~> 3.0' + +gem 'rubocop', '~> 1.21' + +gem 'pry' +gem 'reline' +gem 'webmock' diff --git a/Gemfile.lock b/Gemfile.lock new file mode 100644 index 0000000..6c1a2ee --- /dev/null +++ b/Gemfile.lock @@ -0,0 +1,129 @@ +PATH + remote: . + specs: + strike_me-api (0.1.0) + activemodel + activesupport + faraday + +GEM + remote: https://rubygems.org/ + specs: + activemodel (8.0.2.1) + activesupport (= 8.0.2.1) + activesupport (8.0.2.1) + base64 + benchmark (>= 0.3) + bigdecimal + concurrent-ruby (~> 1.0, >= 1.3.1) + connection_pool (>= 2.2.5) + drb + i18n (>= 1.6, < 2) + logger (>= 1.4.2) + minitest (>= 5.1) + securerandom (>= 0.3) + tzinfo (~> 2.0, >= 2.0.5) + uri (>= 0.13.1) + addressable (2.8.7) + public_suffix (>= 2.0.2, < 7.0) + ast (2.4.3) + base64 (0.3.0) + benchmark (0.4.1) + bigdecimal (3.2.2) + coderay (1.1.3) + concurrent-ruby (1.3.5) + connection_pool (2.5.3) + crack (1.0.0) + bigdecimal + rexml + diff-lcs (1.6.2) + drb (2.2.3) + faraday (2.13.4) + faraday-net_http (>= 2.0, < 3.5) + json + logger + faraday-net_http (3.4.1) + net-http (>= 0.5.0) + hashdiff (1.2.0) + i18n (1.14.7) + concurrent-ruby (~> 1.0) + io-console (0.8.1) + json (2.13.2) + language_server-protocol (3.17.0.5) + lint_roller (1.1.0) + logger (1.7.0) + method_source (1.1.0) + minitest (5.25.5) + net-http (0.6.0) + uri + parallel (1.27.0) + parser (3.3.9.0) + ast (~> 2.4.1) + racc + prism (1.4.0) + pry (0.15.2) + coderay (~> 1.1) + method_source (~> 1.0) + public_suffix (6.0.2) + racc (1.8.1) + rainbow (3.1.1) + rake (13.3.0) + regexp_parser (2.11.2) + reline (0.6.2) + io-console (~> 0.5) + rexml (3.4.1) + rspec (3.13.1) + rspec-core (~> 3.13.0) + rspec-expectations (~> 3.13.0) + rspec-mocks (~> 3.13.0) + rspec-core (3.13.5) + rspec-support (~> 3.13.0) + rspec-expectations (3.13.5) + diff-lcs (>= 1.2.0, < 2.0) + rspec-support (~> 3.13.0) + rspec-mocks (3.13.5) + diff-lcs (>= 1.2.0, < 2.0) + rspec-support (~> 3.13.0) + rspec-support (3.13.4) + rubocop (1.79.2) + json (~> 2.3) + language_server-protocol (~> 3.17.0.2) + lint_roller (~> 1.1.0) + parallel (~> 1.10) + parser (>= 3.3.0.2) + rainbow (>= 2.2.2, < 4.0) + regexp_parser (>= 2.9.3, < 3.0) + rubocop-ast (>= 1.46.0, < 2.0) + ruby-progressbar (~> 1.7) + unicode-display_width (>= 2.4.0, < 4.0) + rubocop-ast (1.46.0) + parser (>= 3.3.7.2) + prism (~> 1.4) + ruby-progressbar (1.13.0) + securerandom (0.4.1) + tzinfo (2.0.6) + concurrent-ruby (~> 1.0) + unicode-display_width (3.1.5) + unicode-emoji (~> 4.0, >= 4.0.4) + unicode-emoji (4.0.4) + uri (1.0.3) + webmock (3.25.1) + addressable (>= 2.8.0) + crack (>= 0.3.2) + hashdiff (>= 0.4.0, < 2.0.0) + +PLATFORMS + aarch64-linux + ruby + +DEPENDENCIES + pry + rake (~> 13.0) + reline + rspec (~> 3.0) + rubocop (~> 1.21) + strike_me-api! + webmock + +BUNDLED WITH + 2.6.2 diff --git a/LICENSE.txt b/LICENSE.txt new file mode 100644 index 0000000..be836cc --- /dev/null +++ b/LICENSE.txt @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) 2025 Benjamin ter Kuile + +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. diff --git a/README.md b/README.md new file mode 100644 index 0000000..209c45b --- /dev/null +++ b/README.md @@ -0,0 +1,37 @@ +# StrikeMe::API + +This is a ruby gem to interact with the strike.me API. It is an inition proof of concept implementation. + +## Installation + +TODO: Replace `strike_me-api` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org. + +Install the gem and add to the application's Gemfile by executing: + +```bash +bundle add strike_me-api +``` + +If bundler is not being used to manage dependencies, install the gem by executing: + +```bash +gem install strike_me-api +``` + +## Usage + +TODO: Write usage instructions here + +## Development + +After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment. + +To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org). + +## Contributing + +Bug reports and pull requests are welcome on GitHub at https://gitlab.com/benja-2/strike_me-api. + +## License + +The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT). diff --git a/Rakefile b/Rakefile new file mode 100644 index 0000000..4964751 --- /dev/null +++ b/Rakefile @@ -0,0 +1,12 @@ +# frozen_string_literal: true + +require 'bundler/gem_tasks' +require 'rspec/core/rake_task' + +RSpec::Core::RakeTask.new(:spec) + +require 'rubocop/rake_task' + +RuboCop::RakeTask.new + +task default: %i[spec rubocop] diff --git a/bin/console b/bin/console new file mode 100755 index 0000000..3f2ba89 --- /dev/null +++ b/bin/console @@ -0,0 +1,11 @@ +#!/usr/bin/env ruby +# frozen_string_literal: true + +require 'bundler/setup' +require 'strike_me/api' + +# You can add fixtures and/or initialization code here to make experimenting +# with your gem easier. You can also use a different console, if you like. + +require 'irb' +IRB.start(__FILE__) diff --git a/bin/setup b/bin/setup new file mode 100755 index 0000000..dce67d8 --- /dev/null +++ b/bin/setup @@ -0,0 +1,8 @@ +#!/usr/bin/env bash +set -euo pipefail +IFS=$'\n\t' +set -vx + +bundle install + +# Do any other automated setup that you need to do here diff --git a/lib/strike_me/api.rb b/lib/strike_me/api.rb new file mode 100644 index 0000000..33b844a --- /dev/null +++ b/lib/strike_me/api.rb @@ -0,0 +1,34 @@ +# frozen_string_literal: true + +require 'pry' + +# External +require 'active_support/all' +require 'active_model' +require 'faraday' + +# Internal +require_relative 'api/version' +require_relative 'btc' +require_relative 'btc_attribute_type' +# require_relative 'api/base_model' +# require_relative 'api/invoice_response' + +ActiveSupport::Inflector.inflections(:en) { |inflect| inflect.acronym 'API' } + +module StrikeMe + module API + class Error < StandardError; end + + def self.autoload_default(name) + autoload name, "#{"#{self.name}/#{name}".underscore}.rb" + end + + # autoload :InvoiceResponse, 'strike_me/api/invoice_response.rb' + autoload_default :Client + autoload_default :BaseClient + autoload_default :BaseModel + autoload_default :Account + autoload_default :InvoiceResponse + end +end diff --git a/lib/strike_me/api/account.rb b/lib/strike_me/api/account.rb new file mode 100644 index 0000000..a794c16 --- /dev/null +++ b/lib/strike_me/api/account.rb @@ -0,0 +1,52 @@ +# frozen_string_literal: true + +module StrikeMe + module API + class Account + include BaseClient + + autoload_default :Profile + autoload_default :Balances + autoload_default :Balance + + def profile + return @profile if @profile.present? + + response = client.get profile_url + if response.success? + body = response.body + body = JSON.parse(body) if body.is_a? String + @profile = Profile.new body.merge(account: self) + else + # if you see this, something went wrong, this is test code + @profile = ResponseError.new(profile_url, response) + end + end + + def profile_url + if (account_id = ENV['STRIKE_ACCOUNT_ID'].presence) + "accounts/#{account_id}/profile" + else + account_handle = ENV.fetch('STRIKE_ACCOUNT_HANDLE') + "accounts/handles/#{account_handle}/profile" + end + end + + def balances + return @balances if @balances.present? + + response = client.get 'balances' + @balances = if response.success? + Balances.new self, response.body + else + # if you see this, something went wrong, this is test code + ResponseError.new('balances', response) + end + end + + def inspect + "#" + end + end + end +end diff --git a/lib/strike_me/api/account/balance.rb b/lib/strike_me/api/account/balance.rb new file mode 100644 index 0000000..4a75a44 --- /dev/null +++ b/lib/strike_me/api/account/balance.rb @@ -0,0 +1,17 @@ +# frozen_string_literal: true + +module StrikeMe + module API + class Account + class Balance < BaseModel + attribute :currency + attribute :current, :float + attribute :pending, :float + attribute :outgoing, :float + attribute :reserved, :float + attribute :available, :float + attribute :total, :float + end + end + end +end diff --git a/lib/strike_me/api/account/balances.rb b/lib/strike_me/api/account/balances.rb new file mode 100644 index 0000000..0f0e874 --- /dev/null +++ b/lib/strike_me/api/account/balances.rb @@ -0,0 +1,27 @@ +# frozen_string_literal: true + +module StrikeMe + module API + class Account + class Balances + include Enumerable + + attr_reader :account + + def initialize(account, items) + @account = account + # Map hash to object + @items = items.map { |item| Balance.new item } + end + + def each(&block) + @items.each(&block) + end + + def to_a + @items.map(&:attributes) + end + end + end + end +end diff --git a/lib/strike_me/api/account/profile.rb b/lib/strike_me/api/account/profile.rb new file mode 100644 index 0000000..81d5576 --- /dev/null +++ b/lib/strike_me/api/account/profile.rb @@ -0,0 +1,21 @@ +# frozen_string_literal: true + +module StrikeMe + module API + class Account + class Profile < BaseModel + attribute :account + attribute :id + attribute :handle + attribute :canReceive + attribute :currencies + + delegate :balances, to: :account + + def can_receive? + canReceive.present? + end + end + end + end +end diff --git a/lib/strike_me/api/base_client.rb b/lib/strike_me/api/base_client.rb new file mode 100644 index 0000000..7a3dae3 --- /dev/null +++ b/lib/strike_me/api/base_client.rb @@ -0,0 +1,32 @@ +# frozen_string_literal: true + +module StrikeMe + module API + # The BaseClient module is to be included in objects using the Strike client + # to make calls and transform the responses to objects. + # Factory of BaseModel instances + module BaseClient + extend ActiveSupport::Concern + + def client + @client ||= Client.new + end + + module ClassMethods + def autoload_default(name) + autoload name, "#{"#{self.name}/#{name}".underscore}.rb" + end + end + + # cumbersome, but better for later debugging and handling + class ResponseError + attr_reader :url, :faraday_response + + def initialize(url, faraday_response) + @url = url + @faraday_response = faraday_response + end + end + end + end +end diff --git a/lib/strike_me/api/base_model.rb b/lib/strike_me/api/base_model.rb new file mode 100644 index 0000000..a1ebdc4 --- /dev/null +++ b/lib/strike_me/api/base_model.rb @@ -0,0 +1,10 @@ +# frozen_string_literal: true + +module StrikeMe + module API + class BaseModel + include ActiveModel::Model + include ActiveModel::Attributes + end + end +end diff --git a/lib/strike_me/api/client.rb b/lib/strike_me/api/client.rb new file mode 100644 index 0000000..0763b97 --- /dev/null +++ b/lib/strike_me/api/client.rb @@ -0,0 +1,32 @@ +# frozen_string_literal: true + +module StrikeMe + module API + class Client + attr_reader :version, :connection + + delegate :get, to: :connection + + def initialize(**args) + @version = args[:version] || 'v1' + @connection_args = args.slice(:url, :params, :headers).reverse_merge( + url: "https://api.strike.me/#{version}" + ) + + # headers: { + # 'Content-Type' => 'application/json', + # 'Authorization' => "Bearer #{ENV.fetch('STRIKE_API_KEY')}" + # } + @connection = Faraday.new(@connection_args) do |builder| + builder.request :authorization, 'Bearer', -> { ENV.fetch('STRIKE_API_KEY') } + builder.request :json + builder.response :json + end + end + + def account_id + ENV.fetch('STRIKE_ACCOUNT_ID') + end + end + end +end diff --git a/lib/strike_me/api/invoice_response.rb b/lib/strike_me/api/invoice_response.rb new file mode 100644 index 0000000..0dfd974 --- /dev/null +++ b/lib/strike_me/api/invoice_response.rb @@ -0,0 +1,11 @@ +# frozen_string_literal: true + +module StrikeMe + module API + class InvoiceResponse < BaseModel + attr_accessor :ok, :checking_id, :payment_request, :error_message + + validates :ok, presence: true + end + end +end diff --git a/lib/strike_me/api/version.rb b/lib/strike_me/api/version.rb new file mode 100644 index 0000000..2c4a137 --- /dev/null +++ b/lib/strike_me/api/version.rb @@ -0,0 +1,7 @@ +# frozen_string_literal: true + +module StrikeMe + module API + VERSION = '0.1.0' + end +end diff --git a/lib/strike_me/btc.rb b/lib/strike_me/btc.rb new file mode 100644 index 0000000..dbab137 --- /dev/null +++ b/lib/strike_me/btc.rb @@ -0,0 +1,82 @@ +# frozen_string_literal: true + +class Float + def btc + StrikeMe::BTC.new((self * 100_000_000).round) + end + + def sats + StrikeMe::BTC.new(round) + end +end + +class Integer + def btc + StrikeMe::BTC.new(self * 100_000_000) + end + + def sats + StrikeMe::BTC.new(self) + end +end + +module StrikeMe + class BTC + class Error < StandardError; end + + MAX_NUMBER_OF_SATS = 21_000_000 * 100_000_000 + NUMERICAL_OPERATORS = %i[* /].freeze + + attr_reader :sats + + def initialize(sats) + raise Error, 'Must be initialized using an integer representing sats' unless sats.is_a? Integer + raise Error, "Max sats is: #{MAX_NUMBER_OF_SATS}. You have: #{sats}" if sats > MAX_NUMBER_OF_SATS + raise Error, 'Must be initialized using a positive number' if sats.negative? + + @sats = sats + end + + def to_s + # to_f.to_s + format('%.8f', to_f) + end + + def to_f + sats.to_f / 100_000_000 + end + + def as_json + to_s + end + + def *(other) + self.class.new((sats * other).to_i) + end + + def /(other) + self.class.new((sats / other).to_i) + end + + def +(other) + raise Error, "Addition can only be performed using a #{self.class.name} object" unless other.is_a?(self.class) + + self.class.new(sats + other.sats) + end + + def -(other) + raise Error, "Addition can only be performed using a #{self.class.name} object" unless other.is_a?(self.class) + + self.class.new(sats - other.sats) + end + + def ==(other) + case other + when Integer then sats == other + when BTC then sats == other.sats + else + false + end + end + end +end diff --git a/lib/strike_me/btc_attribute_type.rb b/lib/strike_me/btc_attribute_type.rb new file mode 100644 index 0000000..b6c377a --- /dev/null +++ b/lib/strike_me/btc_attribute_type.rb @@ -0,0 +1,19 @@ +# frozen_string_literal: true + +module StrikeMe + class BtcAttributeType < ActiveModel::Type::Value + def type + :btc + end + + def cast(value) + StrikeMe::BTC.new((100_000_000 * value.to_f).to_i) + end + + def serialize(value) + value.to_s + end + end +end + +ActiveModel::Type.register(:btc, StrikeMe::BtcAttributeType) diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb new file mode 100644 index 0000000..d1fc4b0 --- /dev/null +++ b/spec/spec_helper.rb @@ -0,0 +1,18 @@ +# frozen_string_literal: true + +require 'strike_me/api' +require 'webmock/rspec' + +WebMock.disable_net_connect!(allow_localhost: true) # Prevent real network requests during tests + +RSpec.configure do |config| + # Enable flags like --only-failures and --next-failure + config.example_status_persistence_file_path = '.rspec_status' + + # Disable RSpec exposing methods globally on `Module` and `main` + config.disable_monkey_patching! + + config.expect_with :rspec do |c| + c.syntax = :expect + end +end diff --git a/spec/strike_me/api/account/balances_spec.rb b/spec/strike_me/api/account/balances_spec.rb new file mode 100644 index 0000000..9c50e5b --- /dev/null +++ b/spec/strike_me/api/account/balances_spec.rb @@ -0,0 +1,54 @@ +# frozen_string_literal: true + +RSpec.describe StrikeMe::API::Account::Balances do + subject { StrikeMe::API::Account.new.balances } + context 'good response' do + let(:server_response) do + [ + { + 'currency' => 'BTC', + 'current' => '0.003169', + 'pending' => '0', + 'outgoing' => '0', + 'reserved' => '0', + 'available' => '0.003169', + 'total' => '0.00316976' + }, + { + 'currency' => 'USDT', + 'current' => '50.73', + 'pending' => '0.00', + 'outgoing' => '0.00', + 'reserved' => '0.00', + 'available' => '50.73', + 'total' => '50.73' + } + ] + end + before { allow_any_instance_of(StrikeMe::API::Client).to receive(:get).and_return double(success?: true, body: server_response) } + + it '#to_a returns a simple object representation, where number string inputs are real numbers' do + expected = [ + { + 'currency' => 'BTC', + 'current' => 0.003169, + 'pending' => 0.0, + 'outgoing' => 0.0, + 'reserved' => 0.0, + 'available' => 0.003169, + 'total' => 0.00316976 + }, + { + 'currency' => 'USDT', + 'current' => 50.73, + 'pending' => 0.0, + 'outgoing' => 0.0, + 'reserved' => 0.0, + 'available' => 50.73, + 'total' => 50.73 + } + ] + expect(subject.to_a).to eq expected + end + end +end diff --git a/spec/strike_me/api/account/profile_spec.rb b/spec/strike_me/api/account/profile_spec.rb new file mode 100644 index 0000000..60ec54b --- /dev/null +++ b/spec/strike_me/api/account/profile_spec.rb @@ -0,0 +1,19 @@ +# frozen_string_literal: true + +RSpec.describe StrikeMe::API::Account::Profile do + context 'good response' do + let(:server_response) do + { + 'id' => '02ad532c-2bd0-4b4b-8c06-a2b2dd3f2b0c', + 'handle' => 'strikerrr3', + 'canReceive' => true, + 'currencies' => [ + { 'currency' => 'USDT', 'isDefaultCurrency' => false, 'isAvailable' => true, 'isInvoiceable' => true }, + { 'currency' => 'BTC', 'isDefaultCurrency' => true, 'isAvailable' => true, 'isInvoiceable' => true } + ] + } + end + before { allow_any_instance_of(StrikeMe::API::Client).to receive(:get).and_return double(success?: true, body: server_response) } + end + subject { described_class.new } +end diff --git a/spec/strike_me/api/account_spec.rb b/spec/strike_me/api/account_spec.rb new file mode 100644 index 0000000..aecff38 --- /dev/null +++ b/spec/strike_me/api/account_spec.rb @@ -0,0 +1,34 @@ +# frozen_string_literal: true + +RSpec.describe StrikeMe::API::Account do + subject { described_class.new } + + describe 'profile' do + subject { described_class.new.profile } + + it 'works with STRIKE_ACCOUNT_ID' do + allow(ENV).to receive(:[]) + expect(ENV).to receive(:[]).with('STRIKE_ACCOUNT_ID').and_return 'abc' + stub_request(:get, %r{https://api.strike.me/v1/accounts/abc/profile}) + .to_return(status: 200, body: '{"id":"abc44"}') + expect(subject.id).to eq 'abc44' + end + + it 'works with STRIKE_ACCOUNT_HANDLE without account id' do + allow(ENV).to receive(:[]) + allow(ENV).to receive(:fetch) + expect(ENV).to receive(:fetch).with('STRIKE_ACCOUNT_HANDLE').and_return 'abcd' + stub_request(:get, %r{https://api.strike.me/v1/accounts/handles/abcd/profile}) + .to_return(status: 200, body: '{"id":"abc44"}') + expect(subject.id).to eq 'abc44' + end + end + + describe 'balances' do + subject { described_class.new.balances } + + before { stub_request(:get, 'https://api.strike.me/v1/balances').to_return(status: 200, body: []) } + + it { is_expected.to be_an_instance_of StrikeMe::API::Account::Balances } + end +end diff --git a/spec/strike_me/api/client_spec.rb b/spec/strike_me/api/client_spec.rb new file mode 100644 index 0000000..8dc58cb --- /dev/null +++ b/spec/strike_me/api/client_spec.rb @@ -0,0 +1,9 @@ +# frozen_string_literal: true + +RSpec.describe StrikeMe::API::Client do + subject { described_class.new } + + it 'does things', pending: 'await more structure' do + expect(2).to eq 3 + end +end diff --git a/spec/strike_me/api/invoice_response_spec.rb b/spec/strike_me/api/invoice_response_spec.rb new file mode 100644 index 0000000..8bf3634 --- /dev/null +++ b/spec/strike_me/api/invoice_response_spec.rb @@ -0,0 +1,20 @@ +# frozen_string_literal: true + +RSpec.describe StrikeMe::API::InvoiceResponse do + let(:valid_attributes) { { ok: true, checking_id: 'abc1', payment_request: 'abc2', error_message: 'abc3' } } + let(:invalid_attributes) { valid_attributes.merge(ok: false) } + + context 'valid response' do + subject { described_class.new(valid_attributes) } + + it { is_expected.to be_a StrikeMe::API::BaseModel } + it { is_expected.to be_valid } + it { is_expected.to have_attributes checking_id: 'abc1', payment_request: 'abc2', error_message: 'abc3' } + end + + context 'invalid response' do + subject { described_class.new(invalid_attributes) } + + it { is_expected.to_not be_valid } + end +end diff --git a/spec/strike_me/api_spec.rb b/spec/strike_me/api_spec.rb new file mode 100644 index 0000000..c70907c --- /dev/null +++ b/spec/strike_me/api_spec.rb @@ -0,0 +1,7 @@ +# frozen_string_literal: true + +RSpec.describe StrikeMe::API do + it 'has a version number' do + expect(StrikeMe::API::VERSION).not_to be nil + end +end diff --git a/spec/strike_me/btc_spec.rb b/spec/strike_me/btc_spec.rb new file mode 100644 index 0000000..ef5b40e --- /dev/null +++ b/spec/strike_me/btc_spec.rb @@ -0,0 +1,108 @@ +# frozen_string_literal: true + +RSpec.describe StrikeMe::BTC do + context 'create from integer' do + it 'creates using #btc' do + btc = 314.btc + + expect(btc).to be_a described_class + expect(btc.sats).to eq 314 * 100_000_000 + end + + it 'creates using #sats' do + btc = 314.sats + + expect(btc).to be_a described_class + expect(btc.sats).to eq 314 + end + end + + context 'create from float' do + it 'creates using #btc' do + btc = 314.85.btc + + expect(btc).to be_a described_class + expect(btc.sats).to eq 31_485_000_000 + end + + it 'creates using #sats' do + btc = 314.85.sats + + expect(btc).to be_a described_class + expect(btc.sats).to eq 315 + end + end + + it 'properly formats to string' do + expect(described_class.new(234_522).to_s).to eq '0.00234522' + end + + it 'formats properly in json structures' do + btc = described_class.new(234_522) + expect({ amount: btc }.to_json).to eq '{"amount":"0.00234522"}' + end + + context 'arithmetic' do + it 'supports multiplication with numbers' do + btc1 = described_class.new(234_529) + btc2 = btc1 * 3 + btc3 = btc2 * 2.9 + + expect(btc2).to be_a described_class + expect(btc3).to be_a described_class + + expect(btc2).not_to eq btc1 + expect(btc2).not_to eq btc3 + + expect(btc1.sats).to eq 234_529 + expect(btc1).to eq 234_529 + expect(btc2).to eq 703_587 + expect(btc3).to eq 2_040_402 + end + + it 'supports division with numbers' do + btc1 = described_class.new(34_529) + btc2 = btc1 / 3 + btc3 = btc2 / 2.9 + + expect(btc2).to be_a described_class + expect(btc3).to be_a described_class + + expect(btc2).not_to eq btc1 + expect(btc2).not_to eq btc3 + + expect(btc1.sats).to eq 34_529 + expect(btc1).to eq 34_529 + expect(btc2).to eq 11_509 + expect(btc3).to eq 3_968 + end + + it 'supports addition with other BTC objects' do + btc1 = described_class.new(34_529) + btc2 = described_class.new(2_556) + btc3 = btc1 + btc2 + + expect { btc1 + 23 }.to raise_error StrikeMe::BTC::Error + + expect(btc3).to be_a described_class + expect(btc3).to eq 37_085 + end + + it 'supports subtraction with other BTC objects' do + btc1 = described_class.new(34_529) + btc2 = described_class.new(2_556) + btc3 = btc1 - btc2 + + expect { btc1 - 23 }.to raise_error StrikeMe::BTC::Error + + expect(btc3).to be_a described_class + expect(btc3).to eq 31_973 + end + end + + it 'is consistent for small values' do + string = 3.sats.to_s + expect(string).to eq '0.00000003' + expect(string.to_f.btc.sats).to eq 3 + end +end diff --git a/strike_me-api.gemspec b/strike_me-api.gemspec new file mode 100644 index 0000000..3ed48d8 --- /dev/null +++ b/strike_me-api.gemspec @@ -0,0 +1,42 @@ +# frozen_string_literal: true + +require_relative 'lib/strike_me/api/version' + +Gem::Specification.new do |spec| + spec.name = 'strike_me-api' + spec.version = StrikeMe::API::VERSION + spec.authors = ['Benjamin ter Kuile'] + spec.email = ['bterkuile@gmail.com'] + + spec.summary = 'TODO: Write a short summary, because RubyGems requires one.' + spec.description = 'TODO: Write a longer description or delete this line.' + spec.homepage = 'TODO: Put your gem website or public repo URL here.' + spec.license = 'MIT' + spec.required_ruby_version = '>= 3.1.0' + + spec.metadata['homepage_uri'] = spec.homepage + spec.metadata['source_code_uri'] = 'https://gitlab.com/benja-2/strike_me-api' + spec.metadata['changelog_uri'] = 'TODO: Put your gem CHANGELOG.md URL here.' + + # Specify which files should be added to the gem when it is released. + # The `git ls-files -z` loads the files in the RubyGem that have been added into git. + gemspec = File.basename(__FILE__) + spec.files = IO.popen(%w[git ls-files -z], chdir: __dir__, err: IO::NULL) do |ls| + ls.readlines("\x0", chomp: true).reject do |f| + (f == gemspec) || + f.start_with?(*%w[bin/ test/ spec/ features/ .git .gitlab-ci.yml appveyor Gemfile]) + end + end + spec.bindir = 'exe' + spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) } + spec.require_paths = ['lib'] + + # Uncomment to register a new dependency of your gem + # spec.add_dependency 'example-gem', '~> 1.0' + spec.add_dependency 'activemodel' + spec.add_dependency 'activesupport' + spec.add_dependency 'faraday' + + # For more information and examples about making a new gem, check out our + # guide at: https://bundler.io/guides/creating_gem.html +end