dadata_metro 0.1.0

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: 59de36e8df08734716ef741758e426a06c40a6838e9d6846d2ee48701937ba1f
4
+ data.tar.gz: 7428e5ded12560a66903941001b383281055875c051fd73770b3e003e97a5e74
5
+ SHA512:
6
+ metadata.gz: 11ce2222006978468142db3ee7acd5a44d9559e49da1f7154f85d495779becdb614216e39eeeb994828acd564c9c100ecd9cfc2e0d9ce20adb6b7d0fc0ca754a
7
+ data.tar.gz: 420707af6792dfebacaf8837d8a9603ee4de6bead29f3c7ba24b17b32971d3ee1a129d75c97512e9d6d0256fb09d7d43f81760f4ecdc65c6c733d076906117d1
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,6 @@
1
+ ---
2
+ language: ruby
3
+ cache: bundler
4
+ rvm:
5
+ - 2.6.3
6
+ before_install: gem install bundler -v 2.1.4
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in dadata_metro.gemspec
4
+ gemspec
5
+
6
+ gem "rake", "~> 12.0"
7
+ gem "rspec", "~> 3.0"
8
+ gem 'webmock', '~> 3.13.0'
data/Gemfile.lock ADDED
@@ -0,0 +1,46 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ dadata_metro (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ addressable (2.8.0)
10
+ public_suffix (>= 2.0.2, < 5.0)
11
+ crack (0.4.5)
12
+ rexml
13
+ diff-lcs (1.4.4)
14
+ hashdiff (1.0.1)
15
+ public_suffix (4.0.6)
16
+ rake (12.3.3)
17
+ rexml (3.2.5)
18
+ rspec (3.10.0)
19
+ rspec-core (~> 3.10.0)
20
+ rspec-expectations (~> 3.10.0)
21
+ rspec-mocks (~> 3.10.0)
22
+ rspec-core (3.10.1)
23
+ rspec-support (~> 3.10.0)
24
+ rspec-expectations (3.10.1)
25
+ diff-lcs (>= 1.2.0, < 2.0)
26
+ rspec-support (~> 3.10.0)
27
+ rspec-mocks (3.10.2)
28
+ diff-lcs (>= 1.2.0, < 2.0)
29
+ rspec-support (~> 3.10.0)
30
+ rspec-support (3.10.3)
31
+ webmock (3.13.0)
32
+ addressable (>= 2.3.6)
33
+ crack (>= 0.3.2)
34
+ hashdiff (>= 0.4.0, < 2.0.0)
35
+
36
+ PLATFORMS
37
+ ruby
38
+
39
+ DEPENDENCIES
40
+ dadata_metro!
41
+ rake (~> 12.0)
42
+ rspec (~> 3.0)
43
+ webmock (~> 3.13.0)
44
+
45
+ BUNDLED WITH
46
+ 2.1.4
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2021 LSK99
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,33 @@
1
+ # Dadata
2
+
3
+ Wrapper for the [Dadata.ru](https://dadata.ru/) API: Metro stations
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'dadata_metro'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install dadata_metro
20
+
21
+ ## Usage
22
+
23
+ ```ruby
24
+ require 'dadata_metro'
25
+
26
+ dadata = DadataMetro::Search.new(api_token: 'token')
27
+ dadata.call(query: "Маяк", filters: [ { city_kladr_id: "7800000000000" } ])
28
+
29
+ ```
30
+
31
+ ## License
32
+
33
+ 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,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "dadata_metro"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,26 @@
1
+ require_relative 'lib/dadata_metro/version'
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = "dadata_metro"
5
+ spec.version = DadataMetro::VERSION
6
+ spec.authors = ["LSK99"]
7
+ spec.email = ["lobanova.sk99@gmail.com"]
8
+
9
+ spec.summary = "DaData Metro"
10
+ spec.description = "Wrapper for the Dadata API: Metro stations"
11
+ spec.homepage = "https://github.com/LSK99/dadata_metro"
12
+ spec.license = "MIT"
13
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
14
+
15
+ spec.metadata["homepage_uri"] = spec.homepage
16
+ spec.metadata["source_code_uri"] = "https://github.com/LSK99/dadata_metro"
17
+
18
+ # Specify which files should be added to the gem when it is released.
19
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
20
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
21
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
22
+ end
23
+ spec.bindir = "exe"
24
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
25
+ spec.require_paths = ["lib"]
26
+ end
@@ -0,0 +1,9 @@
1
+ module DadataMetro
2
+ class Constants
3
+ BASE_URL = 'https://suggestions.dadata.ru/suggestions/api/4_1/rs/suggest/metro'.freeze
4
+
5
+ FILTERS_FIELDS = %i[city_kladr_id city_fias_id city line_id is_closed].freeze
6
+
7
+ QUERY_MAX_LENGTH = 300
8
+ end
9
+ end
@@ -0,0 +1,62 @@
1
+ require "dadata_metro/constants"
2
+ require 'json'
3
+ require 'net/http'
4
+
5
+ module DadataMetro
6
+ class Search
7
+
8
+ attr_reader :api_token
9
+
10
+ def initialize(api_token:)
11
+ @api_token = api_token
12
+ end
13
+
14
+ def call(query:, filters: nil)
15
+ check_api_token
16
+ check_query(query)
17
+ check_filters(filters)
18
+
19
+ JSON.parse(request(query, filters))
20
+ end
21
+
22
+ private
23
+
24
+ def url
25
+ URI(Constants::BASE_URL)
26
+ end
27
+
28
+ def request(query, filters)
29
+ req = Net::HTTP::Post.new(url)
30
+
31
+ req['Content-Type'] = 'application/json'
32
+ req['Accept'] = 'application/json'
33
+ req['Authorization'] = "Token #{api_token}"
34
+
35
+ req.body = { query: query, filters: filters }.to_json
36
+
37
+ http = Net::HTTP.new(url.host, url.port)
38
+ http.use_ssl = true
39
+ http.request(req).body.force_encoding('UTF-8')
40
+ end
41
+
42
+ def check_query(query)
43
+ raise ArgumentError, 'query is required' unless query
44
+ raise ArgumentError, 'query is too long' if query.length > Constants::QUERY_MAX_LENGTH
45
+ end
46
+
47
+ def check_filters(filters)
48
+ return unless filters
49
+ raise ArgumentError, 'filters should be array' unless filters.is_a?(Array)
50
+ filters.each do |field|
51
+ pp field.keys.first
52
+ key = field.keys.first
53
+ next if Constants::FILTERS_FIELDS.include? key
54
+ raise ArgumentError, "filter field #{key} not supported"
55
+ end
56
+ end
57
+
58
+ def check_api_token
59
+ raise ArgumentError, 'api_token is required' unless api_token
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,3 @@
1
+ module DadataMetro
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,5 @@
1
+ require "dadata_metro/version"
2
+ require "dadata_metro/search"
3
+
4
+ module DadataMetro
5
+ end
metadata ADDED
@@ -0,0 +1,60 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dadata_metro
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - LSK99
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2021-12-13 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: 'Wrapper for the Dadata API: Metro stations'
14
+ email:
15
+ - lobanova.sk99@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - ".gitignore"
21
+ - ".rspec"
22
+ - ".travis.yml"
23
+ - Gemfile
24
+ - Gemfile.lock
25
+ - LICENSE.txt
26
+ - README.md
27
+ - Rakefile
28
+ - bin/console
29
+ - bin/setup
30
+ - dadata_metro.gemspec
31
+ - lib/dadata_metro.rb
32
+ - lib/dadata_metro/constants.rb
33
+ - lib/dadata_metro/search.rb
34
+ - lib/dadata_metro/version.rb
35
+ homepage: https://github.com/LSK99/dadata_metro
36
+ licenses:
37
+ - MIT
38
+ metadata:
39
+ homepage_uri: https://github.com/LSK99/dadata_metro
40
+ source_code_uri: https://github.com/LSK99/dadata_metro
41
+ post_install_message:
42
+ rdoc_options: []
43
+ require_paths:
44
+ - lib
45
+ required_ruby_version: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: 2.3.0
50
+ required_rubygems_version: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ requirements: []
56
+ rubygems_version: 3.2.7
57
+ signing_key:
58
+ specification_version: 4
59
+ summary: DaData Metro
60
+ test_files: []