ruby-gls 1.0.2 → 1.0.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9b7cde32a4268f0464679ee590c06a28e88347dd4679ec53f508ad9c04bc93eb
4
- data.tar.gz: 51d153dc3da485baed746ee194958311c454d8913752adc31680323458627e4d
3
+ metadata.gz: 6accd06ac3fecb994bf65820e2a2387c20694fac5c87ad5aa794195957623c46
4
+ data.tar.gz: 00ce366ae98be2d9988fc673f216ff94b1b8cf0ad61f5783daa04ce347f9aba1
5
5
  SHA512:
6
- metadata.gz: b44a115a0ba6af3b3a1324754026457be216645c04b997c1d8cb7bb3c66031137eb600777f80c4264d8ff750b7b86f31801b6280eafdb8021103e7d0dfbb1166
7
- data.tar.gz: 7598bc94db828061be69c75fdbc4b304d22ef0f9ffb5e20762d5df75d1fcd4e5897c248b737cd5469a4b97f97ddbfe537aadbe47709ba150ef093fc8bd97ffa7
6
+ metadata.gz: 6bdfce2fe2f2ae250ae9adebfabd31dab844fa9bcea37e12fe824a88489409cab24c392fd199e2d5f79fe6ff57cd396d54d66b95be57a5c5a541388d8130a7c9
7
+ data.tar.gz: bee48c83d3b8e59df1101c0b38a01c8bb837fea4b3a039a45cd420b3be056d0d5a60b17af7fa0806f79db982e92a2462f50d75dae7f88ab469e0ce7290fb7669
@@ -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,71 @@
1
+ require 'ruby-gls/helper'
2
+ require 'ruby-gls/tracking'
3
+
4
+ class RubyGLS::Connection
5
+ extend RubyGLS::Tracking
6
+ include RubyGLS::Helper
7
+
8
+ attr_reader :client
9
+
10
+ def initialize(base_url:, basic_auth: nil, username: nil, password: nil, contact_id: nil)
11
+ @client = RubyGLS::Client.new(base_url, basic_auth, username, password, contact_id)
12
+ end
13
+
14
+ #
15
+ # https://shipit.gls-group.eu/webservices/3_0_6/doxygen/WS-REST-API/rest_shipment_processing.html#REST_API_REST_F_114
16
+ #
17
+ def create_parcel(**opts)
18
+ payload = opts
19
+ payload[:Shipment][:Shipper][:ContactID] = client.contact_id
20
+
21
+ action(RubyGLS::URL::CREATE_PARCEL, payload: payload)
22
+ end
23
+
24
+ #
25
+ # https://shipit.gls-group.eu/webservices/3_0_6/doxygen/WS-REST-API/rest_shipment_processing.html#REST_API_REST_F_115
26
+ #
27
+ def validate_parcels(**opts)
28
+ payload = opts
29
+ payload[:Shipment][:Shipper][:ContactID] = client.contact_id
30
+
31
+ action(RubyGLS::URL::VALIDATE_PARCELS, payload: opts)
32
+ end
33
+
34
+ #
35
+ # https://shipit.gls-group.eu/webservices/3_0_6/doxygen/WS-REST-API/rest_shipment_processing.html#REST_API_REST_F_117
36
+ #
37
+ def get_allowed_services(**opts)
38
+ action(RubyGLS::URL::GET_ALLOWED_SERVICES, payload: opts)
39
+ end
40
+
41
+ #
42
+ # https://shipit.gls-group.eu/webservices/3_0_6/doxygen/WS-REST-API/rest_shipment_processing.html#REST_API_REST_F_116
43
+ #
44
+ def cancel_parcel(tracking_number)
45
+ path = replace_string(RubyGLS::URL::CANCEL_PARCEL, ':parcel_id', tracking_number)
46
+
47
+ action(RubyGLS::URL::CANCEL_PARCEL)
48
+ end
49
+
50
+ #
51
+ # https://shipit.gls-group.eu/webservices/3_0_6/doxygen/WS-REST-API/rest_shipment_processing.html#REST_API_REST_F_119
52
+ #
53
+ def update_parcel_weight(**opts)
54
+ action(RubyGLS::URL::UPDATE_PARCEL_WEIGHT, payload: opts)
55
+ end
56
+
57
+ #
58
+ # https://shipit.gls-group.eu/webservices/3_0_6/doxygen/WS-REST-API/rest_shipment_processing.html#REST_API_REST_F_118
59
+ #
60
+ def get_end_of_day_report(date)
61
+ path = replace_string(RubyGLS::URL::GET_END_OF_DAY_REPORT, ':date', date)
62
+
63
+ action(path)
64
+ end
65
+
66
+ private
67
+
68
+ def action(path, payload: {})
69
+ client.action(path, payload: payload)
70
+ end
71
+ end
@@ -0,0 +1,7 @@
1
+ class RubyGLS
2
+ module Helper
3
+ def replace_string(full, substring, to_replace)
4
+ full.sub!(substring, to_replace.to_s)
5
+ end
6
+ end
7
+ 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
@@ -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
@@ -1,3 +1,3 @@
1
1
  class RubyGLS
2
- VERSION='1.0.2'
2
+ VERSION='1.0.5'
3
3
  end
data/lib/ruby-gls.rb CHANGED
@@ -1,12 +1,9 @@
1
1
  require 'ruby-gls/version'
2
2
  require 'ruby-gls/configuration'
3
- require 'ruby-gls/tracking'
4
3
  require 'ruby-gls/client'
5
4
  require 'ruby-gls/connection'
6
5
  require 'ruby-gls/url'
7
6
 
8
- class RubyGLS
9
- extend Tracking
10
-
7
+ class RubyGLS
11
8
  RubyGLS.configure
12
9
  end
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.2
4
+ version: 1.0.5
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: