Skip to content

Commit

Permalink
GET /api/reload.json to reload metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
alpaca-tc committed Dec 11, 2024
1 parent 1f1b2d2 commit 15a0111
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 2 deletions.
2 changes: 2 additions & 0 deletions lib/diver_down/web.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ def call(env)
in ['GET', %r{\A/api/sources/(?<source>[^/]+)\.json\z}]
source = Regexp.last_match[:source]
@action.source(source)
in ['GET', %r{\A/api/reload\.json\z}]
@action.reload_cache
in ['POST', %r{\A/api/sources/(?<source>[^/]+)/module.json\z}]
source = Regexp.last_match[:source]
modulee = request.params['module'] || ''
Expand Down
7 changes: 7 additions & 0 deletions lib/diver_down/web/action.rb
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,13 @@ def set_memo(source_name, memo)
end
end

# @return [Array[Integer, Hash, Array]]
def reload_cache
@metadata.reload
reload
json({})
end

# @return [Array[Integer, Hash, Array]]
def not_found
[404, { 'content-type' => 'text/plain' }, ['not found']]
Expand Down
4 changes: 2 additions & 2 deletions lib/diver_down/web/metadata.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class Metadata
require 'diver_down/web/metadata/source_metadata'
require 'diver_down/web/metadata/source_alias'

attr_reader :source_alias
attr_reader :path, :source_alias

# @param path [String]
def initialize(path)
Expand Down Expand Up @@ -47,7 +47,7 @@ def reload
@source_map = Hash.new { |h, source_name| h[source_name] = DiverDown::Web::Metadata::SourceMetadata.new }
@source_alias = DiverDown::Web::Metadata::SourceAlias.new

loaded = YAML.load_file(@path)
loaded = YAML.load_file(@path) if File.exist?(@path)

return if loaded.nil?

Expand Down
16 changes: 16 additions & 0 deletions spec/diver_down/web_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -790,4 +790,20 @@ def assert_definition_group(definition_group, expected_ids)
expect(metadata.source_alias.to_h).to eq(prev)
end
end

describe 'POST /api/reload.json' do
it 'reloads metadata from file' do
new_metadata = DiverDown::Web::Metadata.new(metadata.path)
new_metadata.source('a.rb').module = 'A'
new_metadata.flush

expect {
get '/api/reload.json'
}.to change {
metadata.source('a.rb').module
}.from(nil).to('A')

expect(last_response.status).to eq(200)
end
end
end

0 comments on commit 15a0111

Please sign in to comment.