Skip to content

Commit 194d476

Browse files
committed
change to internal client
1 parent 7a8b16a commit 194d476

10 files changed

+46
-73
lines changed

.editorconfig

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# https://github.com/ruby/ruby/blob/master/.editorconfig
12
root = true
23

34
[*]

.gitignore

+5-1
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,8 @@
3838
.rvmrc
3939

4040
# Used by RuboCop. Remote config files pulled in from inherit_from directive.
41-
# .rubocop-https?--*
41+
# .rubocop-https?--*
42+
43+
# Editor-specific directories
44+
.vscode
45+
.idea

Guardfile

+3-39
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,6 @@
1-
# A sample Guardfile
21
# More info at https://github.com/guard/guard#readme
3-
4-
## Uncomment and set this to only include directories you want to watch
5-
# directories %w(app lib config test spec features) \
6-
# .select{|d| Dir.exist?(d) ? d : UI.warning("Directory #{d} does not exist")}
7-
8-
## Note: if you are using the `directories` clause above and you are not
9-
## watching the project directory ('.'), then you will want to move
10-
## the Guardfile to a watched dir and symlink it back, e.g.
11-
#
12-
# $ mkdir config
13-
# $ mv Guardfile config/
14-
# $ ln -s config/Guardfile .
15-
#
16-
# and, you'll have to watch "config/Guardfile" instead of "Guardfile"
17-
182
guard :minitest do
19-
# with Minitest::Unit
20-
watch(%r{^test/(.*)/?test_(.*)\.rb$})
21-
watch(%r{^lib/(.*/)?([^/]+)\.rb$}) { |m| "test/#{m[1]}test_#{m[2]}.rb" }
22-
watch(%r{^test/test_helper\.rb$}) { 'test' }
23-
24-
# with Minitest::Spec
25-
# watch(%r{^spec/(.*)_spec\.rb$})
26-
# watch(%r{^lib/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
27-
# watch(%r{^spec/spec_helper\.rb$}) { 'spec' }
28-
29-
# Rails 4
30-
# watch(%r{^app/(.+)\.rb$}) { |m| "test/#{m[1]}_test.rb" }
31-
# watch(%r{^app/controllers/application_controller\.rb$}) { 'test/controllers' }
32-
# watch(%r{^app/controllers/(.+)_controller\.rb$}) { |m| "test/integration/#{m[1]}_test.rb" }
33-
# watch(%r{^app/views/(.+)_mailer/.+}) { |m| "test/mailers/#{m[1]}_mailer_test.rb" }
34-
# watch(%r{^lib/(.+)\.rb$}) { |m| "test/lib/#{m[1]}_test.rb" }
35-
# watch(%r{^test/.+_test\.rb$})
36-
# watch(%r{^test/test_helper\.rb$}) { 'test' }
37-
38-
# Rails < 4
39-
# watch(%r{^app/controllers/(.*)\.rb$}) { |m| "test/functional/#{m[1]}_test.rb" }
40-
# watch(%r{^app/helpers/(.*)\.rb$}) { |m| "test/helpers/#{m[1]}_test.rb" }
41-
# watch(%r{^app/models/(.*)\.rb$}) { |m| "test/unit/#{m[1]}_test.rb" }
3+
watch(%r{^test/(.*)_test\.rb$})
4+
watch(%r{^lib/(.+)\.rb$}) { |m| "test/#{m[1]}_test.rb" }
5+
watch(%r{^test/test_helper\.rb$}) { 'test' }
426
end

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ The Official Ruby SDK for ShipEngine
1616

1717
## Commands
1818

19-
- Run tests: `rake test`
19+
- Run tests once: `rake test`
20+
- Run tests on change: `guard`
2021
- Lint: `rake lint`
2122
- Autoformat: `rake fix`

lib/shipengine.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# frozen_string_literal: true
22

33
require 'shipengine/version'
4-
require 'shipengine/client/platform'
4+
require 'shipengine/client/internal'
55
require 'shipengine/exceptions'

lib/shipengine/client/platform.rb lib/shipengine/client/internal.rb

+15-14
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
require 'faraday_middleware'
44

55
module ShipEngine
6-
class PlatformClient
6+
class InternalClient
77
attr_accessor :connection
88

