ua_town_finder 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
+ SHA1:
3
+ metadata.gz: ed1e1f9bd19af4629f1927131058b29ecda1a926
4
+ data.tar.gz: f8b3838c419a94e5242c3bef3e0026fb10ca649e
5
+ SHA512:
6
+ metadata.gz: 54482e0cd21c06a5ba13ebf78a934a429b755cda75f5416e2e8005f31ea4c248f8e7fcf7601e5bfa8d6ccdfa1c6e01cad3bfd18197ffef1396b6f45e049d28c0
7
+ data.tar.gz: 419bb85c262fac59bb6a68f57b68ec1c0cdc7b661a83823b81f9a92cfb07b3672595021643384fccea1b6b656cb68b413bcc1c2d575b35dd4168b29c4fdf8af4
data/.gitignore ADDED
@@ -0,0 +1,8 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in ua_town_finder.gemspec
6
+ gemspec
data/README.md ADDED
@@ -0,0 +1,35 @@
1
+ # UaTownFinder
2
+
3
+ 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/ua_town_finder`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'ua_town_finder'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install ua_town_finder
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ 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 tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/ua_town_finder.
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "ua_town_finder"
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,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Adapters
4
+ class Area
5
+ def initialize(areas_hash)
6
+ @areas = areas_hash.dig('data', 'areas_by_title')
7
+ end
8
+
9
+ def call
10
+ @areas.nil? ? [] : parse_areas
11
+ end
12
+
13
+ private
14
+
15
+ def parse_areas
16
+ @areas.map do |area_hash|
17
+ {
18
+ title: area_hash['full_name'],
19
+ id: area_hash['mongo_id']
20
+ }
21
+ end
22
+ end
23
+ end
24
+ end
data/lib/api/area.rb ADDED
@@ -0,0 +1,67 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'api/area_data'
4
+ module Api
5
+ class Area
6
+ extend Api::AreaData
7
+ class << self
8
+ def by_rest_api(path, version = :v1)
9
+ area_data("https://map-ms.herokuapp.com/api/#{version}/#{path}")
10
+ end
11
+
12
+ def areas(*fields)
13
+ query_fields = default_fields + fields.join(',')
14
+ filter_areas(area_json("{areas{#{query_fields}}}").dig('data', 'areas'))
15
+ end
16
+
17
+ def full_areas(*fields)
18
+ query_fields = default_fields + fields.join(',')
19
+ filter_areas area_json("{full_areas{#{query_fields}}}").dig('data', 'full_areas')
20
+ end
21
+
22
+ def find(id)
23
+ query_fields = default_fields + ['description',
24
+ 'reform_office',
25
+ 'borders{geometry_type, coordinates}',
26
+ 'center_coords{geometry_type, coordinates}',
27
+ 'decentralisation_id'].join(',')
28
+ area_json("{full_area(area_id: \"#{id}\") { #{query_fields} } }").dig('data', 'full_area')
29
+ end
30
+
31
+ def area_full_name(area_id)
32
+ return if area_id.blank?
33
+ res = Rails.cache.fetch("area_full_name/#{area_id}", expires_in: 1.day) do
34
+ retrieve_full_name(area_id)
35
+ end
36
+ res.blank? ? retrieve_full_name(area_id) : res
37
+ end
38
+
39
+ def areas_for_select
40
+ areas.map { |area| [area['title'], area['id']] }
41
+ end
42
+
43
+ def find_by_title(part_title)
44
+ area_json("{ areas_by_title(title: \"#{part_title}\") { full_name, mongo_id } }")
45
+ end
46
+
47
+ private
48
+
49
+ def retrieve_full_name(area_id)
50
+ area_full_name_hash = area_json("{ area(area_id: \"#{area_id}\") { full_name } }")
51
+ area_full_name_hash.dig('data', 'area', 'full_name')
52
+ end
53
+
54
+ def filter_areas(areas)
55
+ # TODO: move this logic to map microservice
56
+
57
+ areas.map { |area| area if area['title'].include?('область') }.compact
58
+ rescue StandardError
59
+ default_areas
60
+ end
61
+
62
+ def default_areas
63
+ []
64
+ end
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,45 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Api
4
+ module AreaData
5
+ def area_data(path)
6
+ res = retrieve_data(path)
7
+ begin
8
+ JSON.parse(res.body)
9
+ rescue StandardError
10
+ { data: { areas: [] } }
11
+ end
12
+ end
13
+
14
+ def area_json(query)
15
+ # if Rails.env.production?
16
+ Rails.cache.fetch(query, expires_in: 2.minutes) do
17
+ area_data("https://map-ms.herokuapp.com/graphql?query=#{CGI.escape(query)}")
18
+ end
19
+ # else
20
+ # area_data("http://localhost:3000/graphql?query=#{URI.escape(query)}")
21
+ # end
22
+ end
23
+
24
+ protected
25
+
26
+ def default_fields
27
+ 'title, id, population, square, phone_code, local_community_count,'
28
+ end
29
+
30
+ private
31
+
32
+ def retrieve_data(path)
33
+ require 'net/http'
34
+ url = URI.parse(path)
35
+
36
+ req = Net::HTTP::Get.new(url.to_s, 'Content-Type' => 'application/json')
37
+ http = Net::HTTP.new(url.host, url.port)
38
+ http.use_ssl = true if url.scheme == 'https'
39
+
40
+ http.start do |http_obj|
41
+ http_obj.request(req)
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,6 @@
1
+ require "ua_town_finder/version"
2
+ require 'api/area'
3
+ require 'adapters/area'
4
+ module UaTownFinder
5
+ require 'ua_town_finder/search_town_controller'
6
+ end
@@ -0,0 +1,8 @@
1
+ class SearchTownController < ActionController::Base
2
+ def by_query
3
+ areas = Adapters::Area.new(Api::Area.find_by_title(params[:term])).call
4
+ respond_to do |format|
5
+ format.json { render json: areas }
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,3 @@
1
+ module UaTownFinder
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,33 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "ua_town_finder/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "ua_town_finder"
8
+ spec.version = UaTownFinder::VERSION
9
+ spec.authors = ["Alexandr"]
10
+ spec.email = ["1989alpan@gmail.com"]
11
+
12
+ spec.summary = %q{Gem for search towns by part of name}
13
+ spec.description = %q{Search town by part of fullname }
14
+
15
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
16
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
17
+ if spec.respond_to?(:metadata)
18
+ #spec.metadata["allowed_push_host"] = ""
19
+ else
20
+ raise "RubyGems 2.0 or newer is required to protect against " \
21
+ "public gem pushes."
22
+ end
23
+
24
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
25
+ f.match(%r{^(test|spec|features)/})
26
+ end
27
+ spec.bindir = "exe"
28
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
29
+ spec.require_paths = ["lib"]
30
+
31
+ spec.add_development_dependency "bundler", "~> 1.16"
32
+ spec.add_development_dependency "rake", "~> 10.0"
33
+ end
metadata ADDED
@@ -0,0 +1,84 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ua_town_finder
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Alexandr
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-04-23 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.16'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.16'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ description: 'Search town by part of fullname '
42
+ email:
43
+ - 1989alpan@gmail.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ".gitignore"
49
+ - Gemfile
50
+ - README.md
51
+ - Rakefile
52
+ - bin/console
53
+ - bin/setup
54
+ - lib/adapters/area.rb
55
+ - lib/api/area.rb
56
+ - lib/api/area_data.rb
57
+ - lib/ua_town_finder.rb
58
+ - lib/ua_town_finder/search_town_controller.rb
59
+ - lib/ua_town_finder/version.rb
60
+ - ua_town_finder.gemspec
61
+ homepage:
62
+ licenses: []
63
+ metadata: {}
64
+ post_install_message:
65
+ rdoc_options: []
66
+ require_paths:
67
+ - lib
68
+ required_ruby_version: !ruby/object:Gem::Requirement
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ version: '0'
73
+ required_rubygems_version: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ requirements: []
79
+ rubyforge_project:
80
+ rubygems_version: 2.5.1
81
+ signing_key:
82
+ specification_version: 4
83
+ summary: Gem for search towns by part of name
84
+ test_files: []