Skip to content

Commit

Permalink
Merge pull request #6 from MortarStone/deb/add_exceptions
Browse files Browse the repository at this point in the history
Add exceptions
  • Loading branch information
debok authored Jul 3, 2024
2 parents 95d8943 + 8f9c603 commit d707c90
Show file tree
Hide file tree
Showing 31 changed files with 344 additions and 277 deletions.
30 changes: 15 additions & 15 deletions .rspec_status
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
example_id | status | run_time |
-------------------------------------------------- | ------ | --------------- |
./spec/deka/client_spec.rb[1:1:1:1] | passed | 0.00168 seconds |
./spec/deka/client_spec.rb[1:1:2:1] | passed | 0.00481 seconds |
./spec/deka/endpoints/families_spec.rb[1:1:1] | passed | 0.00204 seconds |
./spec/deka/endpoints/families_spec.rb[1:1:2] | passed | 0.0032 seconds |
./spec/deka/endpoints/families_spec.rb[1:2:1] | passed | 0.00226 seconds |
./spec/deka/endpoints/organizations_spec.rb[1:1:1] | passed | 0.00175 seconds |
./spec/deka/endpoints/purposes_spec.rb[1:1:1] | passed | 0.00204 seconds |
./spec/deka/endpoints/purposes_spec.rb[1:1:2] | passed | 0.0018 seconds |
./spec/deka/endpoints/transactions_spec.rb[1:1:1] | passed | 0.00204 seconds |
./spec/deka/endpoints/transactions_spec.rb[1:1:2] | passed | 0.00262 seconds |
./spec/deka/endpoints/transactions_spec.rb[1:2:1] | passed | 0.00206 seconds |
./spec/deka/endpoints/users_spec.rb[1:1:1] | passed | 0.00269 seconds |
./spec/deka/endpoints/users_spec.rb[1:1:2] | passed | 0.00471 seconds |
./spec/deka/endpoints/users_spec.rb[1:2:1] | passed | 0.00218 seconds |
./spec/deka_spec.rb[1:1] | passed | 0.00083 seconds |
./spec/deka/client_spec.rb[1:1:1:1] | passed | 0.00242 seconds |
./spec/deka/client_spec.rb[1:1:2:1] | passed | 0.23839 seconds |
./spec/deka/endpoints/families_spec.rb[1:1:1] | passed | 0.24978 seconds |
./spec/deka/endpoints/families_spec.rb[1:1:2] | passed | 0.22075 seconds |
./spec/deka/endpoints/families_spec.rb[1:2:1] | passed | 0.24196 seconds |
./spec/deka/endpoints/organizations_spec.rb[1:1:1] | passed | 0.26343 seconds |
./spec/deka/endpoints/purposes_spec.rb[1:1:1] | passed | 0.22289 seconds |
./spec/deka/endpoints/purposes_spec.rb[1:1:2] | passed | 0.21893 seconds |
./spec/deka/endpoints/transactions_spec.rb[1:1:1] | passed | 0.26693 seconds |
./spec/deka/endpoints/transactions_spec.rb[1:1:2] | passed | 0.31352 seconds |
./spec/deka/endpoints/transactions_spec.rb[1:2:1] | passed | 0.24392 seconds |
./spec/deka/endpoints/users_spec.rb[1:1:1] | passed | 0.2215 seconds |
./spec/deka/endpoints/users_spec.rb[1:1:2] | passed | 0.2559 seconds |
./spec/deka/endpoints/users_spec.rb[1:2:1] | passed | 0.25194 seconds |
./spec/deka_spec.rb[1:1] | passed | 0.00128 seconds |
12 changes: 12 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,15 @@ git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }

# Specify your gem's dependencies in deka.gemspec
gemspec

group :development, :test do
gem 'bundler'
gem 'factory_bot'
gem 'pry'
gem 'rake'
gem 'rspec'
gem 'rubocop'
gem 'simplecov'
gem 'vcr'
gem 'webmock'
end
11 changes: 0 additions & 11 deletions deka.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ lib = File.expand_path("lib", __dir__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require "deka/version"

# rubocop:disable Metrics/BlockLength
Gem::Specification.new do |spec|
spec.name = "deka"
spec.version = Deka::VERSION
Expand Down Expand Up @@ -30,18 +29,8 @@ Gem::Specification.new do |spec|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
spec.require_paths = ["lib"]

spec.add_development_dependency "bundler"
spec.add_development_dependency 'factory_bot'
spec.add_development_dependency 'pry'
spec.add_development_dependency "rake"
spec.add_development_dependency "rspec"
spec.add_development_dependency 'rubocop'
spec.add_development_dependency 'simplecov'
spec.add_development_dependency 'vcr'
spec.add_development_dependency 'webmock'
spec.add_dependency 'activesupport'
spec.add_dependency 'dotenv'
spec.add_dependency 'faraday', '>= 1.0'
spec.metadata['rubygems_mfa_required'] = 'true'
end
# rubocop:enable Metrics/BlockLength
6 changes: 3 additions & 3 deletions examples/helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
Dotenv.load('../.env')

@client = Deka::Client.new(
personal_access_token: ENV['PERSONAL_ACCESS_TOKEN'],
organization_uuid: ENV['ORGANIZATION_UUID'],
environment: ENV['ENVIRONMENT']
personal_access_token: ENV.fetch('PERSONAL_ACCESS_TOKEN', nil),
organization_uuid: ENV.fetch('ORGANIZATION_UUID', nil),
environment: ENV.fetch('ENVIRONMENT', nil)
)

def print_no_access_message
Expand Down
4 changes: 4 additions & 0 deletions examples/not_found.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
require_relative 'helper'

user = @client.user(100_000_000)
print_user(user)
3 changes: 2 additions & 1 deletion examples/transactions.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require_relative 'helper'

transactions = @client.transactions
# transactions = @client.transactions('filter[status]' => 'success,settled,refunded')
transactions = @client.transactions('filter[status]' => 'failed,voided,pending,declined', 'page[number]' => 11)
print_transactions(transactions)
2 changes: 1 addition & 1 deletion examples/user_family.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require_relative 'helper'

family = @client.user_family(1)
family = @client.user_family(613)
print_family(family)
2 changes: 1 addition & 1 deletion examples/users.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require_relative 'helper'

users = @client.users
users = @client.users('filter[has_family]': false)
print_users(users)
1 change: 1 addition & 0 deletions lib/deka.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require_relative 'deka/client'
require_relative 'deka/exceptions'
require_relative 'deka/version'

module Deka
Expand Down
9 changes: 3 additions & 6 deletions lib/deka/auto_load.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
require 'faraday'
require 'json'

require_relative 'exceptions'
require_relative 'endpoints/families'
require_relative 'endpoints/organizations'
require_relative 'endpoints/purposes'
require_relative 'endpoints/transactions'
require_relative 'endpoints/users'
require_relative 'response_handler'

Dir[File.join(__dir__, 'endpoints', '*.rb')].each { |file| require file }
18 changes: 5 additions & 13 deletions lib/deka/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,33 +33,25 @@ def connection
end

def request(path, params = {})
res = connection.get do |req|
response = connection.get do |req|
req.url format_request(path, params)
req.headers['Content-Type'] = 'application/json'
req.headers['Accept'] = 'application/json'
req.options.timeout = 300 # 5 minutes
end

unless res.status == 200
raise Deka::Exceptions::ResponseError,
"#{res.status} #{res.reason_phrase}"
end

results = JSON.parse(res.body, symbolize_names: true)
results[:headers] = res.headers
results
ResponseHandler.new(response).call
end

def format_request(path, params = {})
"/v1/#{path}?#{format_get_params(params)}"
end

def format_get_params(params = {})
return if params.blank?
return if params.empty?

arr = []
params.each do |k, v|
arr << "#{k}=#{v}"
arr = params.map do |k, v|
"#{k}=#{v}"
end
arr.join('&')
end
Expand Down
19 changes: 17 additions & 2 deletions lib/deka/exceptions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,28 @@ module Exceptions
class DStandardError < StandardError
end

class InvalidInputError < DStandardError
class UnableToConnectError < DStandardError
end

class ConnectionError < DStandardError
class UnknownConnectingError < DStandardError
end

class BadCredentialsError < DStandardError
end

class MissingPermissionsError < DStandardError
end

class TooManyRequestsError < DStandardError
end

class ApiRateLimitError < DStandardError
end

class ResponseError < DStandardError
end

class NotFoundError < DStandardError
end
end
end
46 changes: 46 additions & 0 deletions lib/deka/response_handler.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# frozen_string_literal: true

module Deka
class ResponseHandler
attr_accessor :response

def initialize(response)
@response = response
end

def call
handle_response
end

private

def handle_response
case response.status
when 200..201
format_response
when 202..299
nil # nothing to do
when 400
raise Deka::Exceptions::BadRequestError, error_message
when 401
raise Deka::Exceptions::UnauthorizedError, error_message
when 403
raise Deka::Exceptions::ForbiddenError, error_message
when 404
raise Deka::Exceptions::NotFoundError, error_message
when 500
raise Deka::Exceptions::ResponseError, error_message
end
end

def error_message
"#{response.status} #{response.reason_phrase}"
end

def format_response
results = JSON.parse(response.body, symbolize_names: true)
results[:headers] = response.headers
results
end
end
end
4 changes: 2 additions & 2 deletions spec/deka/endpoints/families_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@
describe '#user_family', :vcr do
before do
@client = FactoryBot.build(:client)
@user_family = @client.user_family(1)
@user_family = @client.user_family(613)
end

it 'returns a user_family object' do
expect(@user_family[:data]).to be_a(Hash)
expect(@user_family[:data][:id]).to eq(1)
expect(@user_family[:data][:id]).to eq(610)
end
end
end
4 changes: 2 additions & 2 deletions spec/deka/endpoints/organizations_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

RSpec.describe Deka::Endpoints::Families do
describe '#organization', :vcr do
let(:organization_uuid) { ENV['ORGANIZATION_UUID'] }
let(:organization_uuid) { ENV.fetch('ORGANIZATION_UUID', nil) }

before do
@client = FactoryBot.build(:client)
Expand All @@ -11,7 +11,7 @@

it 'returns a organization object' do
expect(@organization[:data]).to be_a(Hash)
expect(@organization[:data][:id]).to eq(129)
expect(@organization[:data][:id]).to eq(118)
end
end
end
4 changes: 2 additions & 2 deletions spec/deka/endpoints/transactions_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@
describe '#transaction', :vcr do
before do
@client = FactoryBot.build(:client)
@transaction = @client.transaction(1)
@transaction = @client.transaction(13_168)
end

it 'returns a transaction object' do
expect(@transaction[:data]).to be_a(Hash)
expect(@transaction[:data][:id]).to eq(1)
expect(@transaction[:data][:id]).to eq(13_168)
end
end
end
4 changes: 2 additions & 2 deletions spec/deka/endpoints/users_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@
describe '#user', :vcr do
before do
@client = FactoryBot.build(:client)
@user = @client.user(1)
@user = @client.user(1520)
end

it 'returns a user object' do
expect(@user[:data]).to be_a(Hash)
expect(@user[:data][:id]).to eq(1)
expect(@user[:data][:id]).to eq(1520)
end
end
end
4 changes: 2 additions & 2 deletions spec/factories/clients.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
FactoryBot.define do
factory :client, class: Deka::Client do
organization_uuid { ENV['ORGANIZATION_UUID'] }
personal_access_token { ENV['PERSONAL_ACCESS_TOKEN'] }
organization_uuid { ENV.fetch('ORGANIZATION_UUID', nil) }
personal_access_token { ENV.fetch('PERSONAL_ACCESS_TOKEN', nil) }

initialize_with do
new(
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit d707c90

Please sign in to comment.