janio_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 +13 -0
- data/.rspec +3 -0
- data/.travis.yml +6 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +6 -0
- data/Gemfile.lock +73 -0
- data/LICENSE.txt +21 -0
- data/README.md +47 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/janio_api.gemspec +35 -0
- data/lib/janio_api/configuration.rb +5 -0
- data/lib/janio_api/connection.rb +22 -0
- data/lib/janio_api/redirect_fetcher.rb +46 -0
- data/lib/janio_api/resources/base.rb +13 -0
- data/lib/janio_api/resources/order.rb +139 -0
- data/lib/janio_api/version.rb +3 -0
- data/lib/janio_api.rb +22 -0
- data/lib/zeitwerk_loader.rb +14 -0
- metadata +137 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 64be649aa911d63a575983fb264d6eccb1110181730087bf4c597c356d380f3d
|
4
|
+
data.tar.gz: d6e5ac35aa954e8e9f2f01dcad588aa0e03ee97e984347377771cba7b0bb89c0
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: d1f027f9b7a248d1126cf8a1bb3e0ad8cab9db0b1b51b28eca0bc0c802bc500b0aef1a6e6a64f9de94a8e0efcb2cd4082f4e7d589a42d169fd72ecb7a5de1c74
|
7
|
+
data.tar.gz: 68796a18371a5d99ec452b2d8422dd24bda865d523aac4a0417cde2344341120f8173bfcc33aeb1b19c96f0a7889ce480c220f366cd66803f8d72efa4909c720
|
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/Gemfile.lock
ADDED
@@ -0,0 +1,73 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
janio_api (0.1.0)
|
5
|
+
activeresource
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: https://rubygems.org/
|
9
|
+
specs:
|
10
|
+
activemodel (6.0.2.2)
|
11
|
+
activesupport (= 6.0.2.2)
|
12
|
+
activemodel-serializers-xml (1.0.2)
|
13
|
+
activemodel (> 5.x)
|
14
|
+
activesupport (> 5.x)
|
15
|
+
builder (~> 3.1)
|
16
|
+
activeresource (5.1.0)
|
17
|
+
activemodel (>= 5.0, < 7)
|
18
|
+
activemodel-serializers-xml (~> 1.0)
|
19
|
+
activesupport (>= 5.0, < 7)
|
20
|
+
activesupport (6.0.2.2)
|
21
|
+
concurrent-ruby (~> 1.0, >= 1.0.2)
|
22
|
+
i18n (>= 0.7, < 2)
|
23
|
+
minitest (~> 5.1)
|
24
|
+
tzinfo (~> 1.1)
|
25
|
+
zeitwerk (~> 2.2)
|
26
|
+
builder (3.2.4)
|
27
|
+
byebug (11.1.1)
|
28
|
+
coderay (1.1.2)
|
29
|
+
concurrent-ruby (1.1.6)
|
30
|
+
diff-lcs (1.3)
|
31
|
+
dotenv (2.7.5)
|
32
|
+
i18n (1.8.2)
|
33
|
+
concurrent-ruby (~> 1.0)
|
34
|
+
method_source (1.0.0)
|
35
|
+
minitest (5.14.0)
|
36
|
+
pry (0.13.1)
|
37
|
+
coderay (~> 1.1)
|
38
|
+
method_source (~> 1.0)
|
39
|
+
pry-byebug (3.9.0)
|
40
|
+
byebug (~> 11.0)
|
41
|
+
pry (~> 0.13.0)
|
42
|
+
rake (12.3.3)
|
43
|
+
rspec (3.9.0)
|
44
|
+
rspec-core (~> 3.9.0)
|
45
|
+
rspec-expectations (~> 3.9.0)
|
46
|
+
rspec-mocks (~> 3.9.0)
|
47
|
+
rspec-core (3.9.1)
|
48
|
+
rspec-support (~> 3.9.1)
|
49
|
+
rspec-expectations (3.9.1)
|
50
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
51
|
+
rspec-support (~> 3.9.0)
|
52
|
+
rspec-mocks (3.9.1)
|
53
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
54
|
+
rspec-support (~> 3.9.0)
|
55
|
+
rspec-support (3.9.2)
|
56
|
+
thread_safe (0.3.6)
|
57
|
+
tzinfo (1.2.7)
|
58
|
+
thread_safe (~> 0.1)
|
59
|
+
zeitwerk (2.3.0)
|
60
|
+
|
61
|
+
PLATFORMS
|
62
|
+
ruby
|
63
|
+
|
64
|
+
DEPENDENCIES
|
65
|
+
dotenv
|
66
|
+
janio_api!
|
67
|
+
pry
|
68
|
+
pry-byebug
|
69
|
+
rake (~> 12.3)
|
70
|
+
rspec (~> 3.2)
|
71
|
+
|
72
|
+
BUNDLED WITH
|
73
|
+
2.1.4
|
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,47 @@
|
|
1
|
+
# JanioAPI
|
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/janio_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 'janio_api'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle install
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install janio_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/PostCo/janio_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]/janio_api/blob/master/CODE_OF_CONDUCT.md).
|
36
|
+
|
37
|
+
### Gem Building Guide
|
38
|
+
|
39
|
+
Please read this [guide](https://bundler.io/guides/creating_gem.html), it is very helpful for beginner gem builder.
|
40
|
+
|
41
|
+
## License
|
42
|
+
|
43
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
44
|
+
|
45
|
+
## Code of Conduct
|
46
|
+
|
47
|
+
Everyone interacting in the JanioAPI project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/janio_api/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 "janio_api"
|
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
data/janio_api.gemspec
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
require_relative "lib/janio_api/version"
|
2
|
+
|
3
|
+
Gem::Specification.new do |spec|
|
4
|
+
spec.name = "janio_api"
|
5
|
+
spec.version = JanioAPI::VERSION
|
6
|
+
spec.authors = ["Andy Chong"]
|
7
|
+
spec.email = ["andygg1996personal@gmail.com"]
|
8
|
+
|
9
|
+
spec.summary = "Ruby object based Janio API wrapper."
|
10
|
+
spec.homepage = "https://github.com/PostCo/janio_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/janio_api"
|
18
|
+
spec.metadata["changelog_uri"] = "https://github.com/PostCo/janio_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"
|
30
|
+
|
31
|
+
spec.add_development_dependency "rspec", "~> 3.2"
|
32
|
+
spec.add_development_dependency "dotenv"
|
33
|
+
spec.add_development_dependency "pry-byebug"
|
34
|
+
spec.add_development_dependency "pry"
|
35
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
require "fetcher"
|
2
|
+
module JanioAPI
|
3
|
+
class Connection < ActiveResource::Connection
|
4
|
+
def request(method, path, *arguments)
|
5
|
+
result = ActiveSupport::Notifications.instrument("request.active_resource") { |payload|
|
6
|
+
payload[:method] = method
|
7
|
+
payload[:request_uri] = "#{site.scheme}://#{site.host}:#{site.port}#{path}"
|
8
|
+
|
9
|
+
payload[:result] = if method == :get
|
10
|
+
RedirectFetcher.get("#{site}#{path}")
|
11
|
+
else
|
12
|
+
http.send(method, path, *arguments)
|
13
|
+
end
|
14
|
+
}
|
15
|
+
handle_response(result)
|
16
|
+
rescue Timeout::Error => e
|
17
|
+
raise TimeoutError.new(e.message)
|
18
|
+
rescue OpenSSL::SSL::SSLError => e
|
19
|
+
raise SSLError.new(e.message)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
module JanioAPI
|
2
|
+
class RedirectFetcher
|
3
|
+
def self.get(src)
|
4
|
+
uri = URI.parse(src)
|
5
|
+
|
6
|
+
proxy = OpenStruct.new
|
7
|
+
|
8
|
+
http_proxy = Net::HTTP::Proxy(proxy.host, proxy.port, proxy.user, proxy.password)
|
9
|
+
|
10
|
+
redirect_limit = 6
|
11
|
+
response = nil
|
12
|
+
|
13
|
+
loop do
|
14
|
+
raise ArgumentError, "HTTP redirect too deep" if redirect_limit == 0
|
15
|
+
redirect_limit -= 1
|
16
|
+
|
17
|
+
http = http_proxy.new(uri.host, uri.port)
|
18
|
+
|
19
|
+
request = Net::HTTP::Get.new(uri.request_uri, {})
|
20
|
+
if uri.instance_of? URI::HTTPS
|
21
|
+
http.use_ssl = true
|
22
|
+
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
23
|
+
end
|
24
|
+
|
25
|
+
response = http.request(request)
|
26
|
+
|
27
|
+
if response.code == "200"
|
28
|
+
break
|
29
|
+
elsif response.code == "304"
|
30
|
+
break
|
31
|
+
elsif response.code == "301" || response.code == "302" || response.code == "303" || response.code == "307"
|
32
|
+
|
33
|
+
newuri = URI.parse(response.header["location"])
|
34
|
+
if newuri.relative?
|
35
|
+
newuri = uri + response.header["location"]
|
36
|
+
end
|
37
|
+
uri = newuri
|
38
|
+
else
|
39
|
+
break
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
response
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
module JanioAPI
|
2
|
+
class Base < ::ActiveResource::Base
|
3
|
+
self.include_root_in_json = false
|
4
|
+
self.include_format_in_path = false
|
5
|
+
self.connection_class = JanioAPI::Connection
|
6
|
+
|
7
|
+
if JanioAPI.config&.api_host
|
8
|
+
self.site = JanioAPI.config.api_host
|
9
|
+
else
|
10
|
+
raise ArgumentError, "Please set the api_host in the initializer file"
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,139 @@
|
|
1
|
+
module JanioAPI
|
2
|
+
class Order < Base
|
3
|
+
self.prefix = "/api/order/orders/"
|
4
|
+
self.element_name = ""
|
5
|
+
|
6
|
+
validate :check_api_token
|
7
|
+
|
8
|
+
def check_api_token
|
9
|
+
errors.add(:base, "Please set the api_token in the initializer file") if JanioAPI.config.api_token.blank?
|
10
|
+
end
|
11
|
+
|
12
|
+
class Collection < ActiveResource::Collection
|
13
|
+
attr_accessor :count, :next, :previous
|
14
|
+
|
15
|
+
def initialize(parsed = {})
|
16
|
+
@count = parsed["count"]
|
17
|
+
@next = parsed["next"]
|
18
|
+
@previous = parsed["previous"]
|
19
|
+
@elements = parsed["results"]
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
self.collection_parser = Collection
|
24
|
+
|
25
|
+
class << self
|
26
|
+
def tracking_path
|
27
|
+
"/api/tracker/query-by-tracking-nos/"
|
28
|
+
end
|
29
|
+
|
30
|
+
# override find to customize url, and only allow find_every
|
31
|
+
# use to find the parcel info, include the label pdf url
|
32
|
+
# check http://apidocs.janio.asia/view for more information
|
33
|
+
# params accept tracking numbers as well: ```tracking_no=[tracking_no1],[tracking_no2],[tracking_no3]```
|
34
|
+
def find(*arguments)
|
35
|
+
scope = arguments.slice!(0)
|
36
|
+
options = arguments.slice!(0) || {}
|
37
|
+
options[:from] = "/api/order/order" unless options[:from]
|
38
|
+
options[:params] = {} unless options[:params]
|
39
|
+
options[:params][:secret_key] = JanioAPI.config.api_token
|
40
|
+
options[:params][:with_items] = true unless options[:params][:with_items]
|
41
|
+
|
42
|
+
case scope
|
43
|
+
when :all
|
44
|
+
find_every(options)
|
45
|
+
when :first
|
46
|
+
collection = find_every(options)
|
47
|
+
collection&.first
|
48
|
+
when :last
|
49
|
+
collection = find_every(options)
|
50
|
+
collection&.last
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
# Track one or more tracking nos
|
55
|
+
#
|
56
|
+
# Check http://apidocs.janio.asia/track for more information
|
57
|
+
def track(tracking_nos)
|
58
|
+
raise ArgumentError, "tracking_nos not an array" unless tracking_nos.is_a?(Array)
|
59
|
+
|
60
|
+
body = {
|
61
|
+
get_related_updates: true,
|
62
|
+
flatten_data: true,
|
63
|
+
tracking_nos: tracking_nos
|
64
|
+
}
|
65
|
+
response = connection.post(tracking_path, body.to_json, headers)
|
66
|
+
|
67
|
+
self.format.decode(response.body)
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
# Tracks the current order
|
72
|
+
#
|
73
|
+
# Check http://apidocs.janio.asia/track for more information
|
74
|
+
def track
|
75
|
+
body = {
|
76
|
+
get_related_updates: true,
|
77
|
+
flatten_data: true,
|
78
|
+
tracking_nos: [@attributes["tracking_no"]]
|
79
|
+
}
|
80
|
+
response = connection.post(self.class.tracking_path, body.to_json, self.class.headers)
|
81
|
+
self.class.format.decode(response.body)[0]
|
82
|
+
end
|
83
|
+
|
84
|
+
def save(blocking: true)
|
85
|
+
run_callbacks :save do
|
86
|
+
new? ? create(blocking: blocking) : update(blocking: blocking)
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
def create(blocking: true)
|
91
|
+
reformat_before_save(blocking)
|
92
|
+
super()
|
93
|
+
rescue => e
|
94
|
+
reset_attributes_format
|
95
|
+
raise e
|
96
|
+
end
|
97
|
+
|
98
|
+
def update(blocking: true)
|
99
|
+
reformat_before_save(blocking)
|
100
|
+
super()
|
101
|
+
rescue => e
|
102
|
+
reset_attributes_format
|
103
|
+
raise e
|
104
|
+
end
|
105
|
+
|
106
|
+
# Reformat the attributes before POST to server
|
107
|
+
def reformat_before_save(blocking)
|
108
|
+
attributes = @attributes.dup
|
109
|
+
@attributes.clear
|
110
|
+
@attributes["secret_key"] = JanioAPI.config.api_token
|
111
|
+
# set blocking until label generated
|
112
|
+
@attributes["blocking"] = blocking
|
113
|
+
# reformat attributes
|
114
|
+
@attributes["orders"] = [attributes]
|
115
|
+
end
|
116
|
+
|
117
|
+
def reset_attributes_format
|
118
|
+
attributes = @attributes.dup
|
119
|
+
@attributes.clear
|
120
|
+
load(attributes.delete("orders")[0])
|
121
|
+
end
|
122
|
+
|
123
|
+
def load_attributes_from_response(response)
|
124
|
+
# reset the attributes structure before assign attributes that came back from server
|
125
|
+
reset_attributes_format
|
126
|
+
|
127
|
+
# save the response attributes
|
128
|
+
if response_code_allows_body?(response.code.to_i) &&
|
129
|
+
(response["Content-Length"].nil? || response["Content-Length"] != "0") &&
|
130
|
+
!response.body.nil? && response.body.strip.size > 0
|
131
|
+
|
132
|
+
attributes = self.class.format.decode(response.body)
|
133
|
+
attributes.merge!(attributes["orders"][0]).delete("orders")
|
134
|
+
load(attributes, false, true)
|
135
|
+
@persisted = true
|
136
|
+
end
|
137
|
+
end
|
138
|
+
end
|
139
|
+
end
|
data/lib/janio_api.rb
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
require "janio_api/version"
|
2
|
+
require "active_resource"
|
3
|
+
require_relative "zeitwerk_loader"
|
4
|
+
require "dotenv/load"
|
5
|
+
module JanioAPI
|
6
|
+
class << self
|
7
|
+
attr_accessor :config
|
8
|
+
|
9
|
+
def configure
|
10
|
+
self.config ||= Configuration.new
|
11
|
+
yield(config)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
class Error < StandardError; end
|
16
|
+
# Your code goes here...
|
17
|
+
end
|
18
|
+
|
19
|
+
JanioAPI.configure do |config|
|
20
|
+
config.api_host = ENV["API_HOST"]
|
21
|
+
config.api_token = ENV["API_TOKEN"]
|
22
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
require "zeitwerk"
|
2
|
+
|
3
|
+
$__janio_api_loader__ = Zeitwerk::Loader.for_gem
|
4
|
+
$__janio_api_loader__.inflector.inflect(
|
5
|
+
"janio_api" => "JanioAPI"
|
6
|
+
)
|
7
|
+
$__janio_api_loader__.push_dir("lib")
|
8
|
+
$__janio_api_loader__.collapse("lib/janio_api/resources")
|
9
|
+
$__janio_api_loader__.enable_reloading
|
10
|
+
# loader.log!
|
11
|
+
$__janio_api_loader__.setup
|
12
|
+
def reload!
|
13
|
+
$__janio_api_loader__.reload
|
14
|
+
end
|
metadata
ADDED
@@ -0,0 +1,137 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: janio_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-04-14 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: '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'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rspec
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '3.2'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '3.2'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: dotenv
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: pry-byebug
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: pry
|
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
|
+
description:
|
84
|
+
email:
|
85
|
+
- andygg1996personal@gmail.com
|
86
|
+
executables: []
|
87
|
+
extensions: []
|
88
|
+
extra_rdoc_files: []
|
89
|
+
files:
|
90
|
+
- ".gitignore"
|
91
|
+
- ".rspec"
|
92
|
+
- ".travis.yml"
|
93
|
+
- CODE_OF_CONDUCT.md
|
94
|
+
- Gemfile
|
95
|
+
- Gemfile.lock
|
96
|
+
- LICENSE.txt
|
97
|
+
- README.md
|
98
|
+
- Rakefile
|
99
|
+
- bin/console
|
100
|
+
- bin/setup
|
101
|
+
- janio_api.gemspec
|
102
|
+
- lib/janio_api.rb
|
103
|
+
- lib/janio_api/configuration.rb
|
104
|
+
- lib/janio_api/connection.rb
|
105
|
+
- lib/janio_api/redirect_fetcher.rb
|
106
|
+
- lib/janio_api/resources/base.rb
|
107
|
+
- lib/janio_api/resources/order.rb
|
108
|
+
- lib/janio_api/version.rb
|
109
|
+
- lib/zeitwerk_loader.rb
|
110
|
+
homepage: https://github.com/PostCo/janio_api
|
111
|
+
licenses:
|
112
|
+
- MIT
|
113
|
+
metadata:
|
114
|
+
allowed_push_host: https://rubygems.org/
|
115
|
+
homepage_uri: https://github.com/PostCo/janio_api
|
116
|
+
source_code_uri: https://github.com/PostCo/janio_api
|
117
|
+
changelog_uri: https://github.com/PostCo/janio_api/releases
|
118
|
+
post_install_message:
|
119
|
+
rdoc_options: []
|
120
|
+
require_paths:
|
121
|
+
- lib
|
122
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
123
|
+
requirements:
|
124
|
+
- - ">="
|
125
|
+
- !ruby/object:Gem::Version
|
126
|
+
version: 2.3.0
|
127
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
128
|
+
requirements:
|
129
|
+
- - ">="
|
130
|
+
- !ruby/object:Gem::Version
|
131
|
+
version: '0'
|
132
|
+
requirements: []
|
133
|
+
rubygems_version: 3.1.2
|
134
|
+
signing_key:
|
135
|
+
specification_version: 4
|
136
|
+
summary: Ruby object based Janio API wrapper.
|
137
|
+
test_files: []
|