Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ gem 'activesupport', require: false
gem 'dry-initializer'
gem 'dry-validation'

gem 'faraday'

gem 'bunny'

group :development do
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# Geocoder Microservice (asynchronous RabbitMQ)
Geocoder microservice for RabbitMQ asynchronous [Ads Microservice](https://github.com/rubygitflow/ads_microservice/tree/rabbitmq_asynchronous) from Ruby Microservices course
# Geocoder Microservice (synchronous RabbitMQ)
Geocoder microservice for RabbitMQ synchronous [Ads Microservice](https://github.com/rubygitflow/ads_microservice/tree/rabbitmq_synchro) from Ruby Microservices course

It's set up so you can clone this repository and base your application on it:
```bash
$ git clone git@github.com:rubygitflow/geocoder_microservice.git app_geo --single-branch --branch rabbitmq_asynchronous && cd app_geo && rm -r -f .git/
$ git clone git@github.com:rubygitflow/geocoder_microservice.git app_geo --single-branch --branch rabbitmq_synchro && cd app_geo && rm -r -f .git/
```
Initialize and configure a new Git repository (you need to have a [personal access token](https://github.com/settings/tokens)):
```bash
Expand Down
17 changes: 17 additions & 0 deletions app/lib/ads_service/http_api.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# frozen_string_literal: true

module AdsService
module HttpApi
def update_coordinates(id, coordinates)
params = {
id: id,
coordinates: {
lat: coordinates[0],
lon: coordinates[1]
}
}

connection.patch('ads', params)
end
end
end
24 changes: 24 additions & 0 deletions app/lib/ads_service/http_client.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# frozen_string_literal: true

require 'dry/initializer'
require_relative 'http_api'

module AdsService
class HttpClient
extend Dry::Initializer[undefined: false]
include HttpApi

option :url, default: proc { ENV.fetch('ADS_URL', 'http://localhost:3001/api/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
6 changes: 5 additions & 1 deletion app/lib/rabbit_mq.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ module RabbitMq

def connection
@mutex.synchronize do
@connection ||= Bunny.new.start
@connection ||= Bunny.new(
host: ENV.fetch('RABBITMQ_HOST'),
username: ENV.fetch('RABBITMQ_USER'),
password: ENV.fetch('RABBITMQ_PASSWORD')
).start
end
end

Expand Down
6 changes: 6 additions & 0 deletions config/environment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

ENV['RACK_ENV'] ||= 'development'

begin
require_relative '../.env'
rescue LoadError
# do nothing
end

require 'bundler/setup'
Bundler.require(:default, ENV['RACK_ENV'])

Expand Down
2 changes: 1 addition & 1 deletion config/initializers/consumer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
coordinates = Geocoder.geocode(payload['city'])

if coordinates.present?
client = AdsService::RpcClient.fetch
client = AdsService::HttpClient.new
client.update_coordinates(payload['id'], coordinates)
end

Expand Down