Design updates with error handling
This commit is contained in:
1 parent
4c73608549
commit
4b84df0af9
9 files changed
+124
-38
No files matched your search
@@ -2,6 +2,8 @@
|
||||
|
||||
module StrikeMe
|
||||
module API
|
||||
|
||||
# Factory helper object connecting the client to the models
|
||||
class Account
|
||||
include BaseClient
|
||||
|
||||
@@ -12,14 +14,15 @@ module StrikeMe
|
||||
def profile
|
||||
return @profile if @profile.present?
|
||||
|
||||
response = client.get profile_url
|
||||
url = profile_url
|
||||
response = client.get url
|
||||
#body = response.body.presence
|
||||
#body = JSON.parse(body) if body.is_a? String
|
||||
if response.success?
|
||||
body = response.body
|
||||
body = JSON.parse(body) if body.is_a? String
|
||||
@profile = Profile.new body.merge(account: self)
|
||||
@profile = Profile.new response.body.merge(account: self)
|
||||
else
|
||||
# if you see this, something went wrong, this is test code
|
||||
@profile = ResponseError.new(profile_url, response)
|
||||
raise ResponseError.new(url, response.body)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -36,12 +39,11 @@ module StrikeMe
|
||||
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
|
||||
if response.success?
|
||||
@balances = Balances.new self, response.body
|
||||
else
|
||||
raise ResponseError.new('balances', response.body)
|
||||
end
|
||||
end
|
||||
|
||||
def inspect
|
||||
|
||||
@@ -21,6 +21,10 @@ module StrikeMe
|
||||
def to_a
|
||||
@items.map(&:attributes)
|
||||
end
|
||||
|
||||
def btc
|
||||
@items.find { |item| item.currency == 'BTC' }
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -19,13 +19,21 @@ module StrikeMe
|
||||
end
|
||||
|
||||
# cumbersome, but better for later debugging and handling
|
||||
class ResponseError
|
||||
attr_reader :url, :faraday_response
|
||||
class ResponseError < StandardError
|
||||
attr_reader :url, :response_body, :status, :reason
|
||||
|
||||
def initialize(url, faraday_response)
|
||||
def initialize(url, response_body)
|
||||
@reason = response_body.try(:[], 'data').try(:[], 'message')
|
||||
@status = response_body.try(:[], 'data').try(:[], 'status')
|
||||
|
||||
super "[#{status}, {}, [\"#{reason}\"]]" if reason
|
||||
@url = url
|
||||
@faraday_response = faraday_response
|
||||
@response_body = response_body
|
||||
end
|
||||
|
||||
# def inspect
|
||||
# "StrikeMe::API::BaseClient::ResponseError: #{url} status: #{faraday_response.status}"
|
||||
# end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -3,9 +3,7 @@
|
||||
module StrikeMe
|
||||
module API
|
||||
class Client
|
||||
attr_reader :version, :connection
|
||||
|
||||
delegate :get, to: :connection
|
||||
attr_reader :version, :connection, :last_response
|
||||
|
||||
def initialize(**args)
|
||||
@version = args[:version] || 'v1'
|
||||
@@ -23,6 +21,11 @@ module StrikeMe
|
||||
builder.response :json
|
||||
end
|
||||
end
|
||||
|
||||
#delegate :get, to: :connection
|
||||
def get(...)
|
||||
@last_response = connection.get(...)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -24,8 +24,7 @@ module StrikeMe
|
||||
class BTC
|
||||
class Error < StandardError; end
|
||||
|
||||
MAX_NUMBER_OF_SATS = 21_000_000 * 100_000_000
|
||||
NUMERICAL_OPERATORS = %i[* /].freeze
|
||||
MAX_NUMBER_OF_SATS = 21_000_000 * 100_000_000
|
||||
|
||||
attr_reader :sats
|
||||
|
||||
|
||||
@@ -7,7 +7,7 @@ module StrikeMe
|
||||
end
|
||||
|
||||
def cast(value)
|
||||
StrikeMe::BTC.new((100_000_000 * value.to_f).to_i)
|
||||
StrikeMe::BTC.new((100_000_000 * value.to_f).round)
|
||||
end
|
||||
|
||||
def serialize(value)
|
||||
@@ -16,4 +16,4 @@ module StrikeMe
|
||||
end
|
||||
end
|
||||
|
||||
ActiveModel::Type.register(:btc, StrikeMe::BtcAttributeType)
|
||||
ActiveModel::Type.register(:strike_btc, StrikeMe::BtcAttributeType)
|
||||
Reference in new issue
Block a user