radio-code-calculator 1.1.6

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: 2217228808ddf32aee295041252ab53f74c371ac513532f798fb8bf28ae8a289
4
+ data.tar.gz: a1a975556e51b3cf9500e1c608a73c926496cc7a7c9f748fe3066b01658d23aa
5
+ SHA512:
6
+ metadata.gz: 51b9e21a9041d37108b1a0d8ee18f6d9810220ee1701c68984a8ed584069ded37b821e7f49d643dc4399e5671bc0b8732f624ec4e8fb7f7d79b8dd5b4f168ce9
7
+ data.tar.gz: 73bcf1644e40dbc6160c925e750acd263f3e8a72087e8489ffc666f7394dea9eb16995112d26c16440ec1a8529b888af5b306aadf30d97e94945ed91acf362ef
data/LICENSE ADDED
@@ -0,0 +1,203 @@
1
+ Copyright 2026 Bartosz Wójcik
2
+
3
+ Apache License
4
+ Version 2.0, January 2004
5
+ http://www.apache.org/licenses/
6
+
7
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
8
+
9
+ 1. Definitions.
10
+
11
+ "License" shall mean the terms and conditions for use, reproduction,
12
+ and distribution as defined by Sections 1 through 9 of this document.
13
+
14
+ "Licensor" shall mean the copyright owner or entity authorized by
15
+ the copyright owner that is granting the License.
16
+
17
+ "Legal Entity" shall mean the union of the acting entity and all
18
+ other entities that control, are controlled by, or are under common
19
+ control with that entity. For the purposes of this definition,
20
+ "control" means (i) the power, direct or indirect, to cause the
21
+ direction or management of such entity, whether by contract or
22
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
23
+ outstanding shares, or (iii) beneficial ownership of such entity.
24
+
25
+ "You" (or "Your") shall mean an individual or Legal Entity
26
+ exercising permissions granted by this License.
27
+
28
+ "Source" form shall mean the preferred form for making modifications,
29
+ including but not limited to software source code, documentation
30
+ source, and configuration files.
31
+
32
+ "Object" form shall mean any form resulting from mechanical
33
+ transformation or translation of a Source form, including but
34
+ not limited to compiled object code, generated documentation,
35
+ and conversions to other media types.
36
+
37
+ "Work" shall mean the work of authorship, whether in Source or
38
+ Object form, made available under the License, as indicated by a
39
+ copyright notice that is included in or attached to the work
40
+ (an example is provided in the Appendix below).
41
+
42
+ "Derivative Works" shall mean any work, whether in Source or Object
43
+ form, that is based on (or derived from) the Work and for which the
44
+ editorial revisions, annotations, elaborations, or other modifications
45
+ represent, as a whole, an original work of authorship. For the purposes
46
+ of this License, Derivative Works shall not include works that remain
47
+ separable from, or merely link (or bind by name) to the interfaces of,
48
+ the Work and Derivative Works thereof.
49
+
50
+ "Contribution" shall mean any work of authorship, including
51
+ the original version of the Work and any modifications or additions
52
+ to that Work or Derivative Works thereof, that is intentionally
53
+ submitted to Licensor for inclusion in the Work by the copyright owner
54
+ or by an individual or Legal Entity authorized to submit on behalf of
55
+ the copyright owner. For the purposes of this definition, "submitted"
56
+ means any form of electronic, verbal, or written communication sent
57
+ to the Licensor or its representatives, including but not limited to
58
+ communication on electronic mailing lists, source code control systems,
59
+ and issue tracking systems that are managed by, or on behalf of, the
60
+ Licensor for the purpose of discussing and improving the Work, but
61
+ excluding communication that is conspicuously marked or otherwise
62
+ designated in writing by the copyright owner as "Not a Contribution."
63
+
64
+ "Contributor" shall mean Licensor and any individual or Legal Entity
65
+ on behalf of whom a Contribution has been received by Licensor and
66
+ subsequently incorporated within the Work.
67
+
68
+ 2. Grant of Copyright License. Subject to the terms and conditions of
69
+ this License, each Contributor hereby grants to You a perpetual,
70
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
71
+ copyright license to reproduce, prepare Derivative Works of,
72
+ publicly display, publicly perform, sublicense, and distribute the
73
+ Work and such Derivative Works in Source or Object form.
74
+
75
+ 3. Grant of Patent License. Subject to the terms and conditions of
76
+ this License, each Contributor hereby grants to You a perpetual,
77
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
78
+ (except as stated in this section) patent license to make, have made,
79
+ use, offer to sell, sell, import, and otherwise transfer the Work,
80
+ where such license applies only to those patent claims licensable
81
+ by such Contributor that are necessarily infringed by their
82
+ Contribution(s) alone or by combination of their Contribution(s)
83
+ with the Work to which such Contribution(s) was submitted. If You
84
+ institute patent litigation against any entity (including a
85
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
86
+ or a Contribution incorporated within the Work constitutes direct
87
+ or contributory patent infringement, then any patent licenses
88
+ granted to You under this License for that Work shall terminate
89
+ as of the date such litigation is filed.
90
+
91
+ 4. Redistribution. You may reproduce and distribute copies of the
92
+ Work or Derivative Works thereof in any medium, with or without
93
+ modifications, and in Source or Object form, provided that You
94
+ meet the following conditions:
95
+
96
+ (a) You must give any other recipients of the Work or
97
+ Derivative Works a copy of this License; and
98
+
99
+ (b) You must cause any modified files to carry prominent notices
100
+ stating that You changed the files; and
101
+
102
+ (c) You must retain, in the Source form of any Derivative Works
103
+ that You distribute, all copyright, patent, trademark, and
104
+ attribution notices from the Source form of the Work,
105
+ excluding those notices that do not pertain to any part of
106
+ the Derivative Works; and
107
+
108
+ (d) If the Work includes a "NOTICE" text file as part of its
109
+ distribution, then any Derivative Works that You distribute must
110
+ include a readable copy of the attribution notices contained
111
+ within such NOTICE file, excluding those notices that do not
112
+ pertain to any part of the Derivative Works, in at least one
113
+ of the following places: within a NOTICE text file distributed
114
+ as part of the Derivative Works; within the Source form or
115
+ documentation, if provided along with the Derivative Works; or,
116
+ within a display generated by the Derivative Works, if and
117
+ wherever such third-party notices normally appear. The contents
118
+ of the NOTICE file are for informational purposes only and
119
+ do not modify the License. You may add Your own attribution
120
+ notices within Derivative Works that You distribute, alongside
121
+ or as an addendum to the NOTICE text from the Work, provided
122
+ that such additional attribution notices cannot be construed
123
+ as modifying the License.
124
+
125
+ You may add Your own copyright statement to Your modifications and
126
+ may provide additional or different license terms and conditions
127
+ for use, reproduction, or distribution of Your modifications, or
128
+ for any such Derivative Works as a whole, provided Your use,
129
+ reproduction, and distribution of the Work otherwise complies with
130
+ the conditions stated in this License.
131
+
132
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
133
+ any Contribution intentionally submitted for inclusion in the Work
134
+ by You to the Licensor shall be under the terms and conditions of
135
+ this License, without any additional terms or conditions.
136
+ Notwithstanding the above, nothing herein shall supersede or modify
137
+ the terms of any separate license agreement you may have executed
138
+ with Licensor regarding such Contributions.
139
+
140
+ 6. Trademarks. This License does not grant permission to use the trade
141
+ names, trademarks, service marks, or product names of the Licensor,
142
+ except as required for reasonable and customary use in describing the
143
+ origin of the Work and reproducing the content of the NOTICE file.
144
+
145
+ 7. Disclaimer of Warranty. Unless required by applicable law or
146
+ agreed to in writing, Licensor provides the Work (and each
147
+ Contributor provides its Contributions) on an "AS IS" BASIS,
148
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
149
+ implied, including, without limitation, any warranties or conditions
150
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
151
+ PARTICULAR PURPOSE. You are solely responsible for determining the
152
+ appropriateness of using or redistributing the Work and assume any
153
+ risks associated with Your exercise of permissions under this License.
154
+
155
+ 8. Limitation of Liability. In no event and under no legal theory,
156
+ whether in tort (including negligence), contract, or otherwise,
157
+ unless required by applicable law (such as deliberate and grossly
158
+ negligent acts) or agreed to in writing, shall any Contributor be
159
+ liable to You for damages, including any direct, indirect, special,
160
+ incidental, or consequential damages of any character arising as a
161
+ result of this License or out of the use or inability to use the
162
+ Work (including but not limited to damages for loss of goodwill,
163
+ work stoppage, computer failure or malfunction, or any and all
164
+ other commercial damages or losses), even if such Contributor
165
+ has been advised of the possibility of such damages.
166
+
167
+ 9. Accepting Warranty or Additional Liability. While redistributing
168
+ the Work or Derivative Works thereof, You may choose to offer,
169
+ and charge a fee for, acceptance of support, warranty, indemnity,
170
+ or other liability obligations and/or rights consistent with this
171
+ License. However, in accepting such obligations, You may act only
172
+ on Your own behalf and on Your sole responsibility, not on behalf
173
+ of any other Contributor, and only if You agree to indemnify,
174
+ defend, and hold each Contributor harmless for any liability
175
+ incurred by, or claims asserted against, such Contributor by reason
176
+ of your accepting any such warranty or additional liability.
177
+
178
+ END OF TERMS AND CONDITIONS
179
+
180
+ APPENDIX: How to apply the Apache License to your work.
181
+
182
+ To apply the Apache License to your work, attach the following
183
+ boilerplate notice, with the fields enclosed by brackets "[]"
184
+ replaced with your own identifying information. (Don't include
185
+ the brackets!) The text should be enclosed in the appropriate
186
+ comment syntax for the file format. We also recommend that a
187
+ file or class name and description of purpose be included on the
188
+ same "printed page" as the copyright notice for easier
189
+ identification within third-party archives.
190
+
191
+ Copyright [yyyy] [name of copyright owner]
192
+
193
+ Licensed under the Apache License, Version 2.0 (the "License");
194
+ you may not use this file except in compliance with the License.
195
+ You may obtain a copy of the License at
196
+
197
+ http://www.apache.org/licenses/LICENSE-2.0
198
+
199
+ Unless required by applicable law or agreed to in writing, software
200
+ distributed under the License is distributed on an "AS IS" BASIS,
201
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
202
+ See the License for the specific language governing permissions and
203
+ limitations under the License.
data/README.md ADDED
@@ -0,0 +1,207 @@
1
+ # Radio Code Calculator Online & SDK for Ruby
2
+
3
+ **[Radio Code Calculator](https://www.pelock.com/products/radio-code-calculator)** is an online service along with [Web API & SDK](https://www.pelock.com/products/radio-code-calculator/sdk) for generating car radio unlock codes for popular vehicle brands.
4
+
5
+ Following a breakdown or a disconnection of the car battery, most of the vehicle radio & navigation units will ask for an unlocking code. It's standard anti-theft protection.
6
+
7
+ Our car Radio Code Calculator allows you to generate **100% valid** radio codes to unlock car radios & navigation without the need to use the expensive service of authorized dealers.
8
+
9
+ ![Radio Code Calculator](https://www.pelock.com/img/en/products/radio-code-calculator/car-radio-code-calculator-online-web-api-sdk.jpg)
10
+
11
+ The service is available through a simple online interface and `Web API`, with multiple `SDK` development libraries for popular programming languages.
12
+
13
+ Thanks to our solution, you can create, for instance, mobile or web applications that allow for easy generation of radio codes.
14
+
15
+ ## Supported car models and radios
16
+
17
+ Our service is being continuously developed and new algorithms are gradually added for new car models and their radios.
18
+
19
+ If a new algorithm is added, you will get automatic and free access to it as part of your current license.
20
+
21
+ Individual calculators are available on our site as a paid service for end customers. You can verify them and see lists of supported radios on the relevant subpages:
22
+
23
+ * [Renault & Dacia](https://www.pelock.com/products/renault-and-dacia-car-radio-code-calculator-generator)
24
+ * [Toyota ERC](https://www.pelock.com/products/toyota-erc-calculator-radio-unlock-code-generator)
25
+ * [Jeep Cherokee](https://www.pelock.com/products/jeep-cherokee-radio-unlock-code-calculator-generator)
26
+ * [Ford M Serial](https://www.pelock.com/products/ford-radio-code-m-serial-calculator-generator)
27
+ * [Ford V Serial](https://www.pelock.com/products/ford-radio-code-v-serial-calculator-generator)
28
+ * [Ford TravelPilot EX, FX & NX](https://www.pelock.com/products/ford-travelpilot-ex-fx-nx-radio-code-generator-calculator)
29
+ * [Chrysler Panasonic TM9](https://www.pelock.com/products/chrysler-panasonic-tm9-car-radio-code-calculator-generator)
30
+ * [Chrysler Dodge Ram VP2](https://www.pelock.com/products/chrysler-dodge-ram-uconnect-harman-kardon-radio-code)
31
+ * [Fiat Stilo & Bravo Visteon](https://www.pelock.com/products/fiat-stilo-bravo-visteon-radio-code-calculator-generator)
32
+ * [Fiat DAIICHI MOPAR](https://www.pelock.com/products/fiat-daiichi-radio-code-calculator-generator)
33
+ * [Fiat Continental 250 & 500 VP1/VP2](https://www.pelock.com/products/fiat-250-500-vp1-vp2-radio-code-calculator-generator)
34
+ * [Nissan Glove Box Immobiliser PIN](https://www.pelock.com/products/nissan-glove-box-pin-code-calculator)
35
+ * [Eclipse ESN Unlock Code Calculator](https://www.pelock.com/products/eclipse-esn-unlock-code-calculator)
36
+ * [Jaguar Alpine](https://www.pelock.com/products/jaguar-alpine-car-radio-unlock-code-calculator)
37
+
38
+ ## Use of radio code calculator
39
+
40
+ Where and who can use the radio code generation service and make money from code generation?
41
+
42
+ ### ![Android](https://www.pelock.com/img/en/icons/android-32.png) App developers
43
+ The main audience for our software is clearly developers and programmers, either of mobile or desktop applications.
44
+
45
+ ### ![Shoping cart](https://www.pelock.com/img/en/icons/cart-32.png) Online stores
46
+ If you run an online e-commerce store, you can sell radio codes through it using our software solutions.
47
+
48
+ ### ![Car](https://www.pelock.com/img/en/icons/car-32.png) Auto repair shops
49
+ We also encourage car repair shops whose customers often use car radio unlocking services.
50
+
51
+ ### ![Person](https://www.pelock.com/img/en/icons/user-32.png) Private individuals
52
+ Private individuals will also profit from our solution by generating codes and selling them on car forums or auction sites such as eBay, Craigslist.
53
+
54
+ ### No limits!
55
+
56
+ You can generate codes **without limitation** with your purchased one year license.
57
+
58
+ Set your own price for generating a single code and start earning by using **tried and tested** algorithms from a programming language you know.
59
+
60
+ If you are not a programmer - don't worry. Just use our [online calculator](https://www.pelock.com/products/radio-code-calculator/online).
61
+
62
+ ## Installation
63
+
64
+ The preferred way to install the Web API SDK is via RubyGems.
65
+
66
+ ```bash
67
+ gem install radio-code-calculator
68
+ ```
69
+
70
+ ```ruby
71
+ require "radio-code-calculator"
72
+ ```
73
+
74
+ The gem is published at https://rubygems.org/gems/radio-code-calculator
75
+
76
+ ## Packages for other programming languages
77
+
78
+ The installation packages have been uploaded to repositories for several popular programming languages and their source codes have been published on GitHub:
79
+
80
+ | Repository | Language | Installation | Package | GitHub |
81
+ | ------------ | ---------| ------------ | ------- | ------ |
82
+ | ![Packagist repository for PHP and Composer](https://www.pelock.com/img/logos/repo-packagist-composer.png) | PHP | Add the following line to `require` section of your `composer.json` file `"pelock/radio-code-calculator": "*"` | [Packagist](https://packagist.org/packages/pelock/radio-code-calculator) | [Sources](https://github.com/PELock/Radio-Code-Calculator-PHP)
83
+ | ![PyPI repository for Python](https://www.pelock.com/img/logos/repo-pypi.png) | Python | Run `pip install radio-code-calculator` | [PyPi](https://pypi.org/project/radio-code-calculator/) | [Sources](https://github.com/PELock/Radio-Code-Calculator-Python)
84
+ | ![NPM repository for JavaScript and TypeScript](https://www.pelock.com/img/logos/repo-npm.png) | JavaScript, TypeScript | Run `npm i radio-code-calculator` or add the following to `dependencies` section of your `package.json` file `"dependencies": { "radio-code-calculator": "latest" },` | [NPM](https://www.npmjs.com/package/radio-code-calculator) | [Sources](https://github.com/PELock/Radio-Code-Calculator-JavaScript)
85
+
86
+ ## Usage examples
87
+
88
+ ### Example — `list.rb`
89
+
90
+ ```ruby
91
+ # frozen_string_literal: true
92
+
93
+ ###############################################################################
94
+ #
95
+ # Radio Code Calculator — list models
96
+ #
97
+ # Version : v1.1.6
98
+ # Language : Ruby
99
+ # Author : Bartosz Wójcik
100
+ # Web page : https://www.pelock.com
101
+ #
102
+ ###############################################################################
103
+
104
+ $LOAD_PATH.unshift(File.expand_path("../lib", __dir__))
105
+ require "radio-code-calculator"
106
+
107
+ error, models = RadioCodeCalculator.new("YOUR-WEB-API-KEY").list
108
+ if error == RadioErrors::SUCCESS
109
+ models.each { |m| puts "#{m.name} serial=#{m.serial_max_len} #{m.serial_regex_pattern}" }
110
+ else
111
+ warn "Error: #{error}"
112
+ end
113
+ ```
114
+
115
+ ### Example — `login.rb`
116
+
117
+ ```ruby
118
+ # frozen_string_literal: true
119
+
120
+ ###############################################################################
121
+ #
122
+ # Radio Code Calculator — login
123
+ #
124
+ # Version : v1.1.6
125
+ # Language : Ruby
126
+ # Author : Bartosz Wójcik
127
+ # Web page : https://www.pelock.com
128
+ #
129
+ ###############################################################################
130
+
131
+ $LOAD_PATH.unshift(File.expand_path("../lib", __dir__))
132
+ require "radio-code-calculator"
133
+
134
+ error, result = RadioCodeCalculator.new("YOUR-WEB-API-KEY").login
135
+ p [error, result]
136
+ ```
137
+
138
+ ### Example — `offline_validation.rb`
139
+
140
+ ```ruby
141
+ # frozen_string_literal: true
142
+
143
+ ###############################################################################
144
+ #
145
+ # Radio Code Calculator — offline validation
146
+ #
147
+ # Version : v1.1.6
148
+ # Language : Ruby
149
+ # Author : Bartosz Wójcik
150
+ # Web page : https://www.pelock.com
151
+ #
152
+ ###############################################################################
153
+
154
+ $LOAD_PATH.unshift(File.expand_path("../lib", __dir__))
155
+ require "radio-code-calculator"
156
+
157
+ model = RadioModels.get(RadioModels::FORD_M_SERIES)
158
+ puts model.validate("123456") # 0 = SUCCESS
159
+ puts model.validate("1") # INVALID_SERIAL_LENGTH
160
+ puts model.validate("12345A") # INVALID_SERIAL_PATTERN
161
+ ```
162
+
163
+ ### Example — `simple.rb`
164
+
165
+ ```ruby
166
+ # frozen_string_literal: true
167
+
168
+ ###############################################################################
169
+ #
170
+ # Radio Code Calculator — generate a code
171
+ #
172
+ # Version : v1.1.6
173
+ # Language : Ruby
174
+ # Author : Bartosz Wójcik
175
+ # Web page : https://www.pelock.com
176
+ #
177
+ ###############################################################################
178
+
179
+ $LOAD_PATH.unshift(File.expand_path("../lib", __dir__))
180
+ require "radio-code-calculator"
181
+
182
+ client = RadioCodeCalculator.new("YOUR-WEB-API-KEY")
183
+ error, result = client.calc(RadioModels.get(RadioModels::FORD_M_SERIES), "123456")
184
+
185
+ case error
186
+ when RadioErrors::SUCCESS
187
+ puts "Radio code is #{result["code"]}"
188
+ when RadioErrors::INVALID_RADIO_MODEL
189
+ puts "Invalid radio model (not supported)"
190
+ when RadioErrors::INVALID_LICENSE
191
+ puts "Invalid license key!"
192
+ else
193
+ puts "Error: #{error}"
194
+ end
195
+ ```
196
+
197
+ See the `examples/` directory in this repository for complete samples.
198
+
199
+ ## Got questions?
200
+
201
+ If you are interested in the Radio Code Calculator Web API or have any questions regarding radio code generator SDK packages, technical or legal issues, or if something is not clear, [please contact me](https://www.pelock.com/contact). I'll be happy to answer all of your questions.
202
+
203
+ Bartosz Wójcik
204
+
205
+ * Visit my site at — https://www.pelock.com
206
+ * Twitter — https://twitter.com/PELock
207
+ * GitHub — https://github.com/PELock
@@ -0,0 +1,80 @@
1
+ # frozen_string_literal: true
2
+
3
+ class RadioCodeCalculator
4
+ API_URL = "https://www.pelock.com/api/radio-code-calculator/v1"
5
+ USER_AGENT = "PELock Radio Code Calculator"
6
+
7
+ attr_accessor :api_key
8
+
9
+ def initialize(api_key = nil)
10
+ @api_key = api_key
11
+ end
12
+
13
+ def login
14
+ result = post_request("command" => "login")
15
+ [result["error"], result]
16
+ end
17
+
18
+ def calc(radio_model, radio_serial_number, radio_extra_data = "")
19
+ model_name = self.class.model_name(radio_model)
20
+ result = post_request(
21
+ "command" => "calc",
22
+ "radio_model" => model_name,
23
+ "serial" => radio_serial_number,
24
+ "extra" => radio_extra_data
25
+ )
26
+ [result["error"], result]
27
+ end
28
+
29
+ def info(radio_model)
30
+ model_name = self.class.model_name(radio_model)
31
+ result = post_request("command" => "info", "radio_model" => model_name)
32
+ return [result["error"], nil] unless result["error"] == RadioErrors::SUCCESS
33
+
34
+ model = RadioModel.new(
35
+ model_name,
36
+ result["serialMaxLen"],
37
+ result["serialRegexPattern"],
38
+ result["extraMaxLen"],
39
+ result["extraRegexPattern"]
40
+ )
41
+ [result["error"], model]
42
+ end
43
+
44
+ def list
45
+ result = post_request("command" => "list")
46
+ return [result["error"], nil] unless result["error"] == RadioErrors::SUCCESS
47
+
48
+ radio_models = []
49
+ (result["supportedRadioModels"] || {}).each do |radio_model_name, radio_model|
50
+ radio_models << RadioModel.new(
51
+ radio_model_name,
52
+ radio_model["serialMaxLen"],
53
+ radio_model["serialRegexPattern"],
54
+ radio_model["extraMaxLen"],
55
+ radio_model["extraRegexPattern"]
56
+ )
57
+ end
58
+ [result["error"], radio_models]
59
+ end
60
+
61
+ def post_request(params_array)
62
+ params = params_array.dup
63
+ params["key"] = @api_key unless @api_key.nil? || @api_key.to_s.empty?
64
+ default_error = { "error" => RadioErrors::ERROR_CONNECTION }
65
+
66
+ body = Http.post_multipart(API_URL, params, user_agent: USER_AGENT)
67
+ return default_error if body.nil? || body.empty?
68
+
69
+ result = Http.parse_json(body)
70
+ result.is_a?(Hash) && !result.empty? ? result : default_error
71
+ end
72
+
73
+ def self.model_name(radio_model)
74
+ case radio_model
75
+ when RadioModel then radio_model.name
76
+ when Array then radio_model[0].to_s
77
+ else radio_model.to_s
78
+ end
79
+ end
80
+ end
@@ -0,0 +1,85 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "json"
4
+ require "net/http"
5
+ require "openssl"
6
+ require "securerandom"
7
+ require "uri"
8
+
9
+ class RadioCodeCalculator
10
+ # Stdlib HTTP helper. Multipart for API form posts / file uploads; urlencoded when needed.
11
+ module Http
12
+ class << self
13
+ def post_multipart(url, fields, user_agent:, files: {})
14
+ boundary = "----PELock#{SecureRandom.hex(16)}"
15
+ body = build_multipart(fields, files, boundary)
16
+ request(url, body, user_agent, "multipart/form-data; boundary=#{boundary}")
17
+ end
18
+
19
+ def post_urlencoded(url, fields, user_agent:)
20
+ pairs = []
21
+ fields.each do |key, value|
22
+ next if value.nil?
23
+
24
+ pairs << [key.to_s, normalize_form_value(value)]
25
+ end
26
+ request(url, URI.encode_www_form(pairs), user_agent, "application/x-www-form-urlencoded")
27
+ end
28
+
29
+ def parse_json(body)
30
+ return nil if body.nil? || body.to_s.empty?
31
+
32
+ JSON.parse(body)
33
+ rescue JSON::ParserError
34
+ nil
35
+ end
36
+
37
+ def request(url, body, user_agent, content_type)
38
+ uri = URI.parse(url)
39
+ http = Net::HTTP.new(uri.host, uri.port)
40
+ http.use_ssl = uri.scheme == "https"
41
+ http.open_timeout = 30
42
+ http.read_timeout = 180
43
+ req = Net::HTTP::Post.new(uri.request_uri)
44
+ req["User-Agent"] = user_agent
45
+ req["Content-Type"] = content_type
46
+ req.body = body
47
+ http.request(req).body
48
+ rescue StandardError
49
+ nil
50
+ end
51
+
52
+ def build_multipart(fields, files, boundary)
53
+ body = String.new(encoding: Encoding::ASCII_8BIT)
54
+ fields.each do |name, value|
55
+ next if value.nil?
56
+
57
+ body << "--#{boundary}\r\n"
58
+ body << "Content-Disposition: form-data; name=\"#{name}\"\r\n\r\n"
59
+ body << value.to_s.dup.force_encoding(Encoding::UTF_8).b
60
+ body << "\r\n"
61
+ end
62
+ files.each do |name, file|
63
+ filename = file[:filename] || File.basename(file[:path].to_s)
64
+ content = file[:content] || File.binread(file[:path])
65
+ content_type = file[:content_type] || "application/octet-stream"
66
+ body << "--#{boundary}\r\n"
67
+ body << "Content-Disposition: form-data; name=\"#{name}\"; filename=\"#{filename}\"\r\n"
68
+ body << "Content-Type: #{content_type}\r\n\r\n"
69
+ body << content.b
70
+ body << "\r\n"
71
+ end
72
+ body << "--#{boundary}--\r\n"
73
+ body
74
+ end
75
+
76
+ def normalize_form_value(value)
77
+ case value
78
+ when true then "1"
79
+ when false then "0"
80
+ else value.to_s
81
+ end
82
+ end
83
+ end
84
+ end
85
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ class RadioErrors
4
+ ERROR_CONNECTION = -1
5
+ SUCCESS = 0
6
+ INVALID_INPUT = 1
7
+ INVALID_COMMAND = 2
8
+ INVALID_RADIO_MODEL = 3
9
+ INVALID_SERIAL_LENGTH = 4
10
+ INVALID_SERIAL_PATTERN = 5
11
+ INVALID_SERIAL_NOT_SUPPORTED = 6
12
+ INVALID_EXTRA_LENGTH = 7
13
+ INVALID_EXTRA_PATTERN = 8
14
+ INVALID_LICENSE = 100
15
+ end
@@ -0,0 +1,77 @@
1
+ # frozen_string_literal: true
2
+
3
+ class RadioModel
4
+ attr_accessor :name, :serial_max_len, :extra_max_len, :default_programming_language
5
+ attr_reader :_serial_regex_patterns, :_extra_regex_patterns
6
+
7
+ def initialize(name, serial_max_len, serial_regex_pattern, extra_max_len = 0, extra_regex_pattern = nil)
8
+ @name = name
9
+ @serial_max_len = serial_max_len
10
+ @default_programming_language = "php"
11
+ @_serial_regex_patterns = {}
12
+
13
+ if serial_regex_pattern.is_a?(String)
14
+ @_serial_regex_patterns[@default_programming_language] = serial_regex_pattern
15
+ elsif serial_regex_pattern.is_a?(Hash)
16
+ @_serial_regex_patterns = serial_regex_pattern
17
+ end
18
+
19
+ @extra_max_len = extra_max_len
20
+ @_extra_regex_patterns = nil
21
+ return if extra_max_len.zero?
22
+
23
+ if extra_regex_pattern.is_a?(String)
24
+ @_extra_regex_patterns = { @default_programming_language => extra_regex_pattern }
25
+ elsif extra_regex_pattern.is_a?(Hash)
26
+ @_extra_regex_patterns = extra_regex_pattern
27
+ end
28
+ end
29
+
30
+ def serial_regex_pattern
31
+ @_serial_regex_patterns[@default_programming_language] || ""
32
+ end
33
+
34
+ def extra_regex_pattern
35
+ return nil if @_extra_regex_patterns.nil?
36
+
37
+ @_extra_regex_patterns[@default_programming_language]
38
+ end
39
+
40
+ def validate(serial, extra = nil)
41
+ return RadioErrors::INVALID_SERIAL_LENGTH if serial.length != @serial_max_len
42
+ return RadioErrors::INVALID_SERIAL_PATTERN unless match?(serial_regex_pattern, serial)
43
+
44
+ if extra && !extra.empty?
45
+ return RadioErrors::INVALID_EXTRA_LENGTH if extra.length != @extra_max_len
46
+ return RadioErrors::INVALID_EXTRA_PATTERN unless match?(extra_regex_pattern, extra)
47
+ end
48
+
49
+ RadioErrors::SUCCESS
50
+ end
51
+
52
+ private
53
+
54
+ def match?(php_regex, value)
55
+ return false if php_regex.nil? || php_regex.to_s.empty?
56
+
57
+ re = self.class.php_regex_to_ruby(php_regex)
58
+ return false if re.nil?
59
+
60
+ !re.match(value).nil?
61
+ end
62
+
63
+ def self.php_regex_to_ruby(pattern)
64
+ return nil if pattern.nil? || pattern.empty?
65
+ return Regexp.new(pattern) unless pattern.start_with?("/")
66
+
67
+ last = pattern.rindex("/")
68
+ return Regexp.new(pattern) if last.nil? || last.zero?
69
+
70
+ body = pattern[1...last]
71
+ flags = pattern[(last + 1)..] || ""
72
+ opts = 0
73
+ opts |= Regexp::IGNORECASE if flags.include?("i")
74
+ opts |= Regexp::MULTILINE if flags.include?("m")
75
+ Regexp.new(body, opts)
76
+ end
77
+ end
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Built-in radio models with offline validation metadata.
4
+ # ECLIPSE_ESN uses API name "eclipse-esn" (PHP SDK currently lists toyota-erc by mistake).
5
+ class RadioModels
6
+ RENAULT_DACIA = ["renault-dacia", 4, "/^([A-Z]{1}[0-9]{3})$/"].freeze
7
+ CHRYSLER_PANASONIC_TM9 = ["chrysler-panasonic-tm9", 4, "/^([0-9]{4})$/"].freeze
8
+ CHRYSLER_DODGE_VP = ["chrysler-dodge-vp", 4, "/^([a-zA-Z0-9]{4})$/"].freeze
9
+ FORD_M_SERIES = ["ford-m-series", 6, "/^([0-9]{6})$/"].freeze
10
+ FORD_V_SERIES = ["ford-v-series", 6, "/^([0-9]{6})$/"].freeze
11
+ FORD_TRAVELPILOT = ["ford-travelpilot", 7, "/^([0-9]{7})$/"].freeze
12
+ FIAT_STILO_BRAVO_VISTEON = ["fiat-stilo-bravo-visteon", 6, "/^([a-zA-Z0-9]{6})$/"].freeze
13
+ FIAT_DAIICHI = ["fiat-daiichi", 4, "/^([0-9]{4})$/"].freeze
14
+ FIAT_VP = ["fiat-vp", 4, "/^([0-9]{4})$/"].freeze
15
+ TOYOTA_ERC = ["toyota-erc", 16, "/^([a-zA-Z0-9]{16})$/"].freeze
16
+ JEEP_CHEROKEE = ["jeep-cherokee", 14, "/^([a-zA-Z0-9]{10}[0-9]{4})$/"].freeze
17
+ NISSAN_GLOVE_BOX = ["nissan-glove-box", 12, "/^([a-zA-Z0-9]{12})$/"].freeze
18
+ ECLIPSE_ESN = ["eclipse-esn", 6, "/^([a-zA-Z0-9]{6})$/"].freeze
19
+ JAGUAR_ALPINE = ["jaguar-alpine", 5, "/^([0-9]{5})$/"].freeze
20
+
21
+ def self.get(radio_model_params)
22
+ return radio_model_params if radio_model_params.is_a?(RadioModel)
23
+
24
+ RadioModel.new(
25
+ radio_model_params[0],
26
+ radio_model_params[1],
27
+ radio_model_params[2],
28
+ radio_model_params[3] || 0,
29
+ radio_model_params[4] || ""
30
+ )
31
+ end
32
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ class RadioCodeCalculator
4
+ VERSION = "1.1.6"
5
+ end
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "radio-code-calculator/version"
4
+ require_relative "radio-code-calculator/http"
5
+ require_relative "radio-code-calculator/radio_errors"
6
+ require_relative "radio-code-calculator/radio_model"
7
+ require_relative "radio-code-calculator/radio_models"
8
+ require_relative "radio-code-calculator/client"
metadata ADDED
@@ -0,0 +1,51 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: radio-code-calculator
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.1.6
5
+ platform: ruby
6
+ authors:
7
+ - Bartosz Wójcik
8
+ bindir: bin
9
+ cert_chain: []
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
+ dependencies: []
12
+ email:
13
+ - support@pelock.com
14
+ executables: []
15
+ extensions: []
16
+ extra_rdoc_files: []
17
+ files:
18
+ - LICENSE
19
+ - README.md
20
+ - lib/radio-code-calculator.rb
21
+ - lib/radio-code-calculator/client.rb
22
+ - lib/radio-code-calculator/http.rb
23
+ - lib/radio-code-calculator/radio_errors.rb
24
+ - lib/radio-code-calculator/radio_model.rb
25
+ - lib/radio-code-calculator/radio_models.rb
26
+ - lib/radio-code-calculator/version.rb
27
+ homepage: https://www.pelock.com/products/radio-code-calculator
28
+ licenses:
29
+ - Apache-2.0
30
+ metadata:
31
+ homepage_uri: https://www.pelock.com/products/radio-code-calculator
32
+ source_code_uri: https://github.com/PELock/Radio-Code-Calculator-Ruby
33
+ bug_tracker_uri: https://www.pelock.com
34
+ rdoc_options: []
35
+ require_paths:
36
+ - lib
37
+ required_ruby_version: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: 2.7.0
42
+ required_rubygems_version: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ requirements: []
48
+ rubygems_version: 4.0.20
49
+ specification_version: 4
50
+ summary: Radio Code Calculator Web API Ruby SDK
51
+ test_files: []