trackplus 1.0.0.pre.beta

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: c9967e22895eb21494c52f99f246c0319a0317d3c62e7b126d52282d220c1018
4
+ data.tar.gz: 96b6f37f5d8622c137fadfd08bea8e65769d4f1367f95050b6b539917232a758
5
+ SHA512:
6
+ metadata.gz: 5720be381c082430a7dfecb8e75e04af9db004ab74b1c7ccd6e48caef71d932335e2a1c3341da604e5fd1eb6cd08301f22b7c76d60ae4e4d78d7aef1afba2760
7
+ data.tar.gz: 23c048a2943ccc81da1910291deafaa9c181babcb1ef453bda4fd8ae44ec6cf43ccdf67e6b7315c1f3c88d8589d93d87a6b474b302e3aadb77c52b0aff7cbd76
data/README.md ADDED
@@ -0,0 +1,53 @@
1
+ [![Code Climate](https://codeclimate.com/github/ammit/trackplus-ruby/badges/gpa.svg)](https://codeclimate.com/github/ammit/trackplus-ruby)
2
+ [![Build Status](https://travis-ci.org/ammit/trackplus-ruby.svg?branch=master)](https://travis-ci.org/ammit/trackplus-ruby)
3
+
4
+ # trackplus-ruby
5
+
6
+ Dead-simple Ruby API client for Trackplus. No extra runtime dependencies.
7
+
8
+ Uses v1 API provided by Trackplus.
9
+
10
+
11
+ ## Installation
12
+
13
+ Add this line to your application's Gemfile:
14
+
15
+ ```ruby
16
+ gem 'trackplus', git: 'https://github.com/ammit/trackplus-ruby'
17
+ ```
18
+
19
+ And then execute:
20
+ ```
21
+ $ bundle
22
+ ```
23
+
24
+ ## Usage
25
+
26
+ Gem covers all traclings & available couriers api
27
+
28
+ #### Example
29
+
30
+ ``` ruby
31
+ client = Trackplus::Client.new(api_key: 'api_key')
32
+
33
+ # get supported couriers
34
+ client.couriers
35
+
36
+ # get tracking
37
+ client.tracking(courier: 'airwings', tracking_no: '202315836553')
38
+
39
+
40
+ # Possible errors (each one inherits from Trackplus::Errors::TrackplusError)
41
+ Trackplus::Errors::InvalidConfiguration # missing api_key / subdomain
42
+ Trackplus::Errors::NotAuthorized # wrong api key
43
+ Trackplus::Errors::InvalidResponse # something went wrong during the request?
44
+ Trackplus::Errors::NotFound # 404 from Trackplus
45
+ Trackplus::Errors::RequestToLong # When the requested result takes to long to calculate, try limiting your query
46
+ ```
47
+
48
+ ## Contributing
49
+ 1. Fork it ( https://github.com/ammit/trackplus-ruby/fork )
50
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
51
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
52
+ 4. Push to the branch (`git push origin my-new-feature`)
53
+ 5. Create a new Pull Request
data/lib/trackplus.rb ADDED
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'json'
4
+ require 'uri'
5
+ require 'net/http'
6
+ require 'date'
7
+ require 'ostruct'
8
+ require 'cgi'
9
+ require 'forwardable'
10
+
11
+ require_relative 'trackplus/version'
12
+ require_relative 'trackplus/transformation'
13
+ require_relative 'trackplus/errors'
14
+ require_relative 'trackplus/client'
15
+
16
+ module Trackplus
17
+ API_VERSION = 1
18
+ end
@@ -0,0 +1,101 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Trackplus
4
+ class Client
5
+ def initialize(options = {})
6
+ @max_retries = options.fetch(:max_retries, 3)
7
+ @api_key = options.fetch(:api_key) { configuration_error 'Missing api_key argument' }
8
+ @transform_to = Transformation.new(options[:transform_to])
9
+ end
10
+
11
+ # returns a collection of your account members
12
+ def tracking(courier:, tracking_no:)
13
+ @transform_to.apply(nil, get_request("trackings/#{courier}/#{tracking_no}")['data'])
14
+ end
15
+
16
+ def couriers
17
+ @transform_to.apply(nil, get_request('couriers')['data'])
18
+ end
19
+
20
+ private
21
+
22
+ attr_reader :api_key
23
+
24
+ # do the get request to api
25
+ def get_request(url, params = {})
26
+ params = URI.encode_www_form(params.keep_if { |k, v| k && v })
27
+ full_url = params.empty? ? url : [url, params].join('?')
28
+ do_request(full_url, Net::HTTP::Get)
29
+ end
30
+
31
+ # generic part of requesting api
32
+ def do_request(url, type, &_block)
33
+ retries = 0
34
+ response = nil
35
+
36
+ uri = URI.parse("#{api_url}/#{url}")
37
+ http = Net::HTTP.new(uri.host, uri.port)
38
+ # http.use_ssl = true
39
+
40
+ request = type.new(uri.request_uri, headers)
41
+
42
+ yield request if block_given?
43
+
44
+ http.start do |h|
45
+ begin
46
+ response = h.request(request)
47
+ rescue Errno::ECONNREFUSED, Net::ReadTimeout, Net::OpenTimeout => e
48
+ if (retries += 1) <= @max_retries
49
+ sleep(retries)
50
+ retry
51
+ else
52
+ raise Errors::MaxRetriesExceeded
53
+ end
54
+ ensure
55
+ h.finish if h
56
+ end
57
+ end
58
+
59
+ parse!(response)
60
+ end
61
+
62
+ # parse the api response
63
+ def parse!(response)
64
+ case response.code.to_i
65
+ when 204, 205
66
+ nil
67
+ when 200...300
68
+ JSON.parse(response.body) unless response.body.to_s.empty?
69
+ when 401
70
+ raise Errors::NotAuthorized, JSON.parse(response.body)['error']
71
+ when 404
72
+ raise Errors::NotFound, JSON.parse(response.body)['error']
73
+ when 422
74
+ handle_response_422(response)
75
+ when 503
76
+ raise Errors::RequestToLong, response.body
77
+ else
78
+ raise Errors::InvalidResponse, "Response code: #{response.code} message: #{response.body}"
79
+ end
80
+ end
81
+
82
+ # default headers for authentication and JSON support
83
+ def headers
84
+ {
85
+ 'Accept' => 'application/json',
86
+ 'Authorization' => "Bearer #{api_key}",
87
+ 'Content-Type' => 'application/json',
88
+ 'User-Agent' => 'Workable Ruby Client'
89
+ }
90
+ end
91
+
92
+ # build the url to api
93
+ def api_url
94
+ @_api_url ||= format('http://167.71.238.124/api/v%s', Trackplus::API_VERSION)
95
+ end
96
+
97
+ def configuration_error(message)
98
+ raise Errors::InvalidConfiguration, message
99
+ end
100
+ end
101
+ end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Trackplus
4
+ module Errors
5
+ class TrackplusError < StandardError; end
6
+ class InvalidConfiguration < TrackplusError; end
7
+ class NotAuthorized < TrackplusError; end
8
+ class InvalidResponse < TrackplusError; end
9
+ class NotFound < TrackplusError; end
10
+ class AlreadyExists < TrackplusError; end
11
+ class RequestToLong < TrackplusError; end
12
+ class MaxRetriesExceeded < TrackplusError; end
13
+ end
14
+ end
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Trackplus
4
+ class Transformation
5
+ def initialize(mappings)
6
+ @mappings = mappings || {}
7
+ end
8
+
9
+ # selects transformation strategy based on the inputs
10
+ # @param transformation [Method|Proc|nil] the transformation to perform
11
+ # @param data [Hash|Array|nil] the data to transform
12
+ # @return [Object|nil]
13
+ # results of the transformation if given, raw data otherwise
14
+ def apply(mapping, data)
15
+ transformation = @mappings[mapping]
16
+ return data unless transformation
17
+
18
+ case data
19
+ when nil
20
+ data
21
+ when Array
22
+ data.map { |datas| transformation.call(datas) }
23
+ else
24
+ transformation.call(data)
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Trackplus
4
+ VERSION = '1.0.0-beta'
5
+ end
metadata ADDED
@@ -0,0 +1,92 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: trackplus
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0.pre.beta
5
+ platform: ruby
6
+ authors:
7
+ - Amit Kosti
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-09-21 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: pry
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
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: 2.14.1
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 2.14.1
41
+ - !ruby/object:Gem::Dependency
42
+ name: webmock
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 2.3.2
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 2.3.2
55
+ description: Developed for easy integration with TrackPlus
56
+ email:
57
+ - akosti92@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - README.md
63
+ - lib/trackplus.rb
64
+ - lib/trackplus/client.rb
65
+ - lib/trackplus/errors.rb
66
+ - lib/trackplus/transformation.rb
67
+ - lib/trackplus/version.rb
68
+ homepage: https://github.com/ammit/trackplus-ruby
69
+ licenses:
70
+ - MIT
71
+ metadata: {}
72
+ post_install_message:
73
+ rdoc_options: []
74
+ require_paths:
75
+ - lib
76
+ required_ruby_version: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - ">="
79
+ - !ruby/object:Gem::Version
80
+ version: 2.4.0
81
+ required_rubygems_version: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - ">"
84
+ - !ruby/object:Gem::Version
85
+ version: 1.3.1
86
+ requirements:
87
+ - none
88
+ rubygems_version: 3.0.3
89
+ signing_key:
90
+ specification_version: 4
91
+ summary: Support the latest V1 API
92
+ test_files: []