Class: GovFakeNotify::FetchTemplatesCommand

Inherits:
Object
  • Object
show all
Defined in:
lib/gov_fake_notify/commands/fetch_templates_command.rb

Overview

A service used to fetch all templates

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params, store: Store.instance) ⇒ FetchTemplatesCommand



17
18
19
20
21
22
# File 'lib/gov_fake_notify/commands/fetch_templates_command.rb', line 17

def initialize(params, store: Store.instance)
  @params = params
  @store = store
  @errors = []
  @results = []
end

Instance Attribute Details

#errorsObject (readonly)

Returns the value of attribute errors.



11
12
13
# File 'lib/gov_fake_notify/commands/fetch_templates_command.rb', line 11

def errors
  @errors
end

Class Method Details

.call(params, **kwargs) ⇒ Object



13
14
15
# File 'lib/gov_fake_notify/commands/fetch_templates_command.rb', line 13

def self.call(params, **kwargs)
  new(params, **kwargs).call
end

Instance Method Details

#callObject



24
25
26
27
28
29
# File 'lib/gov_fake_notify/commands/fetch_templates_command.rb', line 24

def call
  @results = store.transaction { store.roots.select { |k| k =~ /^template-/ } }.map do |key|
    store.transaction { store.fetch(key).slice('id', 'name', 'subject') }
  end
  self
end

#success?Boolean



31
32
33
# File 'lib/gov_fake_notify/commands/fetch_templates_command.rb', line 31

def success?
  errors.empty?
end

#to_jsonObject



35
36
37
# File 'lib/gov_fake_notify/commands/fetch_templates_command.rb', line 35

def to_json
  JSON.pretty_generate(templates: results)
end