Initial commit
This commit is contained in:
commit
4b5f7906d7
33 files changed
+933
No files matched your search
@@ -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
|
||||
"#<StrikeMe::API::Account profile: #{@profile}, balances: #{@balances}>"
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -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
|
||||
@@ -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
|
||||
@@ -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
|
||||
@@ -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
|
||||
@@ -0,0 +1,10 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module StrikeMe
|
||||
module API
|
||||
class BaseModel
|
||||
include ActiveModel::Model
|
||||
include ActiveModel::Attributes
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -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
|
||||
@@ -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
|
||||
@@ -0,0 +1,7 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module StrikeMe
|
||||
module API
|
||||
VERSION = '0.1.0'
|
||||
end
|
||||
end
|
||||
Reference in new issue
Block a user