Skip to content

Commit

Permalink
Merge pull request #57 from navikt/swagger/response_objects
Browse files Browse the repository at this point in the history
feat: lagd flotte modeller for Swagger
  • Loading branch information
Kyrremann committed Dec 6, 2023
2 parents e6a99a3 + db637f8 commit 34bce20
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 26 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ gem 'geocoder', '~> 1.8'
gem 'grape', '~> 2.0'
gem 'grape_logging', '~> 1.8'
gem 'grape-swagger', '~> 2.0'
gem 'grape-swagger-entity', '~> 0.5.2'
gem 'prometheus-client', '~> 4.2'
gem 'puma', '~> 6.4'
gem 'rack-cors', '~> 2.0'
Expand Down
8 changes: 8 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,15 @@ GEM
mustermann-grape (~> 1.0.0)
rack (>= 1.3.0)
rack-accept
grape-entity (1.0.0)
activesupport (>= 3.0.0)
multi_json (>= 1.3.2)
grape-swagger (2.0.0)
grape (>= 1.7, < 3.0)
rack-test (~> 2)
grape-swagger-entity (0.5.2)
grape-entity (>= 0.6.0)
grape-swagger (>= 1.2.0)
grape_logging (1.8.4)
grape
rack
Expand All @@ -52,6 +58,7 @@ GEM
json (2.7.0)
language_server-protocol (3.17.0.3)
minitest (5.20.0)
multi_json (1.15.0)
mustermann (3.0.0)
ruby2_keywords (~> 0.0.1)
mustermann-grape (1.0.2)
Expand Down Expand Up @@ -104,6 +111,7 @@ DEPENDENCIES
geocoder (~> 1.8)
grape (~> 2.0)
grape-swagger (~> 2.0)
grape-swagger-entity (~> 0.5.2)
grape_logging (~> 1.8)
minitest (~> 5.20)
prometheus-client (~> 4.2)
Expand Down
4 changes: 2 additions & 2 deletions config.ru
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ run ->(_) { [200, { 'Content-Type' => 'text/html' }, ['OK']] }
require_relative 'lib/prometheus'
require_relative 'lib/api'

G.compile!
run G
GAPI.compile!
run GAPI
26 changes: 16 additions & 10 deletions lib/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@
require 'grape'
require 'grape_logging'
require 'grape-swagger'
require 'grape-swagger-entity'
require 'rack/cors'

require_relative 'g'
require_relative 'apihelper'

class G < Grape::API
class GAPI < Grape::API
include APIHelper

logger.formatter = GrapeLogging::Formatters::Logstash.new
Expand All @@ -28,7 +29,7 @@ class G < Grape::API

desc 'Litt om API-et', hidden: true
get do
g = Grunnbeløp.today
g = G.today
"G er en tjeneste som gir deg dagens grunnbeløp.
Grunnbeløp (#{g['dato']}): #{g['grunnbeløp']}
Expand All @@ -49,7 +50,7 @@ class G < Grape::API
end

def logger
G.logger
GAPI.logger
end
end

Expand All @@ -61,43 +62,48 @@ def logger

desc 'Returnerer dagens grunnbeløp' do
detail 'Man kan også søke opp andre grunnbeløp ved å spesifisere ?dato=<ISO 8601>'
success model: Grunnbeløp
end
params do
use :grunnbeløp
end
get :grunnbeløp do
dato = params[:dato] || DateTime.now
Grunnbeløp.by_date(dato)
G.by_date(dato)
end

desc 'Returnerer dagens grunnbeløp' do
detail 'Man kan også søke opp andre grunnbeløp ved å spesifisere ?dato=<ISO 8601>'
desc 'Returnerer dagens grunnbeloep' do
detail 'Man kan ogsaa soeke opp andre grunnbeloep ved å spesifisere ?dato=<ISO 8601>'
success model: Grunnbeloep
end
params do
use :grunnbeløp
end
get :grunnbeloep do
dato = params[:dato] || DateTime.now
JSON.parse(APIHelper.normalize_norwegian_letters(Grunnbeløp.by_date(dato).to_json))
JSON.parse(APIHelper.normalize_norwegian_letters(G.by_date(dato).to_json))
end

desc 'Historikk over grunnbeløp' do
detail 'Man kan få historikk fra en spesifikk dato ved å spesifisere ?fra=<ISO 8601>'
success model: Grunnbeløp
is_array true
end
params do
optional :fra, type: Date
end
get :historikk do
fra = params[:fra]
return Grunnbeløp.from_date(fra) if fra
return G.from_date(fra) if fra

Grunnbeløp.all_history
G.all_history
end

add_swagger_documentation hide_documentation_path: true,
doc_version: '1.0.0',
info: {
title: 'Grunnbeløp',
description: 'Grunnbeløp API'
}
},
produces: ['application/json']
end
24 changes: 23 additions & 1 deletion lib/g.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,31 @@
# frozen_string_literal: true

require 'date'
require 'grape'
require 'grape-swagger-entity'
require 'json'

