sendle_api 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +15 -0
- data/.rspec +3 -0
- data/.travis.yml +6 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +7 -0
- data/LICENSE.txt +21 -0
- data/README.md +44 -0
- data/Rakefile +6 -0
- data/bin/console +18 -0
- data/bin/setup +8 -0
- data/lib/dev/config.rb +9 -0
- data/lib/dev/zeitwerk_loader.rb +21 -0
- data/lib/sendle_api.rb +22 -0
- data/lib/sendle_api/configuration.rb +24 -0
- data/lib/sendle_api/connection.rb +18 -0
- data/lib/sendle_api/errors.rb +71 -0
- data/lib/sendle_api/not_saveable.rb +23 -0
- data/lib/sendle_api/resources/address.rb +15 -0
- data/lib/sendle_api/resources/base.rb +68 -0
- data/lib/sendle_api/resources/contact.rb +15 -0
- data/lib/sendle_api/resources/order.rb +54 -0
- data/lib/sendle_api/resources/receiver.rb +18 -0
- data/lib/sendle_api/resources/sender.rb +30 -0
- data/lib/sendle_api/resources/tracking.rb +6 -0
- data/lib/sendle_api/resources/volume.rb +11 -0
- data/lib/sendle_api/resources/weight.rb +14 -0
- data/lib/sendle_api/version.rb +3 -0
- data/sendle_api.gemspec +38 -0
- metadata +187 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 386a566c5290868a2c87a9fa784d326a4414ff460e94cb72df849a90f2b797a3
|
4
|
+
data.tar.gz: add9b3a6795ba309494b21d0a634870d49a5d9107854b3eacc31b5ed332cef15
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 5dbb20a21b4a87bf642f70d930967a5e7bd8d1194921519b8dc8682659506ab300a54d5cb36dbec02cc35b188c580282c3fe796c0d9b76462712c799b0a32a9c
|
7
|
+
data.tar.gz: 1af00478d6980c7ba3d06f24d53b9fc50ab274e6658daf2a5ccd350cc81e106a074f0133fb3e20bdaee681edd7cc7b3d02ac989ff083fb4f2dd3104595dc6b4b
|
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 andy_gg1996@hotmail.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 [https://contributor-covenant.org/version/1/4][version]
|
72
|
+
|
73
|
+
[homepage]: https://contributor-covenant.org
|
74
|
+
[version]: https://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2020 Andy Chong
|
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,44 @@
|
|
1
|
+
# SendleApi
|
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/sendle_api`. 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 'sendle_api'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle install
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install sendle_api
|
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]/sendle_api. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/sendle_api/blob/master/CODE_OF_CONDUCT.md).
|
36
|
+
|
37
|
+
|
38
|
+
## License
|
39
|
+
|
40
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
41
|
+
|
42
|
+
## Code of Conduct
|
43
|
+
|
44
|
+
Everyone interacting in the SendleApi project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/sendle_api/blob/master/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
require "bundler/setup"
|
3
|
+
require "dotenv/load"
|
4
|
+
require "dev/zeitwerk_loader"
|
5
|
+
require "dev/config"
|
6
|
+
require "sendle_api"
|
7
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
8
|
+
# with your gem easier. You can also use a different console, if you like.
|
9
|
+
|
10
|
+
set_config
|
11
|
+
|
12
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
13
|
+
require "pry"
|
14
|
+
require "pry-byebug"
|
15
|
+
Pry.start
|
16
|
+
|
17
|
+
# require "irb"
|
18
|
+
# IRB.start(__FILE__)
|
data/bin/setup
ADDED
data/lib/dev/config.rb
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
require "zeitwerk"
|
2
|
+
require_relative "config"
|
3
|
+
|
4
|
+
loader = Zeitwerk::Loader.for_gem
|
5
|
+
loader.inflector.inflect(
|
6
|
+
"sendle_api" => "SendleAPI"
|
7
|
+
)
|
8
|
+
loader.push_dir("./lib")
|
9
|
+
loader.collapse("./lib/sendle_api/resources")
|
10
|
+
loader.ignore("#{__dir__}/config.rb")
|
11
|
+
loader.enable_reloading
|
12
|
+
# loader.log!
|
13
|
+
loader.setup
|
14
|
+
|
15
|
+
$__sendle_api_loader__ = loader
|
16
|
+
|
17
|
+
def reload!
|
18
|
+
$__sendle_api_loader__.reload
|
19
|
+
set_config
|
20
|
+
true
|
21
|
+
end
|
data/lib/sendle_api.rb
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
require "sendle_api/version"
|
2
|
+
require "active_resource"
|
3
|
+
|
4
|
+
module SendleAPI
|
5
|
+
require "phonelib"
|
6
|
+
require "countries"
|
7
|
+
|
8
|
+
require "sendle_api/configuration"
|
9
|
+
require "sendle_api/not_saveable"
|
10
|
+
require "sendle_api/errors"
|
11
|
+
require "sendle_api/connection"
|
12
|
+
|
13
|
+
require "sendle_api/resources/base"
|
14
|
+
require "sendle_api/resources/tracking"
|
15
|
+
require "sendle_api/resources/contact"
|
16
|
+
require "sendle_api/resources/address"
|
17
|
+
require "sendle_api/resources/sender"
|
18
|
+
require "sendle_api/resources/receiver"
|
19
|
+
require "sendle_api/resources/volume"
|
20
|
+
require "sendle_api/resources/weight"
|
21
|
+
require "sendle_api/resources/order"
|
22
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
module SendleAPI
|
2
|
+
class Configuration
|
3
|
+
attr_accessor :sendle_id, :api_key, :testing
|
4
|
+
alias_method :testing?, :testing
|
5
|
+
|
6
|
+
def initialize
|
7
|
+
# default
|
8
|
+
@testing = false
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.config
|
13
|
+
@config ||= Configuration.new
|
14
|
+
end
|
15
|
+
|
16
|
+
def self.config=(config)
|
17
|
+
@config = config
|
18
|
+
end
|
19
|
+
|
20
|
+
def self.configure
|
21
|
+
yield config
|
22
|
+
SendleAPI::Base.set_site
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
module SendleAPI
|
2
|
+
class Connection < ActiveResource::Connection
|
3
|
+
# def request(method, path, *arguments)
|
4
|
+
# result = ActiveSupport::Notifications.instrument("request.active_resource") do |payload|
|
5
|
+
# payload[:method] = method
|
6
|
+
# payload[:request_uri] = "#{site.scheme}://#{site.host}:#{site.port}#{path}"
|
7
|
+
# payload[:result] = http.send(method, path, *arguments)
|
8
|
+
# require 'pry-byebug'; binding.pry
|
9
|
+
# payload[:result]
|
10
|
+
# end
|
11
|
+
# handle_response(result)
|
12
|
+
# rescue Timeout::Error => e
|
13
|
+
# raise TimeoutError.new(e.message)
|
14
|
+
# rescue OpenSSL::SSL::SSLError => e
|
15
|
+
# raise SSLError.new(e.message)
|
16
|
+
# end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,71 @@
|
|
1
|
+
|
2
|
+
require "active_support/core_ext/array/wrap"
|
3
|
+
require "active_support/core_ext/object/blank"
|
4
|
+
|
5
|
+
module SendleAPI
|
6
|
+
class Errors < ActiveModel::Errors
|
7
|
+
# Grabs errors from an array of messages (like ActiveRecord::Validations).
|
8
|
+
# The second parameter directs the errors cache to be cleared (default)
|
9
|
+
# or not (by passing true).
|
10
|
+
def from_array(messages, save_cache = false)
|
11
|
+
clear unless save_cache
|
12
|
+
humanized_attributes = Hash[@base.known_attributes.map { |attr_name| [attr_name.humanize, attr_name] }]
|
13
|
+
messages.each do |message|
|
14
|
+
attr_message = humanized_attributes.keys.sort_by { |a| -a.length }.detect do |attr_name|
|
15
|
+
if message[0, attr_name.size + 1] == "#{attr_name} "
|
16
|
+
add humanized_attributes[attr_name], message[(attr_name.size + 1)..-1]
|
17
|
+
end
|
18
|
+
end
|
19
|
+
self[:base] << message if attr_message.nil?
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
# Grabs errors from a hash of attribute => array of errors elements
|
24
|
+
# The second parameter directs the errors cache to be cleared (default)
|
25
|
+
# or not (by passing true)
|
26
|
+
#
|
27
|
+
# Unrecognized attribute names will be humanized and added to the record's
|
28
|
+
# base errors.
|
29
|
+
def from_hash(messages, save_cache = false)
|
30
|
+
clear unless save_cache
|
31
|
+
|
32
|
+
# delete top level not-needed keys
|
33
|
+
messages.delete("error")
|
34
|
+
messages.delete("error_description")
|
35
|
+
|
36
|
+
messages.each do |(key, errors)|
|
37
|
+
errors.each do |error|
|
38
|
+
if @base.known_attributes.include?(key)
|
39
|
+
add key, error
|
40
|
+
elsif key == "base"
|
41
|
+
self[:base] << error
|
42
|
+
else
|
43
|
+
# reporting an error on an attribute not in attributes
|
44
|
+
# format and add them to base
|
45
|
+
self[:base] << "#{key.humanize} #{error}"
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
# Grabs errors from a json response.
|
52
|
+
def from_json(json, save_cache = false)
|
53
|
+
decoded = ActiveSupport::JSON.decode(json) || {} rescue {}
|
54
|
+
if decoded.kind_of?(Hash) && (decoded.has_key?("errors") || decoded.empty?)
|
55
|
+
errors = decoded["errors"] || {}
|
56
|
+
if errors.kind_of?(Array)
|
57
|
+
# 3.2.1-style with array of strings
|
58
|
+
ActiveSupport::Deprecation.warn("Returning errors as an array of strings is deprecated.")
|
59
|
+
from_array errors, save_cache
|
60
|
+
else
|
61
|
+
# 3.2.2+ style
|
62
|
+
from_hash errors, save_cache
|
63
|
+
end
|
64
|
+
else
|
65
|
+
# <3.2-style respond_with - lacks 'errors' key
|
66
|
+
ActiveSupport::Deprecation.warn('Returning errors as a hash without a root "errors" key is deprecated.')
|
67
|
+
from_hash decoded, save_cache
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
module SendleAPI
|
2
|
+
module NotSaveable
|
3
|
+
def self.create(**args)
|
4
|
+
raise NotImplementedError, "This class is a non-writable resource."
|
5
|
+
end
|
6
|
+
|
7
|
+
def save
|
8
|
+
raise NotImplementedError, "This object is a non-writable resource."
|
9
|
+
end
|
10
|
+
|
11
|
+
def update
|
12
|
+
save
|
13
|
+
end
|
14
|
+
|
15
|
+
def update_attribute(**args)
|
16
|
+
save
|
17
|
+
end
|
18
|
+
|
19
|
+
def update_attributes(**args)
|
20
|
+
save
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
module SendleAPI
|
2
|
+
class Address < Base
|
3
|
+
include NotSaveable
|
4
|
+
|
5
|
+
DEFAULT_ATTRS = {
|
6
|
+
address_line1: nil,
|
7
|
+
suburb: nil,
|
8
|
+
state_name: nil,
|
9
|
+
postcode: nil,
|
10
|
+
country: nil
|
11
|
+
}
|
12
|
+
|
13
|
+
validates :address_line1, :suburb, :state_name, :postcode, :country, presence: true
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,68 @@
|
|
1
|
+
module SendleAPI
|
2
|
+
class Base < ::ActiveResource::Base
|
3
|
+
|
4
|
+
validate :child_object_validations
|
5
|
+
|
6
|
+
self.include_root_in_json = false
|
7
|
+
self.include_format_in_path = false
|
8
|
+
self.connection_class = Connection
|
9
|
+
self.prefix = '/api/'
|
10
|
+
|
11
|
+
def initialize(attributes = {}, persisted = false)
|
12
|
+
if defined?(self.class::DEFAULT_ATTRS)
|
13
|
+
attributes = self.class::DEFAULT_ATTRS.merge(attributes)
|
14
|
+
end
|
15
|
+
super
|
16
|
+
end
|
17
|
+
|
18
|
+
def save
|
19
|
+
self.class.validate_configs
|
20
|
+
super
|
21
|
+
end
|
22
|
+
|
23
|
+
def errors
|
24
|
+
@errors ||= Errors.new(self)
|
25
|
+
end
|
26
|
+
|
27
|
+
private
|
28
|
+
|
29
|
+
def child_object_validations
|
30
|
+
if defined?(self.class::CHILD_OBJECT_KEYS_FOR_VALIDATION)
|
31
|
+
self.class::CHILD_OBJECT_KEYS_FOR_VALIDATION.each do |obj|
|
32
|
+
if !attributes[obj].nil? && !send(obj).valid?
|
33
|
+
send(obj).errors.messages.each do |msg_key, messages|
|
34
|
+
messages.each do |message|
|
35
|
+
errors.add("#{obj}_#{msg_key}".to_sym, message)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
class << self
|
44
|
+
|
45
|
+
def set_site
|
46
|
+
self.site = if SendleAPI.config.testing?
|
47
|
+
"https://#{basic_auth_details}@sandbox.sendle.com"
|
48
|
+
else
|
49
|
+
"https://#{basic_auth_details}@api.sendle.com"
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
def basic_auth_details
|
54
|
+
"#{SendleAPI.config.sendle_id}:#{SendleAPI.config.api_key}"
|
55
|
+
end
|
56
|
+
|
57
|
+
def validate_configs
|
58
|
+
unless SendleAPI.config.sendle_id
|
59
|
+
raise ArgumentError, "SendleAPI sendle_id is missing, please set it in the config and restart."
|
60
|
+
end
|
61
|
+
|
62
|
+
unless SendleAPI.config.api_key
|
63
|
+
raise ArgumentError, "SendleAPI api_key is missing, please set it in the config and restart."
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
@@ -0,0 +1,54 @@
|
|
1
|
+
require 'digest/sha1'
|
2
|
+
|
3
|
+
module SendleAPI
|
4
|
+
class Order < Base
|
5
|
+
has_one :weight, class_name: Weight
|
6
|
+
has_one :volume, class_name: Volume
|
7
|
+
has_one :sender, class_name: Sender
|
8
|
+
has_one :receiver, class_name: Receiver
|
9
|
+
|
10
|
+
validates :description, :weight, :sender, :receiver, presence: true
|
11
|
+
validates :first_mile_option, inclusion: { in: ["pickup", "drop off"]}
|
12
|
+
|
13
|
+
DEFAULT_ATTRS = {
|
14
|
+
pickup_date: nil,
|
15
|
+
first_mile_option: nil,
|
16
|
+
description: nil,
|
17
|
+
customer_reference: nil,
|
18
|
+
metadata: {},
|
19
|
+
contents: {},
|
20
|
+
weight: Weight.new,
|
21
|
+
volume: Volume.new,
|
22
|
+
sender: Sender.new,
|
23
|
+
receiver: Receiver.new
|
24
|
+
}
|
25
|
+
|
26
|
+
CHILD_OBJECT_KEYS_FOR_VALIDATION = [:weight, :volume, :sender, :receiver]
|
27
|
+
|
28
|
+
def save
|
29
|
+
set_idempotency_key_header
|
30
|
+
result = super
|
31
|
+
set_order_id_as_id
|
32
|
+
result
|
33
|
+
end
|
34
|
+
|
35
|
+
def track
|
36
|
+
if attributes["sendle_reference"]
|
37
|
+
attributes[:tracking] = Tracking.find(sendle_reference)
|
38
|
+
else
|
39
|
+
raise ArgumentError, "sendle_reference not found in attributes"
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
private
|
44
|
+
|
45
|
+
def set_order_id_as_id
|
46
|
+
attributes[:id] = attributes[:order_id]
|
47
|
+
end
|
48
|
+
|
49
|
+
def set_idempotency_key_header
|
50
|
+
self.class.headers.merge!("Idempotency-Key": Digest::SHA1.hexdigest(self.encode))
|
51
|
+
end
|
52
|
+
|
53
|
+
end
|
54
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
module SendleAPI
|
2
|
+
class Receiver < Base
|
3
|
+
include NotSaveable
|
4
|
+
|
5
|
+
has_one :contact, class_name: Contact
|
6
|
+
has_one :address, class_name: Address
|
7
|
+
|
8
|
+
validates :contact, :address, :instructions, presence: true
|
9
|
+
|
10
|
+
DEFAULT_ATTRS = {
|
11
|
+
contact: Contact.new,
|
12
|
+
address: Address.new,
|
13
|
+
instructions: ""
|
14
|
+
}
|
15
|
+
|
16
|
+
CHILD_OBJECT_KEYS_FOR_VALIDATION = [:contact, :address]
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
module SendleAPI
|
2
|
+
class Sender < Base
|
3
|
+
include NotSaveable
|
4
|
+
|
5
|
+
has_one :contact, class_name: Contact
|
6
|
+
has_one :address, class_name: Address
|
7
|
+
|
8
|
+
validates :contact, :address, presence: true
|
9
|
+
validate :address_country_valid
|
10
|
+
|
11
|
+
DEFAULT_ATTRS = {
|
12
|
+
contact: Contact.new,
|
13
|
+
address: Address.new,
|
14
|
+
instructions: ""
|
15
|
+
}
|
16
|
+
|
17
|
+
CHILD_OBJECT_KEYS_FOR_VALIDATION = [:contact, :address]
|
18
|
+
|
19
|
+
private
|
20
|
+
|
21
|
+
def address_country_valid
|
22
|
+
return unless address
|
23
|
+
|
24
|
+
valid_sender_countries = ['AU', 'US', 'Australia', 'United States']
|
25
|
+
unless valid_sender_countries.include? address.country
|
26
|
+
errors.add(:sender_address_country, "Must be from #{valid_sender_countries}")
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
data/sendle_api.gemspec
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
require_relative "lib/sendle_api/version"
|
2
|
+
|
3
|
+
Gem::Specification.new do |spec|
|
4
|
+
spec.name = "sendle_api"
|
5
|
+
spec.version = SendleAPI::VERSION
|
6
|
+
spec.authors = ["Andy Chong"]
|
7
|
+
spec.email = ["andygg1996personal@gmail.com"]
|
8
|
+
|
9
|
+
spec.summary = "Ruby object based Sendle API wrapper."
|
10
|
+
spec.homepage = "https://github.com/PostCo/sendle_api"
|
11
|
+
spec.license = "MIT"
|
12
|
+
spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
|
13
|
+
|
14
|
+
spec.metadata["allowed_push_host"] = "https://rubygems.org/"
|
15
|
+
|
16
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
17
|
+
spec.metadata["source_code_uri"] = "https://github.com/PostCo/sendle_api"
|
18
|
+
spec.metadata["changelog_uri"] = "https://github.com/PostCo/sendle_api/releases"
|
19
|
+
|
20
|
+
# Specify which files should be added to the gem when it is released.
|
21
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
22
|
+
spec.files = Dir.chdir(File.expand_path("..", __FILE__)) do
|
23
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
24
|
+
end
|
25
|
+
spec.bindir = "exe"
|
26
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
27
|
+
spec.require_paths = ["lib"]
|
28
|
+
|
29
|
+
spec.add_dependency "activeresource", "~> 5.0"
|
30
|
+
spec.add_dependency "countries", "~> 3.0"
|
31
|
+
spec.add_dependency "phonelib", "~> 0.6"
|
32
|
+
|
33
|
+
spec.add_development_dependency "rspec", "~> 3.2"
|
34
|
+
spec.add_development_dependency "dotenv"
|
35
|
+
spec.add_development_dependency "pry-byebug"
|
36
|
+
spec.add_development_dependency "pry"
|
37
|
+
spec.add_development_dependency "guard-rspec"
|
38
|
+
end
|
metadata
ADDED
@@ -0,0 +1,187 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: sendle_api
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Andy Chong
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2020-06-24 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activeresource
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '5.0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '5.0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: countries
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '3.0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '3.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: phonelib
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0.6'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0.6'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '3.2'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '3.2'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: dotenv
|
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: pry-byebug
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: pry
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - ">="
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: guard-rspec
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - ">="
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0'
|
118
|
+
type: :development
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - ">="
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '0'
|
125
|
+
description:
|
126
|
+
email:
|
127
|
+
- andygg1996personal@gmail.com
|
128
|
+
executables: []
|
129
|
+
extensions: []
|
130
|
+
extra_rdoc_files: []
|
131
|
+
files:
|
132
|
+
- ".gitignore"
|
133
|
+
- ".rspec"
|
134
|
+
- ".travis.yml"
|
135
|
+
- CODE_OF_CONDUCT.md
|
136
|
+
- Gemfile
|
137
|
+
- LICENSE.txt
|
138
|
+
- README.md
|
139
|
+
- Rakefile
|
140
|
+
- bin/console
|
141
|
+
- bin/setup
|
142
|
+
- lib/dev/config.rb
|
143
|
+
- lib/dev/zeitwerk_loader.rb
|
144
|
+
- lib/sendle_api.rb
|
145
|
+
- lib/sendle_api/configuration.rb
|
146
|
+
- lib/sendle_api/connection.rb
|
147
|
+
- lib/sendle_api/errors.rb
|
148
|
+
- lib/sendle_api/not_saveable.rb
|
149
|
+
- lib/sendle_api/resources/address.rb
|
150
|
+
- lib/sendle_api/resources/base.rb
|
151
|
+
- lib/sendle_api/resources/contact.rb
|
152
|
+
- lib/sendle_api/resources/order.rb
|
153
|
+
- lib/sendle_api/resources/receiver.rb
|
154
|
+
- lib/sendle_api/resources/sender.rb
|
155
|
+
- lib/sendle_api/resources/tracking.rb
|
156
|
+
- lib/sendle_api/resources/volume.rb
|
157
|
+
- lib/sendle_api/resources/weight.rb
|
158
|
+
- lib/sendle_api/version.rb
|
159
|
+
- sendle_api.gemspec
|
160
|
+
homepage: https://github.com/PostCo/sendle_api
|
161
|
+
licenses:
|
162
|
+
- MIT
|
163
|
+
metadata:
|
164
|
+
allowed_push_host: https://rubygems.org/
|
165
|
+
homepage_uri: https://github.com/PostCo/sendle_api
|
166
|
+
source_code_uri: https://github.com/PostCo/sendle_api
|
167
|
+
changelog_uri: https://github.com/PostCo/sendle_api/releases
|
168
|
+
post_install_message:
|
169
|
+
rdoc_options: []
|
170
|
+
require_paths:
|
171
|
+
- lib
|
172
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
173
|
+
requirements:
|
174
|
+
- - ">="
|
175
|
+
- !ruby/object:Gem::Version
|
176
|
+
version: 2.3.0
|
177
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
178
|
+
requirements:
|
179
|
+
- - ">="
|
180
|
+
- !ruby/object:Gem::Version
|
181
|
+
version: '0'
|
182
|
+
requirements: []
|
183
|
+
rubygems_version: 3.0.3
|
184
|
+
signing_key:
|
185
|
+
specification_version: 4
|
186
|
+
summary: Ruby object based Sendle API wrapper.
|
187
|
+
test_files: []
|