Skip to content

Commit

Permalink
Setup versions APi to communicate with ETModel
Browse files Browse the repository at this point in the history
  • Loading branch information
noracato committed Dec 17, 2024
1 parent 31e0e3d commit b6228f6
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 28 deletions.
4 changes: 2 additions & 2 deletions app/controllers/api/v1/saved_scenarios_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def update
result = SavedScenario::Update.call(
engine_client,
@saved_scenario,
saved_scenario_params
saved_scenario_params.except(:version)
)

if result.successful?
Expand All @@ -62,7 +62,7 @@ def destroy
# Only allow a list of trusted parameters through.
def saved_scenario_params
params.require(:saved_scenario).permit(
:scenario_id, :title,
:scenario_id, :title, :version,
:description, :area_code, :end_year, :private, :discarded
)
end
Expand Down
22 changes: 2 additions & 20 deletions app/controllers/api/v1/versions_controller.rb
Original file line number Diff line number Diff line change
@@ -1,29 +1,11 @@
module Api
module V1
class VersionsController < BaseController
skip_before_action :authenticate_request!

def index
versions = Version.all
base_url = request.base_url

version_data = versions.map do |version|
{
version: version,
url: version_url(version, base_url)
}
end

render json: { versions: version_data }, status: :ok
render json: { versions: Version.as_json }, status: :ok
end

private

# Generates a version URL given a version name and the base URL
def version_url(version, base_url)
"https://#{version}.#{base_url}"
end


end
end
end
2 changes: 1 addition & 1 deletion app/models/saved_scenario.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class SavedScenario < ApplicationRecord
validates :end_year, presence: true
validates :area_code, presence: true
validates :version, presence: true, inclusion: {
in: Version.all, message: "Version should be one of #{Version.all}"
in: Version.tags, message: "Version should be one of #{Version.tags}"
}

serialize :scenario_id_history, coder: YAML, type: Array
Expand Down
18 changes: 15 additions & 3 deletions app/models/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,25 @@

# A valid version of the ETM
class Version
LIST = %w[
latest
].freeze
URL = "energytransitionmodel.com".freeze

LIST = {
"latest" => "https://#{Version::URL}",
"stable.01" => "https://stable.#{Version::URL}",
"stable.02" => "https://stable2.#{Version::URL}"
}.freeze

# All available versions. Uses ActiveRecord syntax 'all' to
# make future porting to db easier
def self.all
LIST
end

def self.tags
LIST.keys
end

def self.as_json(*)
LIST.map { |tag, url| { tag: tag, url: url } }
end
end
3 changes: 1 addition & 2 deletions spec/controllers/api/v1/versions_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,8 @@
parsed_response = JSON.parse(response.body)

expect(parsed_response['versions']).to be_present
expect(parsed_response['versions'].map { |v| v['version'] }).to match_array(Version.all)
expect(parsed_response['versions'].map { |v| v['tag'] }).to match_array(Version.tags)
expect(parsed_response['versions'].first).to have_key('url')
puts parsed_response
end
end
end

0 comments on commit b6228f6

Please sign in to comment.