ruby-expressone 0.0.1

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: 65446d629b7f7f1bc2683dcd727ffd78f9f5a8ca81ce5e72c4ec2b98b12f31eb
4
+ data.tar.gz: 2a4da851c930be66468286545498dd86aeb80512b21d45ba8304f4a3eef9018a
5
+ SHA512:
6
+ metadata.gz: 0eab5e805864feba7731a4089c71c478ab1741e7f99316b494a98e956211f73bcaa2ef24a10f4e4ff32037da99d40b6e29266d1a5008203afaef4160e05b2445
7
+ data.tar.gz: eaa6ecb1ad1759ade0b29dea6c70e16a2baad91b7315f2ef9a3777a99535972e106e122a5d25db4660f6df6f7777cbe8bff3e5eaeb5049d52ad8e948d82a44d7
@@ -0,0 +1,74 @@
1
+ require 'rest-client'
2
+ require 'json'
3
+ require "base64"
4
+
5
+ module RubyExpressOne
6
+ class Client
7
+
8
+ attr_reader :api_key, :with_logger
9
+
10
+ def initialize(api_key, with_logger: false)
11
+ @api_key = api_key
12
+ @with_logger = with_logger
13
+ end
14
+
15
+ def action(path, http_method: :post, payload: {}, query_params: {})
16
+ begin
17
+ response = ::RestClient::Request.execute(
18
+ method: http_method,
19
+ url: full_url(path, query_params),
20
+ payload: payload.to_json,
21
+ headers: headers,
22
+ timeout: 5,
23
+ verify_ssl: ::OpenSSL::SSL::VERIFY_NONE
24
+ )
25
+ rescue => e
26
+ return parse(e.response)
27
+ end
28
+
29
+ parse(response)
30
+ end
31
+
32
+ private
33
+
34
+ def headers
35
+ {
36
+ 'User-Agent': "Ruby In Time client v#{RubyExpressOne::VERSION})",
37
+ 'Content-Type': 'application/json',
38
+ 'Accept': 'application/json'
39
+ }
40
+ end
41
+
42
+ def full_url(path, query_params={})
43
+ query_params.merge!(ApiKey: api_key)
44
+ query_string = hash_to_query(query_params)
45
+
46
+ "#{base_url}#{path}?#{query_string}"
47
+ end
48
+
49
+ def parse(response)
50
+ success = (200..308).to_a.include?(response.code.to_i) ? true : false
51
+ hash_response = JSON.parse(response.body) rescue response.body
52
+
53
+ struct_response.new(success, hash_response)
54
+ end
55
+
56
+ def struct_response
57
+ Struct.new(:success?, :hash_response)
58
+ end
59
+
60
+ def print_log(log, debug_type: :info)
61
+ if with_logger
62
+ RubyExpressOne.config.logger.send debug_type, "<---- #{log} ----->"
63
+ end
64
+ end
65
+
66
+ def base_url
67
+ 'http://webapi.expressone.ba/api/data'
68
+ end
69
+
70
+ def hash_to_query(query_params)
71
+ query_params.to_a.map { |x| "#{x[0]}=#{x[1]}" }.join("&")
72
+ end
73
+ end
74
+ end
@@ -0,0 +1,20 @@
1
+ module RubyExpressOne
2
+ class << self
3
+ # Accessor for global configuration.
4
+ attr_accessor :config
5
+ end
6
+
7
+ def self.configure
8
+ self.config ||= Configuration.new
9
+ yield(config) if block_given?
10
+ end
11
+
12
+ class Configuration
13
+ attr_accessor :token_endpoint, :logger
14
+
15
+ def initialize
16
+ @token_endpoint = 'https://intime-ba.eu.auth0.com'
17
+ @logger = RubyExpressOne::Logger
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,43 @@
1
+ module RubyExpressOne
2
+ class Connection
3
+
4
+ attr_reader :client, :api_key
5
+
6
+ def initialize(api_key, with_logger: false)
7
+ @api_key = api_key
8
+
9
+ @client = RubyExpressOne::Client.new(api_key, with_logger: with_logger)
10
+ end
11
+
12
+ def get_shipment(id)
13
+ query_params = { ShipmentId: id }
14
+
15
+ action("/GetShipmentStatusByShipmentId", http_method: :get, query_params: query_params)
16
+ end
17
+
18
+ def create_shipment(payload, plain: true)
19
+
20
+ path = plain ? '/CreateShipmentPlain' : '/CreateShipment'
21
+
22
+ action(path, payload: payload)
23
+ end
24
+
25
+ def confirm_shipment(trackingNumber)
26
+ action("/api/shipments/confirmshipments?trackingNumber=#{trackingNumber}", http_method: :put)
27
+ end
28
+
29
+ def update_shipment(id, payload)
30
+ action("/api/shipments/#{id}", payload: payload, http_method: :patch)
31
+ end
32
+
33
+ def get_shipment_events(id)
34
+ action("/api/shipments/#{id}/shipmentevents", http_method: :get)
35
+ end
36
+
37
+ private
38
+
39
+ def action(path, payload: {}, http_method: :post, query_params: {})
40
+ client.action(path, payload: payload, http_method: http_method, query_params: query_params)
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,15 @@
1
+ module RubyExpressOne
2
+ class Logger
3
+ def self.info(log)
4
+ puts log
5
+ end
6
+
7
+ def self.warn(log)
8
+ puts log
9
+ end
10
+
11
+ def self.error(log)
12
+ puts log
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,3 @@
1
+ module RubyExpressOne
2
+ VERSION='0.0.1'
3
+ end
@@ -0,0 +1,10 @@
1
+ require 'ruby-expressone/version'
2
+ require 'ruby-expressone/configuration'
3
+ require 'ruby-expressone/client'
4
+ require 'ruby-expressone/connection'
5
+ require 'ruby-expressone/logger'
6
+ require 'byebug'
7
+
8
+ module RubyExpressOne;end
9
+
10
+ RubyExpressOne.configure
metadata ADDED
@@ -0,0 +1,77 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ruby-expressone
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Adem Dinarevic
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2022-08-10 00:00:00.000000000 Z
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
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '3.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '3.0'
41
+ description: Ruby client for ExpressOne API
42
+ email:
43
+ - ademdinarevic@gmail.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - lib/ruby-expressone.rb
49
+ - lib/ruby-expressone/client.rb
50
+ - lib/ruby-expressone/configuration.rb
51
+ - lib/ruby-expressone/connection.rb
52
+ - lib/ruby-expressone/logger.rb
53
+ - lib/ruby-expressone/version.rb
54
+ homepage: https://github.com/ademdc/ruby-expressone
55
+ licenses:
56
+ - MIT
57
+ metadata: {}
58
+ post_install_message:
59
+ rdoc_options: []
60
+ require_paths:
61
+ - lib
62
+ required_ruby_version: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ version: '2.3'
67
+ required_rubygems_version: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ version: '0'
72
+ requirements: []
73
+ rubygems_version: 3.1.2
74
+ signing_key:
75
+ specification_version: 4
76
+ summary: Ruby client for ExpressOne API
77
+ test_files: []