99
def initialize(api_key:, base_url: 'https://simengine.herokuapp.com/jsonrpc', adapter: Faraday.default_adapter)
@@ -15,18 +15,17 @@ def initialize(api_key:, base_url: 'https://simengine.herokuapp.com/jsonrpc', ad
1515
end
1616
end
1717

18-
def assert_no_platform_errors(response)
19-
# puts response.inspect
20-
body = response.body
21-
error = body['error'] || {}
22-
data = error['data']
23-
return unless data
24-
raise ShipEngine::Exceptions::ShipEngineErrorDetailed.new(body['id'], error['message'], data) unless data.nil?
18+
def assert_shipengine_rpc_success(body)
19+
error, request_id = body.values_at('error', 'request_id')
20+
if error
21+
message, data = error.values_at('message', 'data')
22+
source, type, code = data.values_at('source', 'type', 'code')
23+
raise ShipEngine::Exceptions::ShipEngineErrorDetailed.new(request_id, message, source, type, code)
2524
end
2625
end
2726

2827
# create jsonrpc request has
29-
def create_jsonrpc_request_body(method, params)
28+
def build_jsonrpc_request_body(method, params)
3029
{
3130
jsonrpc: '2.0',
3231
id: '123',
@@ -36,11 +35,13 @@ def create_jsonrpc_request_body(method, params)
3635
end
3736

3837
def make_request(method, params)
39-
response = @connection.send(:post, nil, create_jsonrpc_request_body(method, params))
40-
assert_no_platform_errors(response)
41-
response.body
42-
# throw an error if status code is 400 or above.
43-
# Faraday does not throw errors for 400s -- only 500s!
38+
response = @connection.send(:post, nil, build_jsonrpc_request_body(method, params))
39+
body = response.body
40+
assert_shipengine_rpc_success(body)
41+
42+
body
43+
# throw an error if status code is 400 or above.
44+
# Faraday does not throw errors for 400s -- only 500s!
4445
rescue Faraday::Error => e
4546
raise ShipEngine::Exceptions::ShipEngineError, e.message
4647
end

lib/shipengine/exceptions.rb

+7-4
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,15 @@ def initialize(message_or_messages)
1111

1212
# 400 error, or other "user exceptions"
1313
class ShipEngineErrorDetailed < ShipEngineError
14-
def initialize(request_id, message, data)
14+
attr_reader :request_id, :message, :source, :type, :code
15+
16+
def initialize(request_id, message, source, type, code)
1517
super(message)
1618
@request_id = request_id
17-
@source = data['source']
18-
@type = data['type']
19-
@code = data['code']
19+
@message = message # super with attribute reader seems odd
20+
@source = source
21+
@type = type
22+
@code = code
2023
end
2124
end
2225
end

test/address/validate_address_test.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
require 'test_helper'
22

3-
describe 'validate address' do
3+
describe 'Validate Address' do
44
it 'Should successfully validate an address' do
55
# client = ::ShipEngine::Client.new(api_key: 'abc123')
66
# valid_address = {
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
require 'test_helper'
22
require 'pry'
3-
describe 'Client test' do
3+
describe 'Internal client test' do
44
it 'Should make a request' do
5-
client = ::ShipEngine::PlatformClient.new(api_key: 'abc123')
5+
client = ::ShipEngine::InternalClient.new(api_key: 'abc123')
66
params = { address: {
77
street: ['501 Crawford St'],
88
city_locality: 'Houston',
@@ -15,12 +15,15 @@
1515
end
1616

1717
it 'Should throw Errors' do
18-
client = ::ShipEngine::PlatformClient.new(api_key: 'abc123')
18+
client = ::ShipEngine::InternalClient.new(api_key: 'abc123')
1919
client.make_request('address/validate', { foo: 'invalid request' })
20-
raise 'should not happen'
21-
rescue ShipEngine::Exceptions::ShipEngineError => e
20+
raise 'force fail'
21+
rescue ShipEngine::Exceptions::ShipEngineErrorDetailed => e
22+
assert e.source.is_a?(String)
23+
assert e.type.is_a?(String)
24+
assert e.code.is_a?(String)
2225
assert e.message.is_a?(String)
2326
else
24-
raise 'should not happen'
27+
raise 'force fail'
2528
end
2629
end

test/smoke_test.rb

+2-6
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,8 @@
22

33
require 'test_helper'
44

5-
class ShipEngineTest < Minitest::Test
6-
def test_that_it_has_a_version_number
5+
describe 'Smoke tests' do
6+
it 'Should test a version number' do
77
refute_nil ::ShipEngine::VERSION
88
end
9-
10-
def test_it_does_something_useful
11-
assert true
12-
end
139
end

0 commit comments

Comments
 (0)