Skip to content

Commit

Permalink
Add rubocop and reformat
Browse files Browse the repository at this point in the history
Also updates the README and a few other misc things
  • Loading branch information
thatbudakguy committed Jun 4, 2024
1 parent e6b6397 commit caf6caf
Show file tree
Hide file tree
Showing 23 changed files with 199 additions and 87 deletions.
33 changes: 33 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
require:
- rubocop-rspec
- rubocop-rails
- rubocop-rspec_rails

AllCops:
TargetRubyVersion: 3.2
DisplayCopNames: true
Exclude:
- "Gemfile"
- "*.gemspec"
- "bin/**/*"
- "db/**/*"
- "tmp/**/*"
- "storage/**/*"
- "node_modules/**/*"
- "vendor/**/*"

Rails:
Enabled: true

Metrics/AbcSize:
Enabled: false
Metrics/MethodLength:
Enabled: false
Layout/LineLength:
Enabled: false
RSpec/ExampleLength:
Enabled: false
RSpec/MultipleExpectations:
Enabled: false
Naming/VariableNumber:
EnforcedStyle: snake_case
18 changes: 13 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# GeoMonitor
Short description and motivation.
A Rails engine for monitoring geospatial web services.

## Usage
How to use my plugin.
Expand All @@ -13,16 +13,24 @@ gem 'geo_monitor'

And then execute:
```bash
$ bundle
bundle
```

Or install it yourself as:
```bash
$ gem install geo_monitor
gem install geo_monitor
```

## Contributing
Contribution directions go here.
## Developing
Set up the internal test app using `engine_cart`:
```bash
rake engine_cart:generate
```

Run the test suite, including RuboCop style checks:
```bash
rake ci
```

## License
The gem is available as open source under the terms of the [Apache License 2.0](https://opensource.org/licenses/Apache-2.0).
23 changes: 8 additions & 15 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

begin
require 'bundler/setup'
rescue LoadError
Expand All @@ -6,23 +8,14 @@ end

require 'bundler/gem_tasks'
require 'engine_cart/rake_task'
require 'rspec/core/rake_task'
require 'rubocop/rake_task'

namespace :geomonitor do
desc 'Create the test rails app'
task generate: ['engine_cart:generate'] do
end
end
RSpec::Core::RakeTask.new(:spec)
RuboCop::RakeTask.new(:rubocop) { |task| task.fail_on_error = true }

task ci: ['geomonitor:generate'] do
task ci: [:rubocop, 'engine_cart:generate'] do
Rake::Task['spec'].invoke
end

begin
require 'rspec/core/rake_task'

RSpec::Core::RakeTask.new(:spec)

task default: :ci
rescue LoadError
# no rspec available
end
task default: :ci
3 changes: 3 additions & 0 deletions app/models/geo_monitor/application_record.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
# frozen_string_literal: true

module GeoMonitor
# Base class for all models in the engine
class ApplicationRecord < ActiveRecord::Base
self.abstract_class = true
end
Expand Down
3 changes: 3 additions & 0 deletions app/models/geo_monitor/geo_monitor.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
# frozen_string_literal: true

module GeoMonitor
# Generic database config for the engine
module GeoMonitor
def self.table_name_prefix
'geo_monitor_geo_monitor_'
Expand Down
7 changes: 5 additions & 2 deletions app/models/geo_monitor/layer.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# frozen_string_literal: true

module GeoMonitor
# A single layer in a GeoBlacklight instance
class Layer < ApplicationRecord
has_many :statuses
has_many :statuses, dependent: :destroy

##
# @param [String] schema_json
Expand Down Expand Up @@ -32,7 +35,7 @@ def check
response = nil
time = Benchmark.measure do
response = ::GeoMonitor::Requests::WMS.new(
bbox: bounding_box, url: url, layers: layername
bbox: bounding_box, url:, layers: layername
).tile
end
::GeoMonitor::Status.from_response(response, self, time.real.to_f)
Expand Down
13 changes: 9 additions & 4 deletions app/models/geo_monitor/status.rb
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
# frozen_string_literal: true

module GeoMonitor
# A record of availability for a single layer at a single point in time
class Status < ApplicationRecord
belongs_to :layer, counter_cache: true, touch: true
before_create :limit_by_layer

##
# Limits the number of statuses per layer to prevent a ballooing database
def limit_by_layer
statuses_by_layer = self.class.where(layer: layer).count
statuses_by_layer = self.class.where(layer:).count
max = ::GeoMonitor::Engine.config.max_status_per_layer
return unless statuses_by_layer >= max

self.class
.where(layer: layer)
.where(layer:)
.first(statuses_by_layer - max + 1)
.map(&:destroy) if statuses_by_layer >= max
.map(&:destroy)
end

##
Expand All @@ -23,7 +28,7 @@ def self.from_response(response, layer, time)
res_time: time.to_f,
res_code: response.status,
submitted_query: response.env[:url].to_s,
layer: layer,
layer:,
res_headers: response.headers,
content_type: response.headers.try(:[], :content_type)
)
Expand Down
6 changes: 5 additions & 1 deletion geo_monitor.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ Gem::Specification.new do |s|

