ruby-gls 0.0.4 → 1.0.3

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: 1161f9e8417e9964244f2a7fe0ba28547ca155297d892276ea5619dc0f78c8e8
4
- data.tar.gz: de9369a3e933f084461833e680fb5e06658ded3878a9678240a2ed540adf7155
3
+ metadata.gz: ec195d660c69f8c4dfc010c87d6c38127949c715f2bbfde62d29947472d368b1
4
+ data.tar.gz: 537ca2fc2af3c33bf7bf5012eae7a5db4111a4fceba7a0b28dc884c3ee9a6c42
5
5
  SHA512:
6
- metadata.gz: afff4c8d267a2c90601f241a44902f2607058ff192177ef58f4ffb3d793f3dadc86c027f99c40f4538444703e68458cfedde32d370ffdbc3fe2f876756fd4d88
7
- data.tar.gz: 43b9bca4b55b8503f7107987cd7bd0eedb6c5068e1370cc2c0632310be5b66d58b2d833ad6c432739bbb95ac2499d02b9a5489f5d62053ef31270ecfce064885
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,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='0.0.4'
2
+ VERSION='1.0.3'
3
3
  end
data/lib/ruby-gls.rb CHANGED
@@ -1,69 +1,12 @@
1
- require 'net/http'
2
- require 'json'
3
-
4
1
  require 'ruby-gls/version'
5
2
  require 'ruby-gls/configuration'
6
- require 'nokogiri'
3
+ require 'ruby-gls/tracking'
4
+ require 'ruby-gls/client'
5
+ require 'ruby-gls/connection'
6
+ require 'ruby-gls/url'
7
7
 
8
8
  class RubyGLS
9
+ extend Tracking
10
+
9
11
  RubyGLS.configure
10
-
11
- class << self
12
-
13
- def find(tracking_number)
14
- uri = URI(generate_url(tracking_number))
15
- res = Net::HTTP.get_response(uri)
16
-
17
- parsed = JSON.parse(res.body)
18
-
19
- key = parsed.dig('content').keys.last
20
- html = parsed.dig('content', key, 'html')
21
-
22
- doc = Nokogiri::HTML(html)
23
-
24
- status = doc.css('.trigger-status').text.strip
25
- status_desc = doc.css('.text-primary.lead strong').text.strip
26
- parcel_number = doc.css('.row.justify-content-left').text&.strip&.split(':')&.last&.strip
27
-
28
- detailed = [].tap do |arr|
29
- doc.css('.data_table tbody tr').each do |row|
30
-
31
- date, time, parcel_status, loc = row.css('td').map(&:text).map(&:strip)
32
-
33
- arr << {
34
- date: date,
35
- time: time,
36
- parcel_status: parcel_status,
37
- location: loc
38
- }
39
- end
40
- end
41
-
42
- if status == ""
43
- status = 'Not found'
44
- status_desc = "No tracking information found for #{tracking_number}"
45
- end
46
-
47
- {
48
- tracking_number: tracking_number,
49
- status: status,
50
- status_description: status_desc,
51
- parcel_number: parcel_number,
52
- detailed_tracking: detailed
53
- }
54
- rescue => e
55
- {
56
- tracking_number: tracking_number,
57
- status: 'Not found',
58
- status_description: "#{e.message}"
59
- }
60
- end
61
-
62
- private
63
-
64
- def generate_url(tracking_number)
65
- RubyGLS.config.base_url + "&match=#{tracking_number}"
66
- end
67
-
68
- end
69
12
  end
metadata CHANGED
@@ -1,15 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby-gls
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 1.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Adem Dinarevic
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-06-28 00:00:00.000000000 Z
11
+ date: 2022-07-22 00:00:00.000000000 Z
12
12
  dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rest-client
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 2.1.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 2.1.0
13
27
  - !ruby/object:Gem::Dependency
14
28
  name: rspec
15
29
  requirement: !ruby/object:Gem::Requirement
@@ -46,7 +60,12 @@ extensions: []
46
60
  extra_rdoc_files: []
47
61
  files:
48
62
  - lib/ruby-gls.rb
63
+ - lib/ruby-gls/client.rb
49
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
50
69
  - lib/ruby-gls/version.rb
51
70
  homepage: https://github.com/ademdc/ruby-gls
52
71
  licenses: