ruby-gls 1.0.2 → 1.0.3
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 +4 -4
- data/lib/ruby-gls/client.rb +65 -0
- data/lib/ruby-gls/connection.rb +69 -0
- data/lib/ruby-gls/helper.rb +7 -0
- data/lib/ruby-gls/tracking.rb +62 -0
- data/lib/ruby-gls/url.rb +8 -0
- data/lib/ruby-gls/version.rb +1 -1
- metadata +6 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ec195d660c69f8c4dfc010c87d6c38127949c715f2bbfde62d29947472d368b1
|
4
|
+
data.tar.gz: 537ca2fc2af3c33bf7bf5012eae7a5db4111a4fceba7a0b28dc884c3ee9a6c42
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9d7dd576e9a242a0a50f8647e61a2eb6960dde01f04d49847cbed92e1c9705fb5030651a43515722ce82fe2df08b5ccba90a52bfc081ef1bc13db5528dd944a9
|
7
|
+
data.tar.gz: 9135769d192ce68080d42454d13f462f6196e84209ee3805c338069e81dabbb74a56660de57e0e4e4fcb4ec1c413feabd062b4a2b9387e077845dcf916bd7afd
|
@@ -0,0 +1,65 @@
|
|
1
|
+
require 'rest-client'
|
2
|
+
require 'json'
|
3
|
+
require "base64"
|
4
|
+
|
5
|
+
class RubyGLS::Client
|
6
|
+
|
7
|
+
attr_reader :base_url, :basic_auth, :username, :password, :contact_id
|
8
|
+
|
9
|
+
def initialize(base_url, basic_auth, username, password, contact_id)
|
10
|
+
@base_url = base_url
|
11
|
+
@basic_auth = basic_auth
|
12
|
+
@username = username
|
13
|
+
@password = password
|
14
|
+
@contact_id = contact_id
|
15
|
+
|
16
|
+
raise ArgumentError('Either basic_auth or username and password not provided') unless basic_auth || (username && password)
|
17
|
+
end
|
18
|
+
|
19
|
+
def action(path, http_method: :post, payload: {})
|
20
|
+
begin
|
21
|
+
response = ::RestClient::Request.execute(
|
22
|
+
method: http_method,
|
23
|
+
url: full_url(path),
|
24
|
+
payload: payload.to_json,
|
25
|
+
headers: headers,
|
26
|
+
timeout: 5,
|
27
|
+
verify_ssl: ::OpenSSL::SSL::VERIFY_NONE
|
28
|
+
)
|
29
|
+
rescue => e
|
30
|
+
return struct_response.new(false, { error: e.message })
|
31
|
+
end
|
32
|
+
|
33
|
+
parse(response)
|
34
|
+
end
|
35
|
+
|
36
|
+
def get_basic_auth
|
37
|
+
return basic_auth if basic_auth
|
38
|
+
|
39
|
+
Base64.encode64("#{username}:#{password}")
|
40
|
+
end
|
41
|
+
|
42
|
+
def headers
|
43
|
+
{
|
44
|
+
'User-Agent': "RubyGLS client v#{RubyGLS::VERSION})",
|
45
|
+
'Content-Type': 'application/glsVersion1+json',
|
46
|
+
'Accept': 'application/glsVersion1+json, application/json',
|
47
|
+
'Authorization': "Basic #{get_basic_auth}"
|
48
|
+
}
|
49
|
+
end
|
50
|
+
|
51
|
+
def full_url(path)
|
52
|
+
"#{base_url}#{path}"
|
53
|
+
end
|
54
|
+
|
55
|
+
def parse(response)
|
56
|
+
success = (200..308).to_a.include?(response.code.to_i) ? true : false
|
57
|
+
hash_response = JSON.parse(response.body)
|
58
|
+
|
59
|
+
struct_response.new(success, hash_response)
|
60
|
+
end
|
61
|
+
|
62
|
+
def struct_response
|
63
|
+
Struct.new(:success?, :hash_response)
|
64
|
+
end
|
65
|
+
end
|
@@ -0,0 +1,69 @@
|
|
1
|
+
require 'ruby-gls/helper'
|
2
|
+
|
3
|
+
class RubyGLS::Connection
|
4
|
+
include RubyGLS::Helper
|
5
|
+
|
6
|
+
attr_reader :client
|
7
|
+
|
8
|
+
def initialize(base_url:, basic_auth: nil, username: nil, password: nil, contact_id: nil)
|
9
|
+
@client = RubyGLS::Client.new(base_url, basic_auth, username, password, contact_id)
|
10
|
+
end
|
11
|
+
|
12
|
+
#
|
13
|
+
# https://shipit.gls-group.eu/webservices/3_0_6/doxygen/WS-REST-API/rest_shipment_processing.html#REST_API_REST_F_114
|
14
|
+
#
|
15
|
+
def create_parcel(**opts)
|
16
|
+
payload = opts
|
17
|
+
payload[:Shipment][:Shipper][:ContactID] = client.contact_id
|
18
|
+
|
19
|
+
action(RubyGLS::URL::CREATE_PARCEL, payload: payload)
|
20
|
+
end
|
21
|
+
|
22
|
+
#
|
23
|
+
# https://shipit.gls-group.eu/webservices/3_0_6/doxygen/WS-REST-API/rest_shipment_processing.html#REST_API_REST_F_115
|
24
|
+
#
|
25
|
+
def validate_parcels(**opts)
|
26
|
+
payload = opts
|
27
|
+
payload[:Shipment][:Shipper][:ContactID] = client.contact_id
|
28
|
+
|
29
|
+
action(RubyGLS::URL::VALIDATE_PARCELS, payload: opts)
|
30
|
+
end
|
31
|
+
|
32
|
+
#
|
33
|
+
# https://shipit.gls-group.eu/webservices/3_0_6/doxygen/WS-REST-API/rest_shipment_processing.html#REST_API_REST_F_117
|
34
|
+
#
|
35
|
+
def get_allowed_services(**opts)
|
36
|
+
action(RubyGLS::URL::GET_ALLOWED_SERVICES, payload: opts)
|
37
|
+
end
|
38
|
+
|
39
|
+
#
|
40
|
+
# https://shipit.gls-group.eu/webservices/3_0_6/doxygen/WS-REST-API/rest_shipment_processing.html#REST_API_REST_F_116
|
41
|
+
#
|
42
|
+
def cancel_parcel(tracking_number)
|
43
|
+
path = replace_string(RubyGLS::URL::CANCEL_PARCEL, ':parcel_id', tracking_number)
|
44
|
+
|
45
|
+
action(RubyGLS::URL::CANCEL_PARCEL)
|
46
|
+
end
|
47
|
+
|
48
|
+
#
|
49
|
+
# https://shipit.gls-group.eu/webservices/3_0_6/doxygen/WS-REST-API/rest_shipment_processing.html#REST_API_REST_F_119
|
50
|
+
#
|
51
|
+
def update_parcel_weight(**opts)
|
52
|
+
action(RubyGLS::URL::UPDATE_PARCEL_WEIGHT, payload: opts)
|
53
|
+
end
|
54
|
+
|
55
|
+
#
|
56
|
+
# https://shipit.gls-group.eu/webservices/3_0_6/doxygen/WS-REST-API/rest_shipment_processing.html#REST_API_REST_F_118
|
57
|
+
#
|
58
|
+
def get_end_of_day_report(date)
|
59
|
+
path = replace_string(RubyGLS::URL::GET_END_OF_DAY_REPORT, ':date', date)
|
60
|
+
|
61
|
+
action(path)
|
62
|
+
end
|
63
|
+
|
64
|
+
private
|
65
|
+
|
66
|
+
def action(path, payload: {})
|
67
|
+
client.action(path, payload: payload)
|
68
|
+
end
|
69
|
+
end
|
@@ -0,0 +1,62 @@
|
|
1
|
+
require 'net/http'
|
2
|
+
require 'json'
|
3
|
+
require 'nokogiri'
|
4
|
+
|
5
|
+
class RubyGLS
|
6
|
+
module Tracking
|
7
|
+
def find(tracking_number)
|
8
|
+
uri = URI(generate_url(tracking_number))
|
9
|
+
res = Net::HTTP.get_response(uri)
|
10
|
+
|
11
|
+
parsed = JSON.parse(res.body)
|
12
|
+
|
13
|
+
key = parsed.dig('content').keys.last
|
14
|
+
html = parsed.dig('content', key, 'html')
|
15
|
+
|
16
|
+
doc = Nokogiri::HTML(html)
|
17
|
+
|
18
|
+
status = doc.css('.trigger-status').text.strip
|
19
|
+
status_desc = doc.css('.text-primary.lead strong').text.strip
|
20
|
+
parcel_number = doc.css('.row.justify-content-left').text&.strip&.split(':')&.last&.strip
|
21
|
+
|
22
|
+
detailed = [].tap do |arr|
|
23
|
+
doc.css('.data_table tbody tr').each do |row|
|
24
|
+
|
25
|
+
date, time, parcel_status, loc = row.css('td').map(&:text).map(&:strip)
|
26
|
+
|
27
|
+
arr << {
|
28
|
+
date: date,
|
29
|
+
time: time,
|
30
|
+
parcel_status: parcel_status,
|
31
|
+
location: loc
|
32
|
+
}
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
if status == ""
|
37
|
+
status = 'Not found'
|
38
|
+
status_desc = "No tracking information found for #{tracking_number}"
|
39
|
+
end
|
40
|
+
|
41
|
+
{
|
42
|
+
tracking_number: tracking_number,
|
43
|
+
status: status,
|
44
|
+
status_description: status_desc,
|
45
|
+
parcel_number: parcel_number,
|
46
|
+
detailed_tracking: detailed
|
47
|
+
}
|
48
|
+
rescue => e
|
49
|
+
{
|
50
|
+
tracking_number: tracking_number,
|
51
|
+
status: 'Not found',
|
52
|
+
status_description: "#{e.message}"
|
53
|
+
}
|
54
|
+
end
|
55
|
+
|
56
|
+
private
|
57
|
+
|
58
|
+
def generate_url(tracking_number)
|
59
|
+
RubyGLS.config.base_url + "&match=#{tracking_number}"
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
data/lib/ruby-gls/url.rb
ADDED
@@ -0,0 +1,8 @@
|
|
1
|
+
module RubyGLS::URL
|
2
|
+
CREATE_PARCEL = '/backend/rs/shipments'
|
3
|
+
CANCEL_PARCEL = '//backend/rs/shipments/cancel/:parcel_id'
|
4
|
+
GET_ALLOWED_SERVICES = '/backend/rs/shipments/allowedservices'
|
5
|
+
VALIDATE_PARCELS = '/backend/rs/shipments/validate'
|
6
|
+
GET_END_OF_DAY_REPORT = '/backend/rs/shipments/endofday?date=:date'
|
7
|
+
UPDATE_PARCEL_WEIGHT = '/backend/rs/shipments/updateparcelweight'
|
8
|
+
end
|
data/lib/ruby-gls/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ruby-gls
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Adem Dinarevic
|
@@ -60,7 +60,12 @@ extensions: []
|
|
60
60
|
extra_rdoc_files: []
|
61
61
|
files:
|
62
62
|
- lib/ruby-gls.rb
|
63
|
+
- lib/ruby-gls/client.rb
|
63
64
|
- lib/ruby-gls/configuration.rb
|
65
|
+
- lib/ruby-gls/connection.rb
|
66
|
+
- lib/ruby-gls/helper.rb
|
67
|
+
- lib/ruby-gls/tracking.rb
|
68
|
+
- lib/ruby-gls/url.rb
|
64
69
|
- lib/ruby-gls/version.rb
|
65
70
|
homepage: https://github.com/ademdc/ruby-gls
|
66
71
|
licenses:
|