s.add_development_dependency 'engine_cart', '~> 2.0'
s.add_development_dependency 'sqlite3'
s.add_development_dependency 'rspec-rails'
s.add_development_dependency 'webmock'
s.add_development_dependency 'rspec'
s.add_development_dependency 'rspec-rails'
s.add_development_dependency 'rubocop'
s.add_development_dependency 'rubocop-rspec'
s.add_development_dependency 'rubocop-rails'
end
4 changes: 3 additions & 1 deletion lib/generators/geo_monitor/install_generator.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
# frozen_string_literal: true

module GeoMonitor
##
# GeoMonitor Installer
class Install < Rails::Generators::Base
source_root File.expand_path('../templates', __FILE__)
source_root File.expand_path('templates', __dir__)
end
end
6 changes: 4 additions & 2 deletions lib/geo_monitor.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
require "geo_monitor/engine"
# frozen_string_literal: true

require 'geo_monitor/engine'

# Main module for the engine
module GeoMonitor
extend ActiveSupport::Autoload


# A simple structure to conform to Faraday::Response
FailedResponse = Struct.new(:env, :status, :headers)
end
4 changes: 3 additions & 1 deletion lib/geo_monitor/bounding_box.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module GeoMonitor
##
# Bounding Box
Expand Down Expand Up @@ -59,7 +61,7 @@ def tile_number
else
x
end
{ x: x, y: y }
{ x:, y: }
end
end
end
2 changes: 2 additions & 0 deletions lib/geo_monitor/constants.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module GeoMonitor
module Constants
R = 6_378_137 # Radius of Earth in meters
Expand Down
2 changes: 2 additions & 0 deletions lib/geo_monitor/engine.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'rails/all'
require 'faraday'

Expand Down
6 changes: 5 additions & 1 deletion lib/geo_monitor/lat_lng_point.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# frozen_string_literal: true

module GeoMonitor
# A point in space with latitude and longitude
class LatLngPoint
attr_accessor :lat, :lng

def initialize(lat: 0, lng: 0)
@lat = lat.to_f
@lng = lng.to_f
Expand All @@ -26,7 +30,7 @@ def self.from_number(xtile, ytile, zoom)
lng = xtile / n * 360.0 - 180.0
lat_rad = Math.atan(Math.sinh(Math::PI * (1 - 2 * ytile / n)))
lat = 180.0 * (lat_rad / Math::PI)
new(lat: lat, lng: lng)
new(lat:, lng:)
end
end
end
8 changes: 5 additions & 3 deletions lib/geo_monitor/requests/wms.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module GeoMonitor
module Requests
##
Expand Down Expand Up @@ -33,12 +35,12 @@ def request_params
##
# Request the tile.
def tile
unless url.present?
if url.blank?
return ::GeoMonitor::FailedResponse.new(
{ url: url }, 'No URL provided', {}
{ url: }, 'No URL provided', {}
)
end
conn = Faraday.new(url: url)
conn = Faraday.new(url:)
begin
conn.get do |request|
request.params = request_params
Expand Down
4 changes: 3 additions & 1 deletion lib/geo_monitor/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module GeoMonitor
VERSION = '0.8.0'.freeze
VERSION = '0.8.0'
end
15 changes: 10 additions & 5 deletions spec/lib/geo_monitor/bounding_box_spec.rb
Original file line number Diff line number Diff line change
@@ -1,27 +1,32 @@
# frozen_string_literal: true

require 'spec_helper'

describe GeoMonitor::BoundingBox do
subject do
subject(:box) do
described_class.new(
north: 4.234077,
south: -1.478794,
west: 29.572742,
east: 35.000308
)
end

describe '#to_s' do
it 'w,s,e,n' do
expect(subject.to_s).to eq '29.572742,-1.478794,35.000308,4.234077'
expect(box.to_s).to eq '29.572742,-1.478794,35.000308,4.234077'
end
end

describe '#zoom_level' do
it 'calculates the preferred zoom level' do
expect(subject.zoom_level).to eq 6
expect(box.zoom_level).to eq 6
end
end

describe '#tile_number' do
it '' do
expect(subject.tile_number).to include(x: 37, y: 32)
it do
expect(box.tile_number).to include(x: 37, y: 32)
end
end
end
9 changes: 6 additions & 3 deletions spec/lib/geo_monitor/lat_lng_point_spec.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
# frozen_string_literal: true

require 'spec_helper'

describe GeoMonitor::LatLngPoint do
subject do
subject(:point) do
described_class.new(lat: 21.943045533438177, lng: -103.359375)
end

describe '#to_3857' do
it { expect(subject.to_3857.lat).to eq(2504688.542848655) }
it { expect(subject.to_3857.lng).to eq(-11505912.99371101) }
it { expect(point.to_3857.lat).to eq(2_504_688.542848655) }
it { expect(point.to_3857.lng).to eq(-11_505_912.99371101) }
end
end
Loading

0 comments on commit caf6caf

Please sign in to comment.