module Grunnbeløp
class Grunnbeløp < Grape::Entity
expose :dato, documentation: { type: Date, desc: 'Dato for grunnbeløpet' }
expose :grunnbeløp, documentation: { type: Integer, desc: 'Grunnbeløpet' }
expose :grunnbeløp_per_måned, documentation: { type: Integer, desc: 'Grunnbeløpet per måned' }
expose :gjennomsnitt_per_år, documentation: { type: Integer, desc: 'Gjennomsnitt per år' }
expose :omregningsfaktor, documentation: { type: Float, desc: 'Omregningsfaktor' }
expose :virkningstidspunkt_for_minsteinntekt, documentation: { type: Date,
desc: 'Virkningstidspunkt for minsteinntekt' }
end

class Grunnbeloep < Grape::Entity
expose :dato, documentation: { type: Date, desc: 'Dato for grunnbeloepet' }
expose :grunnbeloep, documentation: { type: Integer, desc: 'Grunnbeloepet' }
expose :grunnbeloep_per_maaned, documentation: { type: Integer, desc: 'Grunnbeloepet per maaned' }
expose :gjennomsnitt_per_aar, documentation: { type: Integer, desc: 'Gjennomsnitt per aar' }
expose :omregningsfaktor, documentation: { type: Float, desc: 'Omregningsfaktor' }
expose :virkningstidspunkt_for_minsteinntekt, documentation: { type: Date,
desc: 'Virkningstidspunkt for minsteinntekt' }
end

module G
@grunnbeløp_data = JSON.parse(File.read(ENV.fetch('GRUNNBELOP', './grunnbeløp.json')), object_class: Hash)
@date_format = '%Y-%m-%d'

Expand Down
2 changes: 1 addition & 1 deletion lib/prometheus.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
require_relative 'g'

grunnbeløp_gauge = Prometheus::Client::Gauge.new(:grunnbeloep, docstring: 'Dagens grunnbeloep')
grunnbeløp_gauge.set(Grunnbeløp.by_date(DateTime.now)['grunnbeløp'])
grunnbeløp_gauge.set(G.by_date(DateTime.now)['grunnbeløp'])

prometheus = Prometheus::Client.registry
prometheus.register(grunnbeløp_gauge)
4 changes: 2 additions & 2 deletions test/test_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
require 'rack/test'
require './lib/api'

describe G do
describe GAPI do
include Rack::Test::Methods

def app
G
GAPI
end

describe 'test av V1' do
Expand Down
20 changes: 10 additions & 10 deletions test/test_g.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,33 @@
require 'minitest/pride'
require './lib/g'

describe Grunnbeløp do
describe G do
before do
# TODO: Mock-e ut datagrunnlaget
end

describe 'når noen ber om grunnbeløp' do
it 'skal du får siste grunnbeløpet' do
_(Grunnbeløp.by_date(DateTime.now)['grunnbeløp']).must_equal 111_477
_(G.by_date(DateTime.now)['grunnbeløp']).must_equal 111_477
end
end

describe 'når noen ber om grunnbeløp for en spesifikk dato' do
it 'skal du få riktig grunnbeløp' do
_(Grunnbeløp.by_date(DateTime.new(2011, 5, 1))['grunnbeløp']).must_equal 79_216
_(G.by_date(DateTime.new(2011, 5, 1))['grunnbeløp']).must_equal 79_216
end

it 'noen år har flere grunnbeløp' do
_(Grunnbeløp.by_date(DateTime.new(1977, 12, 31))['grunnbeløp']).must_equal 14_400
_(Grunnbeløp.by_date(DateTime.new(1977, 12, 1))['grunnbeløp']).must_equal 14_400
_(Grunnbeløp.by_date(DateTime.new(1977, 11, 30))['grunnbeløp']).must_equal 13_400
_(Grunnbeløp.by_date(DateTime.new(1977, 5, 1))['grunnbeløp']).must_equal 13_400
_(Grunnbeløp.by_date(DateTime.new(1977, 4, 30))['grunnbeløp']).must_equal 13_100
_(Grunnbeløp.by_date(DateTime.new(1977, 1, 1))['grunnbeløp']).must_equal 13_100
_(G.by_date(DateTime.new(1977, 12, 31))['grunnbeløp']).must_equal 14_400
_(G.by_date(DateTime.new(1977, 12, 1))['grunnbeløp']).must_equal 14_400
_(G.by_date(DateTime.new(1977, 11, 30))['grunnbeløp']).must_equal 13_400
_(G.by_date(DateTime.new(1977, 5, 1))['grunnbeløp']).must_equal 13_400
_(G.by_date(DateTime.new(1977, 4, 30))['grunnbeløp']).must_equal 13_100
_(G.by_date(DateTime.new(1977, 1, 1))['grunnbeløp']).must_equal 13_100
end

it 'fremtidige datoer er også akseptabelt' do
_(Grunnbeløp.by_date(DateTime.new(2120, 5, 1))['grunnbeløp']).must_equal 111_477
_(G.by_date(DateTime.new(2120, 5, 1))['grunnbeløp']).must_equal 111_477
end
end
end

0 comments on commit 34bce20

Please sign in to comment.