tcat 0.1.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: 93041a7737311b5ce2f8e9ca00f40ef610292ffe15cb394f84d20a2d9b965bb0
4
+ data.tar.gz: f1ed90761a2bf579b201da96e3681212e2fd222b5cf287c9d69668aa58edd9d3
5
+ SHA512:
6
+ metadata.gz: 9d431972fceaaaa6a2e6d0bba2185e0368170d25eabf3bafa84a34b6e74baf49ff59be78a6a5804c128903b3d2ba897180f609acbe8f071a082223c9041eabde
7
+ data.tar.gz: 61603c22b4e3d0f3035364d77b66c4f1a1707921d5a0939b4509c6551226fae4c97bb36154f7306f35ca399cd13f2a9debe8f3e0e270510a433eaa100df5599b
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,13 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.7
3
+
4
+ # Style/StringLiterals:
5
+ # Enabled: true
6
+ # EnforcedStyle: double_quotes
7
+
8
+ # Style/StringLiteralsInInterpolation:
9
+ # Enabled: true
10
+ # EnforcedStyle: double_quotes
11
+
12
+ Layout/LineLength:
13
+ Max: 120
data/.tool-versions ADDED
@@ -0,0 +1 @@
1
+ ruby 3.3.3
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2024 Zac
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,35 @@
1
+ # Tcat
2
+
3
+ TODO: Delete this and the text below, and describe your gem
4
+
5
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/tcat`. To experiment with that code, run `bin/console` for an interactive prompt.
6
+
7
+ ## Installation
8
+
9
+ TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org.
10
+
11
+ Install the gem and add to the application's Gemfile by executing:
12
+
13
+ $ bundle add UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
14
+
15
+ If bundler is not being used to manage dependencies, install the gem by executing:
16
+
17
+ $ gem install UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Development
24
+
25
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
26
+
27
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
28
+
29
+ ## Contributing
30
+
31
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/tcat.
32
+
33
+ ## License
34
+
35
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require "rubocop/rake_task"
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[spec rubocop]
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ module TcatTracker
4
+ class Configuration
5
+ attr_accessor :secret_string, :secret_key
6
+
7
+ def initialize
8
+ @secret_string = nil
9
+ @secret_key = nil
10
+ end
11
+ end
12
+ end
data/lib/tcat/query.rb ADDED
@@ -0,0 +1,101 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'net/http'
4
+ require 'base64'
5
+ require 'ox'
6
+
7
+ module Tcat
8
+ class Query
9
+ def initialize(tracking_number)
10
+ @secret_string = TcatTracker.configuration.secret_string
11
+ @secret_key = TcatTracker.configuration.secret_key
12
+ validate_secrets!
13
+
14
+ @tracking_number = tracking_number
15
+ end
16
+
17
+ def perform
18
+ cookies = 'citrix_ns_id=AAE7go4rZDusCRcEAAAAADuVBLGLdqo1FolMO02r_jOEnjNmr4u9sIYwtllmmfsOOw==KJArZA==AQJLxyeLYOXPo3Uzfh_rWrr_NcU=; ASP.NET_SessionId=p4v1z4nijrynbortgf4iqml4'
19
+ ua = 'BlackCat/2.42.2 (iPhone; iOS 16.4; Scale/3.00)'
20
+ headers = { 'User-Agent': ua, 'Accept-Language': 'zh-Hant-TW;q=1, en-TW;q=0.9', Cookie: cookies }
21
+ uri = URI('https://www.t-cat.com.tw/iPhone/TCatApp.aspx?ver=24.26')
22
+
23
+ http = Net::HTTP.new(uri.host, uri.port)
24
+ http.use_ssl = true
25
+ body = URI.encode_www_form(data)
26
+ response = Net::HTTP.post(uri, body, headers)
27
+
28
+ parse_response(response.body) if response.code == '200'
29
+ end
30
+
31
+ private
32
+
33
+ def validate_secrets!
34
+ raise ArgumentError, "SECRET_STRING must be configured" if @secret_string.nil? || @secret_string.empty?
35
+ raise ArgumentError, "SECRET_KEY must be configured" if @secret_key.nil? || @secret_key.empty?
36
+ end
37
+
38
+ def data
39
+ {
40
+ ConsignmentNo: @tracking_number,
41
+ f: 5,
42
+ isForeign: 'N',
43
+ secret: secret,
44
+ SVC: 'TCATAPP'
45
+ }
46
+ end
47
+
48
+ def date_string
49
+ date = Time.now.utc
50
+ date.strftime('%Y%m%d%H%M%S')
51
+ end
52
+
53
+ def random
54
+ rand(10_000..99_999).to_s
55
+ end
56
+
57
+ def source_string
58
+ random + @secret_string + date_string
59
+ end
60
+
61
+ def secret
62
+ cipher = OpenSSL::Cipher.new('AES-128-ECB')
63
+ cipher.encrypt
64
+ cipher.key = @secret_key
65
+
66
+ block_size = cipher.block_size
67
+ padding_size = block_size - (source_string.length % block_size)
68
+ padding = padding_size.chr * padding_size
69
+ padded_message = source_string + padding
70
+
71
+ encrypted = cipher.update(padded_message)
72
+ Base64.strict_encode64(encrypted)
73
+ end
74
+
75
+ def parse_response(xml)
76
+ result = Ox.load(xml, mode: :hash, with_cdata: true)
77
+
78
+ return if result[:Result][:Status] != '0'
79
+
80
+ statuses =
81
+ if result[:Result][:Content][:Item].is_a?(Hash)
82
+ [result[:Result][:Content][:Item][:Status].force_encoding('UTF-8')]
83
+ elsif result[:Result][:Content][:Item].is_a?(Array)
84
+ result[:Result][:Content][:Item].map do |item|
85
+ item[:Status].force_encoding('UTF-8')
86
+ end
87
+ end
88
+ parse_status_message(statuses.uniq)
89
+ end
90
+
91
+ def parse_status_message(statuses)
92
+ if '順利送達'.in?(statuses)
93
+ :done
94
+ elsif '配送中'.in?(statuses)
95
+ :delivering
96
+ elsif '已集貨'.in?(statuses)
97
+ :collected
98
+ end
99
+ end
100
+ end
101
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Tcat
4
+ VERSION = "0.1.1"
5
+ end
data/lib/tcat.rb ADDED
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'tcat/configuration'
4
+ require_relative 'tcat/version'
5
+ require_relative 'tcat/query'
6
+
7
+ module Tcat
8
+ class << self
9
+ attr_writer :configuration
10
+ end
11
+
12
+ def self.configuration
13
+ @configuration ||= Configuration.new
14
+ end
15
+
16
+ def self.configure
17
+ yield(configuration)
18
+ end
19
+
20
+ def self.reset
21
+ @configuration = Configuration.new
22
+ end
23
+
24
+ class Error < StandardError; end
25
+ # Your code goes here...
26
+ end
data/sig/tcat.rbs ADDED
@@ -0,0 +1,4 @@
1
+ module Tcat
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
data/tcat.gemspec ADDED
@@ -0,0 +1,42 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'lib/tcat/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'tcat'
7
+ spec.version = Tcat::VERSION
8
+ spec.authors = ['Zac']
9
+ spec.email = ['579103+7a6163@users.noreply.github.com']
10
+
11
+ spec.summary = 'A Ruby gem for tracking packages using the T-cat system.'
12
+ spec.description = 'TcatTracker provides an easy-to-use interface for querying package tracking information from the T-Cat system. It handles authentication and request formatting, allowing developers to easily integrate TCAT tracking into their Ruby applications.'
13
+ spec.homepage = 'https://github.com/7a6163/tcat'
14
+ spec.license = 'MIT'
15
+ spec.required_ruby_version = '>= 2.7.0'
16
+
17
+ # spec.metadata['allowed_push_host'] = 'https://rubygems.pkg.github.com'
18
+
19
+ spec.metadata['homepage_uri'] = spec.homepage
20
+ # spec.metadata['source_code_uri'] = "TODO: Put your gem's public repo URL here."
21
+ # spec.metadata['changelog_uri'] = "TODO: Put your gem's CHANGELOG.md URL here."
22
+
23
+ # Specify which files should be added to the gem when it is released.
24
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
25
+ spec.files = Dir.chdir(__dir__) do
26
+ `git ls-files -z`.split("\x0").reject do |f|
27
+ (File.expand_path(f) == __FILE__) ||
28
+ f.start_with?(*%w[bin/ test/ spec/ features/ .git .github appveyor Gemfile])
29
+ end
30
+ end
31
+ spec.bindir = 'exe'
32
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
33
+ spec.require_paths = ['lib']
34
+
35
+ # Uncomment to register a new dependency of your gem
36
+ # spec.add_dependency "example-gem", "~> 1.0"
37
+ spec.add_dependency 'ox', '~> 2.14'
38
+ spec.add_development_dependency 'pry', '~> 0.14.2'
39
+
40
+ # For more information and examples about making a new gem, check out our
41
+ # guide at: https://bundler.io/guides/creating_gem.html
42
+ end
metadata ADDED
@@ -0,0 +1,86 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tcat
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Zac
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2024-09-02 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: ox
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.14'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.14'
27
+ - !ruby/object:Gem::Dependency
28
+ name: pry
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 0.14.2
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 0.14.2
41
+ description: TcatTracker provides an easy-to-use interface for querying package tracking
42
+ information from the T-Cat system. It handles authentication and request formatting,
43
+ allowing developers to easily integrate TCAT tracking into their Ruby applications.
44
+ email:
45
+ - 579103+7a6163@users.noreply.github.com
46
+ executables: []
47
+ extensions: []
48
+ extra_rdoc_files: []
49
+ files:
50
+ - ".rspec"
51
+ - ".rubocop.yml"
52
+ - ".tool-versions"
53
+ - LICENSE.txt
54
+ - README.md
55
+ - Rakefile
56
+ - lib/tcat.rb
57
+ - lib/tcat/configuration.rb
58
+ - lib/tcat/query.rb
59
+ - lib/tcat/version.rb
60
+ - sig/tcat.rbs
61
+ - tcat.gemspec
62
+ homepage: https://github.com/7a6163/tcat
63
+ licenses:
64
+ - MIT
65
+ metadata:
66
+ homepage_uri: https://github.com/7a6163/tcat
67
+ post_install_message:
68
+ rdoc_options: []
69
+ require_paths:
70
+ - lib
71
+ required_ruby_version: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: 2.7.0
76
+ required_rubygems_version: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - ">="
79
+ - !ruby/object:Gem::Version
80
+ version: '0'
81
+ requirements: []
82
+ rubygems_version: 3.5.11
83
+ signing_key:
84
+ specification_version: 4
85
+ summary: A Ruby gem for tracking packages using the T-cat system.
86
+ test_files: []