uk-hs_code 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: f9119bf354ad934444ceb102a5b135e57ae29bb932dd04ae79fde3a4a162a12f
4
+ data.tar.gz: 524441f8f9ac14ee6fdd22e9a5784ec459afe32d552bdc7b9cb9901d91bdf74f
5
+ SHA512:
6
+ metadata.gz: 1d1d1a00af50905816c584315afa8f2797923dcfd5beff9b8dd99797598622b80c33ff4fc772a70c9093d831ce78092e81524d2fcb54686313c0bea682435030
7
+ data.tar.gz: 347e36b41777a46a239194cdf85a65404a4585dd6ab72967bf2d73ffc01616c7965530955f59df86f098a7c9c89e83127d8fba4f78fef8d69718c6b6762d2509
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,25 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.6
3
+
4
+ Style/StringLiterals:
5
+ Enabled: true
6
+ EnforcedStyle: double_quotes
7
+
8
+ Style/StringLiteralsInInterpolation:
9
+ Enabled: true
10
+ EnforcedStyle: double_quotes
11
+
12
+ Style/Documentation:
13
+ Enabled: false
14
+
15
+ Layout/LineLength:
16
+ Max: 120
17
+
18
+ Layout/LineLength:
19
+ Enabled: false
20
+
21
+ Metrics/AbcSize:
22
+ Enabled: false
23
+
24
+ Metrics/BlockLength:
25
+ Enabled: false
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in uk-hs_code.gemspec
6
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,63 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ uk-hs_code (0.1.0)
5
+ rake (~> 13.0)
6
+ rspec (~> 3.0)
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ ast (2.4.2)
12
+ coderay (1.1.3)
13
+ diff-lcs (1.5.0)
14
+ json (2.6.3)
15
+ method_source (1.0.0)
16
+ parallel (1.22.1)
17
+ parser (3.2.0.0)
18
+ ast (~> 2.4.1)
19
+ pry (0.14.2)
20
+ coderay (~> 1.1)
21
+ method_source (~> 1.0)
22
+ rainbow (3.1.1)
23
+ rake (13.0.6)
24
+ regexp_parser (2.7.0)
25
+ rexml (3.2.5)
26
+ rspec (3.12.0)
27
+ rspec-core (~> 3.12.0)
28
+ rspec-expectations (~> 3.12.0)
29
+ rspec-mocks (~> 3.12.0)
30
+ rspec-core (3.12.1)
31
+ rspec-support (~> 3.12.0)
32
+ rspec-expectations (3.12.2)
33
+ diff-lcs (>= 1.2.0, < 2.0)
34
+ rspec-support (~> 3.12.0)
35
+ rspec-mocks (3.12.3)
36
+ diff-lcs (>= 1.2.0, < 2.0)
37
+ rspec-support (~> 3.12.0)
38
+ rspec-support (3.12.0)
39
+ rubocop (1.45.1)
40
+ json (~> 2.3)
41
+ parallel (~> 1.10)
42
+ parser (>= 3.2.0.0)
43
+ rainbow (>= 2.2.2, < 4.0)
44
+ regexp_parser (>= 1.8, < 3.0)
45
+ rexml (>= 3.2.5, < 4.0)
46
+ rubocop-ast (>= 1.24.1, < 2.0)
47
+ ruby-progressbar (~> 1.7)
48
+ unicode-display_width (>= 2.4.0, < 3.0)
49
+ rubocop-ast (1.24.1)
50
+ parser (>= 3.1.1.0)
51
+ ruby-progressbar (1.11.0)
52
+ unicode-display_width (2.4.2)
53
+
54
+ PLATFORMS
55
+ x86_64-linux
56
+
57
+ DEPENDENCIES
58
+ pry
59
+ rubocop
60
+ uk-hs_code!
61
+
62
+ BUNDLED WITH
63
+ 2.4.5
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2023 Mateusz Michalski
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,55 @@
1
+ # Uk::HsCode
2
+
3
+ uk-hs_code enables to dynamically search Harmonized Codes via UK Trade Tariff API, https://api.trade-tariff.service.gov.uk/#gov-uk-trade-tariff-api.
4
+
5
+ ## Installation
6
+
7
+ The repository is here: https://github.com/enigmatt-pl/uk-hs_code.git
8
+
9
+ Add to your Gemfile:
10
+
11
+ $ gem 'uk-hs_code', git: 'https://github.com/enigmatt-pl/uk-hs_code.git'
12
+
13
+ And:
14
+
15
+ $ bundle install
16
+
17
+
18
+ ## Usage
19
+
20
+ ```ruby
21
+ # Get all HS codes matching 'table' reference
22
+
23
+ > refs = Uk::HsCode.search('table')
24
+
25
+ > refs.inspect
26
+
27
+ # => [#<Uk::HsCode::Record id="6133", type="search_reference", attributes=#<Uk::HsCode::Record title="table, billiard and bagatelle", referenced_id="9504", referenced_class="Heading", goods_nomenclature_item_id="9504000000", productline_suffix="80", goods_nomenclature_sid=54540>>,
28
+ #<Uk::HsCode::Record id="11204", type="search_reference", attributes=#<Uk::HsCode::Record title="table cloths, knitted or crocheted", referenced_id="6302", referenced_class="Heading", goods_nomenclature_item_id="6302000000", productline_suffix="80", goods_nomenclature_sid=43645>>,
29
+ #<Uk::HsCode::Record id="11205", type="search_reference", attributes=#<Uk::HsCode::Record title="tablecloths, knitted or crocheted", referenced_id="6302", referenced_class="Heading", goods_nomenclature_item_id="6302000000", productline_suffix="80", goods_nomenclature_sid=43645>>,
30
+ #<Uk::HsCode::Record id="7289", type="search_reference", attributes=#<Uk::HsCode::Record title="tablecloths, of paper", referenced_id="4818300000-80", referenced_class="Commodity", goods_nomenclature_item_id="4818300000", productline_suffix="80", goods_nomenclature_sid=40686>>,
31
+ #<Uk::HsCode::Record id="11206", type="search_reference", attributes=#<Uk::HsCode::Record title="table cloths, other than knitted or crocheted", referenced_id="6302510000-10", referenced_class="Subheading", goods_nomenclature_item_id="6302510000", productline_suffix="10", goods_nomenclature_sid=43679>>
32
+
33
+ # Get only HS codes with their titles matching 'carpet' reference
34
+
35
+ > refs = Uk::HsCode.search_hs_codes('carpet')
36
+
37
+ # => [#<Uk::HsCode::Record code="8451808000", title="carpet cleaning machines liquid injection">,
38
+ #<Uk::HsCode::Record code="4008000000", title="carpet underlay, rubber">,
39
+ #<Uk::HsCode::Record code="5402000000", title="carpet yarn, of man-made filaments">]
40
+
41
+ ```
42
+
43
+ ## Development
44
+
45
+ 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.
46
+
47
+ 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).
48
+
49
+ ## Contributing
50
+
51
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/uk-hs_code.
52
+
53
+ ## License
54
+
55
+ 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,51 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "net/http"
4
+ require "uri"
5
+ require "json"
6
+ # require "record"
7
+
8
+ module Uk
9
+ module HsCode
10
+ class ApiRequest
11
+ attr_reader :url, :query_params
12
+
13
+ def initialize(url, query_params = {})
14
+ @url = url
15
+ @query_params = query_params
16
+ end
17
+
18
+ def get
19
+ request = Net::HTTP::Get.new(parsed_uri)
20
+
21
+ add_headers(request)
22
+
23
+ http = Net::HTTP.new(parsed_uri.host, parsed_uri.port)
24
+ http.use_ssl = true if parsed_uri.scheme == "https"
25
+
26
+ response = http.request(request)
27
+
28
+ raise Uk::HsCode::Error, "Request failed with status: #{response.code} - #{response.message}" unless response.is_a?(Net::HTTPSuccess)
29
+
30
+ JSON.parse(response.body, object_class: Record)&.data
31
+ end
32
+
33
+ private
34
+
35
+ def add_headers(request)
36
+ request["User-Agent"] = "UK HS Code gem/#{Uk::HsCode::VERSION}"
37
+ request["Accept"] = "application/json"
38
+ end
39
+
40
+ def parsed_uri
41
+ @parsed_uri ||= URI.parse("#{url}?#{query_string}")
42
+ end
43
+
44
+ def query_string
45
+ query_params.map do |key, value|
46
+ "#{key}[#{URI.encode_www_form_component(value.keys.first)}]=#{URI.encode_www_form_component(value.values.first)}"
47
+ end.join("&")
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Uk
4
+ module HsCode
5
+ class Record < OpenStruct; end
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Uk
4
+ module HsCode
5
+ VERSION = "0.1.0"
6
+ end
7
+ end
data/lib/uk/hs_code.rb ADDED
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "hs_code/version"
4
+ require_relative "hs_code/api_request"
5
+ require_relative "hs_code/record"
6
+
7
+ module Uk
8
+ module HsCode
9
+ API_ENDPOINT = "https://www.trade-tariff.service.gov.uk/api"
10
+
11
+ class Error < StandardError; end
12
+
13
+ class << self
14
+ def search(query)
15
+ search_references(query)
16
+ end
17
+
18
+ def search_hs_codes(query)
19
+ result = search_references(query)
20
+ result.map { |a| Record.new(code: a.attributes.goods_nomenclature_item_id, title: a.attributes.title) }
21
+ end
22
+
23
+ private
24
+
25
+ def search_references(query)
26
+ ApiRequest.new("#{API_ENDPOINT}/v2/search_references.json", { query: { letter: query } }).get
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,6 @@
1
+ module Uk
2
+ module HsCode
3
+ VERSION: String
4
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
5
+ end
6
+ end
metadata ADDED
@@ -0,0 +1,114 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: uk-hs_code
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Mateusz Michalski
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2023-04-24 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: pry
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rubocop
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '13.0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '13.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.0'
69
+ description: Trade Tariff Public API V2 communicator.
70
+ email:
71
+ - enigmatt.priv@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".rspec"
77
+ - ".rubocop.yml"
78
+ - Gemfile
79
+ - Gemfile.lock
80
+ - LICENSE.txt
81
+ - README.md
82
+ - Rakefile
83
+ - lib/uk/hs_code.rb
84
+ - lib/uk/hs_code/api_request.rb
85
+ - lib/uk/hs_code/record.rb
86
+ - lib/uk/hs_code/version.rb
87
+ - sig/uk/hs_code.rbs
88
+ homepage: https://github.com/enigmatt-pl/uk-hs_code.git
89
+ licenses:
90
+ - MIT
91
+ metadata:
92
+ homepage_uri: https://github.com/enigmatt-pl/uk-hs_code.git
93
+ source_code_uri: https://github.com/enigmatt-pl/uk-hs_code.git
94
+ changelog_uri: https://github.com/enigmatt-pl/uk-hs_code.git
95
+ post_install_message:
96
+ rdoc_options: []
97
+ require_paths:
98
+ - lib
99
+ required_ruby_version: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: 2.6.0
104
+ required_rubygems_version: !ruby/object:Gem::Requirement
105
+ requirements:
106
+ - - ">="
107
+ - !ruby/object:Gem::Version
108
+ version: '0'
109
+ requirements: []
110
+ rubygems_version: 3.3.7
111
+ signing_key:
112
+ specification_version: 4
113
+ summary: Enables communication with Trade Tariff Public API V2.
114
+ test_files: []