Skip to content

Commit

Permalink
Merge pull request #146 from loftwah/dl/geocoder-offline-fix
Browse files Browse the repository at this point in the history
restore old way
  • Loading branch information
loftwah authored Sep 14, 2024
2 parents 207ecbc + 5e9a171 commit a5a303b
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 22 deletions.
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ gem 'redis', '>= 4.0.1'
gem 'chartkick'
gem 'devise'
gem 'font-awesome-sass', '~> 6.5.2'
gem 'offline_geocoder'
gem 'geocoder'
gem 'groupdate'
gem 'mini_magick'
gem 'sidekiq'
Expand Down
15 changes: 5 additions & 10 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ GEM
concurrent-ruby (1.3.3)
connection_pool (2.4.1)
crass (1.0.6)
csv (3.3.0)
date (3.3.4)
debug (1.9.2)
irb (~> 1.10)
Expand Down Expand Up @@ -140,18 +141,14 @@ GEM
ffi (1.17.0-x86-linux-gnu)
ffi (1.17.0-x86_64-darwin)
ffi (1.17.0-x86_64-linux-gnu)
ffi-compiler (1.3.2)
ffi (>= 1.15.5)
rake
font-awesome-sass (6.5.2)
sassc (~> 2.0)
fugit (1.11.1)
et-orbi (~> 1, >= 1.2.11)
raabro (~> 1.4)
geokdtree (0.2.0)
ffi
ffi-compiler
rake
geocoder (1.8.3)
base64 (>= 0.1.0)
csv (>= 3.0.0)
globalid (1.2.1)
activesupport (>= 6.1)
groupdate (6.4.0)
Expand Down Expand Up @@ -208,8 +205,6 @@ GEM
racc (~> 1.4)
nokogiri (1.16.6-x86_64-linux)
racc (~> 1.4)
offline_geocoder (0.2.0)
geokdtree (~> 0.2)
orm_adapter (0.5.0)
psych (5.1.2)
stringio
Expand Down Expand Up @@ -390,11 +385,11 @@ DEPENDENCIES
devise
factory_bot_rails
font-awesome-sass (~> 6.5.2)
geocoder
groupdate
importmap-rails
jbuilder
mini_magick
offline_geocoder
puma (>= 5.0)
rails (~> 7.1.3, >= 7.1.3.4)
redis (>= 4.0.1)
Expand Down
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,16 @@ The restore process:
2. Loads the specified backup file.
3. Applies any pending migrations.

## Geolocation

Geolocation is currently a mandatory feature in Linkarooie. It uses the `geocoder` gem to provide location-based insights for link clicks and page views.

To enable geolocation:
1. Obtain a free API key from [ipapi](https://ipapi.com).
2. Set the `GEOCODER_API_KEY` environment variable with your API key.

Future plans include making geolocation optional to cater to different privacy preferences.

## Customization

Linkarooie is designed to be highly customizable:
Expand Down
19 changes: 10 additions & 9 deletions app/middleware/page_view_tracker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@ def track_page_view(request)
# Extract the original IP from the Cloudflare headers if available
real_ip = request.headers['CF-Connecting-IP'] || request.headers['X-Forwarded-For']&.split(',')&.first || request.ip

location = OFFLINE_GEOCODER.search(real_ip)

# Use Geocoder to find the location based on the real IP
location = Geocoder.search(real_ip).first

PageView.create(
user: user,
path: request.path,
Expand All @@ -36,13 +37,13 @@ def track_page_view(request)
visited_at: Time.current,
ip_address: real_ip,
session_id: request.session[:session_id],
country: location[:country],
city: location[:name],
state: location[:admin1],
county: location[:admin2],
latitude: location[:lat],
longitude: location[:lon],
country_code: location[:cc]
country: location&.country,
city: location&.city,
state: location&.state,
county: location&.county,
latitude: location&.latitude,
longitude: location&.longitude,
country_code: location&.country_code
)
end
rescue ActiveRecord::RecordNotUnique
Expand Down
30 changes: 30 additions & 0 deletions config/initializers/geocoder.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
Geocoder.configure(
# Geocoding options
timeout: 5, # geocoding service timeout (secs)
lookup: :google, # name of geocoding service (symbol)
ip_lookup: :ipinfo_io, # name of IP address geocoding service (you can choose a service)
language: :en, # ISO-639 language code

# Use HTTPS for lookup requests
use_https: true,

# API key for geocoding service
api_key: ENV['GEOCODER_API_KEY'], # Geocoder API key from environment variable

# Caching
cache: Redis.new, # cache object (e.g. Redis.new)
cache_prefix: "geocoder:", # prefix for cache keys

# Exceptions that should not be rescued by default
# (if you want to handle them yourself)
always_raise: [
Geocoder::OverQueryLimitError,
Geocoder::RequestDenied,
Geocoder::InvalidRequest,
Geocoder::InvalidApiKey
],

# Calculation options
units: :km, # :km for kilometers or :mi for miles
distances: :linear # :spherical or :linear
)
2 changes: 0 additions & 2 deletions config/initializers/offline_geocoder.rb

This file was deleted.

0 comments on commit a5a303b

Please sign in to comment.