diff --git a/.tool-versions b/.tool-versions new file mode 100644 index 0000000..1c44dd0 --- /dev/null +++ b/.tool-versions @@ -0,0 +1 @@ +ruby 2.6.6 diff --git a/Gemfile b/Gemfile index da02928..d552914 100644 --- a/Gemfile +++ b/Gemfile @@ -17,6 +17,9 @@ gem 'dry-validation', '~> 1.5.0' gem 'activesupport', '~> 6.0.0', require: false gem 'fast_jsonapi', '~> 1.5' +gem 'faraday' +gem 'faraday_middleware' + group :test do gem 'rspec', '~> 3.9.0' gem 'factory_bot', '~> 5.2.0' diff --git a/Gemfile.lock b/Gemfile.lock index a7adbeb..d4d6b6d 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -56,11 +56,37 @@ GEM dry-schema (~> 1.5) factory_bot (5.2.0) activesupport (>= 4.2.0) + faraday (1.10.2) + faraday-em_http (~> 1.0) + faraday-em_synchrony (~> 1.0) + faraday-excon (~> 1.1) + faraday-httpclient (~> 1.0) + faraday-multipart (~> 1.0) + faraday-net_http (~> 1.0) + faraday-net_http_persistent (~> 1.0) + faraday-patron (~> 1.0) + faraday-rack (~> 1.0) + faraday-retry (~> 1.0) + ruby2_keywords (>= 0.0.4) + faraday-em_http (1.0.0) + faraday-em_synchrony (1.0.0) + faraday-excon (1.1.0) + faraday-httpclient (1.0.1) + faraday-multipart (1.0.4) + multipart-post (~> 2) + faraday-net_http (1.0.1) + faraday-net_http_persistent (1.2.0) + faraday-patron (1.0.0) + faraday-rack (1.0.0) + faraday-retry (1.0.3) + faraday_middleware (1.2.0) + faraday (~> 1.0) fast_jsonapi (1.5) activesupport (>= 4.2) i18n (1.8.2) concurrent-ruby (~> 1.0) minitest (5.14.1) + multipart-post (2.2.3) rake (13.0.1) rspec (3.9.0) rspec-core (~> 3.9.0) @@ -75,6 +101,7 @@ GEM diff-lcs (>= 1.2.0, < 2.0) rspec-support (~> 3.9.0) rspec-support (3.9.3) + ruby2_keywords (0.0.5) thread_safe (0.3.6) tzinfo (1.2.7) thread_safe (~> 0.1) @@ -90,6 +117,8 @@ DEPENDENCIES dry-initializer (~> 3.0.3) dry-validation (~> 1.5.0) factory_bot (~> 5.2.0) + faraday + faraday_middleware fast_jsonapi (~> 1.5) i18n (~> 1.8.2) rake (~> 13.0.1) diff --git a/app/lib/ads_service/api.rb b/app/lib/ads_service/api.rb new file mode 100644 index 0000000..8548b51 --- /dev/null +++ b/app/lib/ads_service/api.rb @@ -0,0 +1,8 @@ +module GeocoderService + module Api + def update_coordinates(id, coordinates) + payload = { id: id, coordinates: coordinates }.to_json + connection.post('update_coordinates', payload) + end + end +end diff --git a/app/lib/ads_service/client.rb b/app/lib/ads_service/client.rb new file mode 100644 index 0000000..a8e426c --- /dev/null +++ b/app/lib/ads_service/client.rb @@ -0,0 +1,22 @@ +require 'dry/initializer' +require_relative 'api' + +module GeocoderService + class Client + extend Dry::Initializer[undefined: false] + include Api + + option :url, default: proc { 'http://localhost:3000/ads/v1' } + option :connection, default: proc { build_connection } + + private + + def build_connection + Faraday.new(@url) do |conn| + conn.request :json + conn.response :json, content_type: /\bjson$/ + conn.adapter Faraday.default_adapter + end + end + end +end diff --git a/app/lib/geocoder.rb b/app/lib/geocoder.rb index ebaee97..4b9994e 100644 --- a/app/lib/geocoder.rb +++ b/app/lib/geocoder.rb @@ -20,7 +20,7 @@ def load_data! city = row['city'] lat = row['geo_lat'].to_f lon = row['geo_lon'].to_f - result[city] = [lat, lon] + result[city] = { lat: lat, lon: lon } result end end diff --git a/config/initializers/consumer.rb b/config/initializers/consumer.rb index f6855ea..21ea9f5 100644 --- a/config/initializers/consumer.rb +++ b/config/initializers/consumer.rb @@ -6,9 +6,9 @@ coordinates = Geocoder.geocode(payload['city']) if coordinates.present? - client = AdsService::RpcClient.fetch + client = GeocoderService::Client.new client.update_coordinates(payload['id'], coordinates) end - +ensure channel.ack(delivery_info.delivery_tag) end