Class: Inpost::Parcel
- Inherits:
-
Object
- Object
- Inpost::Parcel
- Defined in:
- lib/inpost_parcels_tracker.rb
Constant Summary collapse
- HOST =
'https://api-shipx-pl.easypack24.net/v1/tracking/'.freeze
Instance Method Summary collapse
-
#initialize(tracking_code) ⇒ Parcel
constructor
A new instance of Parcel.
- #parse(body) ⇒ Object
- #request(args = {}) ⇒ Object
- #track ⇒ Object
- #uri(args = {}) ⇒ Object
Constructor Details
#initialize(tracking_code) ⇒ Parcel
Returns a new instance of Parcel.
9 10 11 12 13 |
# File 'lib/inpost_parcels_tracker.rb', line 9 def initialize(tracking_code) raise 'Tracking code cannot be nil' if tracking_code.blank? raise 'Invalid tracking code provided' if tracking_code.size < 24 @tracking_code = tracking_code end |
Instance Method Details
#parse(body) ⇒ Object
38 39 40 |
# File 'lib/inpost_parcels_tracker.rb', line 38 def parse(body) JSON.parse(body.gsub(/\{\} && /, '')).deep_symbolize_keys end |
#request(args = {}) ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/inpost_parcels_tracker.rb', line 15 def request(args = {}) method = args.fetch(:method, :GET) path = args.fetch(:path, '') path = '/' + path if path[0] != '/' && path.size >= 1 response = nil Net::HTTP.start(uri.host,uri.port, use_ssl: uri.scheme == 'https', open_timeout: 2.minutes, read_timeout: 2.minutes, keep_alive_timeout: 20.seconds, verify_mode: OpenSSL::SSL::VERIFY_NONE) do |http| case method when :GET, :get req = Net::HTTP::Get.new uri(path: path) else raise 'Unexpected method: ' + method end response = http.request(req) end parse(response.body) end |
#track ⇒ Object
42 43 44 |
# File 'lib/inpost_parcels_tracker.rb', line 42 def track request(method: :get, path: @tracking_code) end |
#uri(args = {}) ⇒ Object
46 47 48 |
# File 'lib/inpost_parcels_tracker.rb', line 46 def uri(args = {}) URI.parse(HOST + args.fetch(:path, '')) end |