friendly_shipping 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.circleci/config.yml +58 -0
- data/.gitignore +18 -0
- data/.rspec +3 -0
- data/.travis.yml +5 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +13 -0
- data/LICENSE.txt +21 -0
- data/README.md +43 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/friendly_shipping.gemspec +33 -0
- data/lib/friendly_shipping/carrier.rb +14 -0
- data/lib/friendly_shipping/label.rb +35 -0
- data/lib/friendly_shipping/services/ship_engine/bad_request.rb +27 -0
- data/lib/friendly_shipping/services/ship_engine/parse_carrier_response.rb +41 -0
- data/lib/friendly_shipping/services/ship_engine/parse_label_response.rb +39 -0
- data/lib/friendly_shipping/services/ship_engine/serialize_label_shipment.rb +75 -0
- data/lib/friendly_shipping/services/ship_engine.rb +78 -0
- data/lib/friendly_shipping/shipping_method.rb +29 -0
- data/lib/friendly_shipping/version.rb +3 -0
- data/lib/friendly_shipping.rb +10 -0
- metadata +164 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: ed3d758e505598739d095bdff683a69d795599f3
|
4
|
+
data.tar.gz: d12267d46eb0c8c788a2bdc47a48853b799602c8
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: c1ee137481e762d5f3dfcef10da897d13e6992575284542cefed9b1c0b3e4d6813fb0fb37ffb004fc7cb2ee8403e0375b973c87588be2f214f0d70b856c324c8
|
7
|
+
data.tar.gz: da22061b59780e8cd0212f9609299aa9328a15f4848eb308aef55482e154ad90bbf5af4342e11c5d64fdddcabf2bc88dafba0f53d0bd31a47ed7fe142485d5cb
|
@@ -0,0 +1,58 @@
|
|
1
|
+
# Ruby CircleCI 2.0 configuration file
|
2
|
+
#
|
3
|
+
# Check https://circleci.com/docs/2.0/language-ruby/ for more details
|
4
|
+
#
|
5
|
+
version: 2
|
6
|
+
jobs:
|
7
|
+
build:
|
8
|
+
docker:
|
9
|
+
# specify the version you desire here
|
10
|
+
- image: circleci/ruby:2.4
|
11
|
+
|
12
|
+
# Specify service dependencies here if necessary
|
13
|
+
# CircleCI maintains a library of pre-built images
|
14
|
+
# documented at https://circleci.com/docs/2.0/circleci-images/
|
15
|
+
# - image: circleci/postgres:9.4
|
16
|
+
|
17
|
+
working_directory: ~/repo
|
18
|
+
|
19
|
+
steps:
|
20
|
+
- checkout
|
21
|
+
|
22
|
+
# Download and cache dependencies
|
23
|
+
- restore_cache:
|
24
|
+
keys:
|
25
|
+
- v1-dependencies-{{ checksum "friendly_shipping.gemspec" }}
|
26
|
+
# fallback to using the latest cache if no exact match is found
|
27
|
+
- v1-dependencies-
|
28
|
+
|
29
|
+
- run:
|
30
|
+
name: install dependencies
|
31
|
+
command: |
|
32
|
+
gem install bundler
|
33
|
+
bundle install --jobs=4 --retry=3 --path vendor/bundle
|
34
|
+
|
35
|
+
- save_cache:
|
36
|
+
paths:
|
37
|
+
- ./vendor/bundle
|
38
|
+
key: v1-dependencies-{{ checksum "Gemfile.lock" }}
|
39
|
+
|
40
|
+
# run tests!
|
41
|
+
- run:
|
42
|
+
name: run tests
|
43
|
+
command: |
|
44
|
+
mkdir /tmp/test-results
|
45
|
+
TEST_FILES="$(circleci tests glob "spec/**/*_spec.rb" | circleci tests split --split-by=timings)"
|
46
|
+
|
47
|
+
bundle exec rspec --format progress \
|
48
|
+
--format RspecJunitFormatter \
|
49
|
+
--out /tmp/test-results/rspec.xml \
|
50
|
+
--format progress \
|
51
|
+
$TEST_FILES
|
52
|
+
|
53
|
+
# collect reports
|
54
|
+
- store_test_results:
|
55
|
+
path: /tmp/test-results
|
56
|
+
- store_artifacts:
|
57
|
+
path: /tmp/test-results
|
58
|
+
destination: test-results
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
10
|
+
orientation.
|
11
|
+
|
12
|
+
## Our Standards
|
13
|
+
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
15
|
+
include:
|
16
|
+
|
17
|
+
* Using welcoming and inclusive language
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
19
|
+
* Gracefully accepting constructive criticism
|
20
|
+
* Focusing on what is best for the community
|
21
|
+
* Showing empathy towards other community members
|
22
|
+
|
23
|
+
Examples of unacceptable behavior by participants include:
|
24
|
+
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
26
|
+
advances
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
28
|
+
* Public or private harassment
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
30
|
+
address, without explicit permission
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
32
|
+
professional setting
|
33
|
+
|
34
|
+
## Our Responsibilities
|
35
|
+
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
38
|
+
response to any instances of unacceptable behavior.
|
39
|
+
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
44
|
+
threatening, offensive, or harmful.
|
45
|
+
|
46
|
+
## Scope
|
47
|
+
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
49
|
+
when an individual is representing the project or its community. Examples of
|
50
|
+
representing a project or community include using an official project e-mail
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
53
|
+
further defined and clarified by project maintainers.
|
54
|
+
|
55
|
+
## Enforcement
|
56
|
+
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
58
|
+
reported by contacting the project team at mamhoff@gmail.com. All
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
63
|
+
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
66
|
+
members of the project's leadership.
|
67
|
+
|
68
|
+
## Attribution
|
69
|
+
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
71
|
+
available at [http://contributor-covenant.org/version/1/4][version]
|
72
|
+
|
73
|
+
[homepage]: http://contributor-covenant.org
|
74
|
+
[version]: http://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
source "https://rubygems.org"
|
2
|
+
|
3
|
+
git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
|
4
|
+
|
5
|
+
# Specify your gem's dependencies in friendly_shipping.gemspec
|
6
|
+
gemspec
|
7
|
+
|
8
|
+
gem 'vcr'
|
9
|
+
gem 'webmock'
|
10
|
+
gem 'pry'
|
11
|
+
gem 'dotenv'
|
12
|
+
gem 'factory_bot'
|
13
|
+
gem 'rspec_junit_formatter'
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2018 Martin Meyerhoff
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
# FriendlyShipping
|
2
|
+
|
3
|
+
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/friendly_shipping`. To experiment with that code, run `bin/console` for an interactive prompt.
|
4
|
+
|
5
|
+
TODO: Delete this and the text above, and describe your gem
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'friendly_shipping'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install friendly_shipping
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
TODO: Write usage instructions here
|
26
|
+
|
27
|
+
## Development
|
28
|
+
|
29
|
+
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.
|
30
|
+
|
31
|
+
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 tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
32
|
+
|
33
|
+
## Contributing
|
34
|
+
|
35
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/friendly_shipping. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
36
|
+
|
37
|
+
## License
|
38
|
+
|
39
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
40
|
+
|
41
|
+
## Code of Conduct
|
42
|
+
|
43
|
+
Everyone interacting in the FriendlyShipping project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/friendly_shipping/blob/master/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "friendly_shipping"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require "friendly_shipping/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "friendly_shipping"
|
8
|
+
spec.version = FriendlyShipping::VERSION
|
9
|
+
spec.authors = ["Martin Meyerhoff"]
|
10
|
+
spec.email = ["mamhoff@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = "An integration layer for shipping services"
|
13
|
+
spec.description = "Allows you to quote or ship a Physical::Shipment object"
|
14
|
+
spec.homepage = "https://github.com/friendly_cart/friendly_shipping"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
18
|
+
f.match(%r{^(test|spec|features)/})
|
19
|
+
end
|
20
|
+
spec.bindir = "exe"
|
21
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
22
|
+
spec.require_paths = ["lib"]
|
23
|
+
|
24
|
+
spec.add_runtime_dependency "physical", "~> 0.2.0"
|
25
|
+
spec.add_runtime_dependency "rest-client", "~> 2.0"
|
26
|
+
spec.add_runtime_dependency "dry-monads", "~> 1.0"
|
27
|
+
spec.add_runtime_dependency "data_uri", "~> 0.0.3"
|
28
|
+
spec.required_ruby_version = '>= 2.4'
|
29
|
+
|
30
|
+
spec.add_development_dependency "bundler"
|
31
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
32
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
33
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
module FriendlyShipping
|
2
|
+
class Carrier
|
3
|
+
attr_reader :id, :name, :code, :shipping_methods, :balance, :data
|
4
|
+
|
5
|
+
def initialize(id: nil, name: nil, code: nil, shipping_methods: [], balance: nil, data: {})
|
6
|
+
@id = id
|
7
|
+
@name = name
|
8
|
+
@code = code
|
9
|
+
@shipping_methods = shipping_methods
|
10
|
+
@balance = balance
|
11
|
+
@data = data
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
module FriendlyShipping
|
2
|
+
class Label
|
3
|
+
attr_reader :id,
|
4
|
+
:shipment_id,
|
5
|
+
:tracking_number,
|
6
|
+
:service_code,
|
7
|
+
:label_href,
|
8
|
+
:data,
|
9
|
+
:label_format,
|
10
|
+
:shipment_cost,
|
11
|
+
:label_data
|
12
|
+
|
13
|
+
def initialize(
|
14
|
+
id: nil,
|
15
|
+
shipment_id: nil,
|
16
|
+
tracking_number: nil,
|
17
|
+
service_code: nil,
|
18
|
+
label_href: nil,
|
19
|
+
label_format: nil,
|
20
|
+
label_data: nil,
|
21
|
+
shipment_cost: nil,
|
22
|
+
data: {}
|
23
|
+
)
|
24
|
+
@id = id
|
25
|
+
@shipment_id = shipment_id
|
26
|
+
@tracking_number = tracking_number
|
27
|
+
@service_code = service_code
|
28
|
+
@label_href = label_href
|
29
|
+
@label_format = label_format
|
30
|
+
@shipment_cost = shipment_cost
|
31
|
+
@label_data = label_data
|
32
|
+
@data = data
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
require 'json'
|
2
|
+
|
3
|
+
module FriendlyShipping
|
4
|
+
module Services
|
5
|
+
class ShipEngine
|
6
|
+
class BadRequest < StandardError
|
7
|
+
attr_reader :rest_error, :response
|
8
|
+
|
9
|
+
def initialize(rest_error)
|
10
|
+
@rest_error = rest_error
|
11
|
+
@response = rest_error.response
|
12
|
+
super parse_json_errors || rest_error
|
13
|
+
end
|
14
|
+
|
15
|
+
private
|
16
|
+
|
17
|
+
def parse_json_errors
|
18
|
+
parsed_body = JSON.parse(response.body)
|
19
|
+
messages = parsed_body.fetch('errors')&.map { |e| e.fetch('message') }
|
20
|
+
messages&.join(', ')
|
21
|
+
rescue JSON::ParserError, KeyError => _error
|
22
|
+
nil
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
require 'json'
|
2
|
+
|
3
|
+
module FriendlyShipping
|
4
|
+
module Services
|
5
|
+
class ShipEngine
|
6
|
+
class ParseCarrierResponse
|
7
|
+
def initialize(response:)
|
8
|
+
@response = response
|
9
|
+
end
|
10
|
+
|
11
|
+
def call
|
12
|
+
parsed_json = JSON.parse(@response.body)
|
13
|
+
parsed_json['carriers'].map do |carrier_data|
|
14
|
+
FriendlyShipping::Carrier.new(
|
15
|
+
id: carrier_data['carrier_id'],
|
16
|
+
name: carrier_data['friendly_name'],
|
17
|
+
code: carrier_data['carrier_code'],
|
18
|
+
shipping_methods: parse_shipping_methods(carrier_data['services']),
|
19
|
+
balance: carrier_data['balance'],
|
20
|
+
data: carrier_data
|
21
|
+
)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
private
|
26
|
+
|
27
|
+
def parse_shipping_methods(shipping_methods_data)
|
28
|
+
shipping_methods_data.map do |shipping_method_data|
|
29
|
+
FriendlyShipping::ShippingMethod.new(
|
30
|
+
name: shipping_method_data["name"],
|
31
|
+
service_code: shipping_method_data["service_code"],
|
32
|
+
domestic: shipping_method_data["domestic"],
|
33
|
+
international: shipping_method_data["international"],
|
34
|
+
multi_package: shipping_method_data["is_multi_package_supported"]
|
35
|
+
)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
require 'json'
|
2
|
+
require 'data_uri'
|
3
|
+
|
4
|
+
module FriendlyShipping
|
5
|
+
module Services
|
6
|
+
class ShipEngine
|
7
|
+
class ParseLabelResponse
|
8
|
+
def initialize(response:)
|
9
|
+
@response = response
|
10
|
+
end
|
11
|
+
|
12
|
+
def call
|
13
|
+
parsed_json = JSON.parse(@response.body)
|
14
|
+
label_uri_string = parsed_json['label_download']['href']
|
15
|
+
label_data = nil
|
16
|
+
label_url = nil
|
17
|
+
if label_uri_string.starts_with?('data')
|
18
|
+
label_data = URI::Data.new(label_uri_string).data
|
19
|
+
else
|
20
|
+
label_url = label_uri_string
|
21
|
+
end
|
22
|
+
[
|
23
|
+
FriendlyShipping::Label.new(
|
24
|
+
id: parsed_json['label_id'],
|
25
|
+
shipment_id: parsed_json['shipment_id'],
|
26
|
+
tracking_number: parsed_json['tracking_number'],
|
27
|
+
service_code: parsed_json['service_code'],
|
28
|
+
label_href: label_url,
|
29
|
+
label_data: label_data,
|
30
|
+
label_format: parsed_json['label_format'].to_sym,
|
31
|
+
shipment_cost: parsed_json['shipment_cost']['amount'],
|
32
|
+
data: parsed_json
|
33
|
+
)
|
34
|
+
]
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
@@ -0,0 +1,75 @@
|
|
1
|
+
module FriendlyShipping
|
2
|
+
module Services
|
3
|
+
class ShipEngine
|
4
|
+
class SerializeLabelShipment
|
5
|
+
attr_reader :shipment
|
6
|
+
|
7
|
+
def initialize(shipment:)
|
8
|
+
@shipment = shipment
|
9
|
+
end
|
10
|
+
|
11
|
+
def call
|
12
|
+
shipment_hash = {
|
13
|
+
label_format: shipment.options[:label_format].presence || "pdf",
|
14
|
+
label_download_type: shipment.options[:label_download_type].presence || "url",
|
15
|
+
shipment: {
|
16
|
+
service_code: shipment.service_code,
|
17
|
+
ship_to: serialize_address(shipment.destination),
|
18
|
+
ship_from: serialize_address(shipment.origin),
|
19
|
+
packages: serialize_packages(shipment.packages)
|
20
|
+
}
|
21
|
+
}
|
22
|
+
if shipment.options[:carrier_id]
|
23
|
+
shipment_hash[:shipment].merge!(carrier_id: shipment.options[:carrier_id])
|
24
|
+
end
|
25
|
+
shipment_hash
|
26
|
+
end
|
27
|
+
|
28
|
+
private
|
29
|
+
|
30
|
+
def serialize_address(address)
|
31
|
+
{
|
32
|
+
name: address.name,
|
33
|
+
phone: address.phone,
|
34
|
+
company_name: address.company_name,
|
35
|
+
address_line1: address.address1,
|
36
|
+
address_line2: address.address2,
|
37
|
+
city_locality: address.city,
|
38
|
+
state_province: address.region.code,
|
39
|
+
postal_code: address.zip,
|
40
|
+
country_code: address.country.code,
|
41
|
+
address_residential_indicator: "No"
|
42
|
+
}
|
43
|
+
end
|
44
|
+
|
45
|
+
def serialize_packages(packages)
|
46
|
+
packages.map do |package|
|
47
|
+
package_hash = {
|
48
|
+
weight: {
|
49
|
+
value: package.weight.convert_to(:ounce).value.to_f,
|
50
|
+
unit: "ounce"
|
51
|
+
}
|
52
|
+
}
|
53
|
+
if package.container.properties[:usps_label_messages]
|
54
|
+
package_hash.merge!(label_messages: package.container.properties[:usps_label_messages])
|
55
|
+
end
|
56
|
+
package_code = package.container.properties[:usps_package_code]
|
57
|
+
if package_code
|
58
|
+
package_hash.merge!(package_code: package_code)
|
59
|
+
else
|
60
|
+
package_hash.merge!(
|
61
|
+
dimensions: {
|
62
|
+
unit: 'inch',
|
63
|
+
width: package.container.width.convert_to(:inches).value.to_f.round(2),
|
64
|
+
length: package.container.length.convert_to(:inches).value.to_f.round(2),
|
65
|
+
height: package.container.height.convert_to(:inches).value.to_f.round(2)
|
66
|
+
}
|
67
|
+
)
|
68
|
+
end
|
69
|
+
package_hash
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
@@ -0,0 +1,78 @@
|
|
1
|
+
require 'dry/monads/result'
|
2
|
+
require 'rest-client'
|
3
|
+
require 'friendly_shipping/services/ship_engine/parse_carrier_response'
|
4
|
+
require 'friendly_shipping/services/ship_engine/serialize_label_shipment'
|
5
|
+
require 'friendly_shipping/services/ship_engine/parse_label_response'
|
6
|
+
require 'friendly_shipping/services/ship_engine/bad_request'
|
7
|
+
|
8
|
+
module FriendlyShipping
|
9
|
+
module Services
|
10
|
+
class ShipEngine
|
11
|
+
include Dry::Monads::Result::Mixin
|
12
|
+
|
13
|
+
API_BASE = "https://api.shipengine.com/v1/"
|
14
|
+
API_PATHS = {
|
15
|
+
carriers: "carriers",
|
16
|
+
labels: "labels"
|
17
|
+
}
|
18
|
+
|
19
|
+
def initialize(token:, test: true)
|
20
|
+
@token = token
|
21
|
+
@test = test
|
22
|
+
end
|
23
|
+
|
24
|
+
def carriers
|
25
|
+
path = API_PATHS[:carriers]
|
26
|
+
get(path).fmap do |response|
|
27
|
+
ParseCarrierResponse.new(response: response).call
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def labels(shipment)
|
32
|
+
payload = SerializeLabelShipment.new(shipment: shipment).call.merge(test_label: test).to_json
|
33
|
+
path = API_PATHS[:labels]
|
34
|
+
post(path, payload).fmap do |response|
|
35
|
+
ParseLabelResponse.new(response: response).call
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
private
|
40
|
+
|
41
|
+
def get(path)
|
42
|
+
Success(
|
43
|
+
RestClient.get(
|
44
|
+
API_BASE + path,
|
45
|
+
request_headers
|
46
|
+
)
|
47
|
+
)
|
48
|
+
rescue RestClient::ExceptionWithResponse => error
|
49
|
+
Failure(error)
|
50
|
+
end
|
51
|
+
|
52
|
+
def post(path, payload)
|
53
|
+
Success(
|
54
|
+
RestClient.post(
|
55
|
+
API_BASE + path,
|
56
|
+
payload,
|
57
|
+
request_headers
|
58
|
+
)
|
59
|
+
)
|
60
|
+
rescue RestClient::ExceptionWithResponse => error
|
61
|
+
if error.to_s == '400 Bad Request'
|
62
|
+
Failure(BadRequest.new(error))
|
63
|
+
else
|
64
|
+
Failure(error)
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
def request_headers
|
69
|
+
{
|
70
|
+
content_type: :json,
|
71
|
+
"api-key": token
|
72
|
+
}
|
73
|
+
end
|
74
|
+
|
75
|
+
attr_reader :token, :test
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
module FriendlyShipping
|
2
|
+
class ShippingMethod
|
3
|
+
attr_reader :name, :service_code
|
4
|
+
|
5
|
+
def initialize(name: nil, service_code: nil, domestic: nil, international: nil, multi_package: nil)
|
6
|
+
@name = name
|
7
|
+
@service_code = service_code
|
8
|
+
@domestic = domestic
|
9
|
+
@international = international
|
10
|
+
@multi_package = multi_package
|
11
|
+
end
|
12
|
+
|
13
|
+
def domestic?
|
14
|
+
!!domestic
|
15
|
+
end
|
16
|
+
|
17
|
+
def international?
|
18
|
+
!!international
|
19
|
+
end
|
20
|
+
|
21
|
+
def multi_package?
|
22
|
+
!!multi_package
|
23
|
+
end
|
24
|
+
|
25
|
+
private
|
26
|
+
|
27
|
+
attr_reader :domestic, :international, :multi_package
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,10 @@
|
|
1
|
+
require "physical"
|
2
|
+
|
3
|
+
require "friendly_shipping/version"
|
4
|
+
require "friendly_shipping/carrier"
|
5
|
+
require "friendly_shipping/services/ship_engine"
|
6
|
+
require "friendly_shipping/shipping_method"
|
7
|
+
require "friendly_shipping/label"
|
8
|
+
|
9
|
+
module FriendlyShipping
|
10
|
+
end
|
metadata
ADDED
@@ -0,0 +1,164 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: friendly_shipping
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.2.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Martin Meyerhoff
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2019-05-21 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: physical
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 0.2.0
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 0.2.0
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rest-client
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '2.0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '2.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: dry-monads
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '1.0'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '1.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: data_uri
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 0.0.3
|
62
|
+
type: :runtime
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: 0.0.3
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: bundler
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: rake
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '10.0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '10.0'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: rspec
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - "~>"
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '3.0'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - "~>"
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '3.0'
|
111
|
+
description: Allows you to quote or ship a Physical::Shipment object
|
112
|
+
email:
|
113
|
+
- mamhoff@gmail.com
|
114
|
+
executables: []
|
115
|
+
extensions: []
|
116
|
+
extra_rdoc_files: []
|
117
|
+
files:
|
118
|
+
- ".circleci/config.yml"
|
119
|
+
- ".gitignore"
|
120
|
+
- ".rspec"
|
121
|
+
- ".travis.yml"
|
122
|
+
- CODE_OF_CONDUCT.md
|
123
|
+
- Gemfile
|
124
|
+
- LICENSE.txt
|
125
|
+
- README.md
|
126
|
+
- Rakefile
|
127
|
+
- bin/console
|
128
|
+
- bin/setup
|
129
|
+
- friendly_shipping.gemspec
|
130
|
+
- lib/friendly_shipping.rb
|
131
|
+
- lib/friendly_shipping/carrier.rb
|
132
|
+
- lib/friendly_shipping/label.rb
|
133
|
+
- lib/friendly_shipping/services/ship_engine.rb
|
134
|
+
- lib/friendly_shipping/services/ship_engine/bad_request.rb
|
135
|
+
- lib/friendly_shipping/services/ship_engine/parse_carrier_response.rb
|
136
|
+
- lib/friendly_shipping/services/ship_engine/parse_label_response.rb
|
137
|
+
- lib/friendly_shipping/services/ship_engine/serialize_label_shipment.rb
|
138
|
+
- lib/friendly_shipping/shipping_method.rb
|
139
|
+
- lib/friendly_shipping/version.rb
|
140
|
+
homepage: https://github.com/friendly_cart/friendly_shipping
|
141
|
+
licenses:
|
142
|
+
- MIT
|
143
|
+
metadata: {}
|
144
|
+
post_install_message:
|
145
|
+
rdoc_options: []
|
146
|
+
require_paths:
|
147
|
+
- lib
|
148
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
149
|
+
requirements:
|
150
|
+
- - ">="
|
151
|
+
- !ruby/object:Gem::Version
|
152
|
+
version: '2.4'
|
153
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
154
|
+
requirements:
|
155
|
+
- - ">="
|
156
|
+
- !ruby/object:Gem::Version
|
157
|
+
version: '0'
|
158
|
+
requirements: []
|
159
|
+
rubyforge_project:
|
160
|
+
rubygems_version: 2.6.14.1
|
161
|
+
signing_key:
|
162
|
+
specification_version: 4
|
163
|
+
summary: An integration layer for shipping services
|
164
|
+
test_files: []
|