Class: Juici::TriggerController

Inherits:
Object
  • Object
show all
Defined in:
lib/juici/controllers/trigger_controller.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(project, params) ⇒ TriggerController

Returns a new instance of TriggerController.



5
6
7
8
# File 'lib/juici/controllers/trigger_controller.rb', line 5

def initialize(project, params)
  @project = Project.find_or_create_by(name: project)
  @params = params
end

Instance Attribute Details

#paramsObject (readonly)

Returns the value of attribute params.



4
5
6
# File 'lib/juici/controllers/trigger_controller.rb', line 4

def params
  @params
end

#projectObject (readonly)

Returns the value of attribute project.



4
5
6
# File 'lib/juici/controllers/trigger_controller.rb', line 4

def project
  @project
end

Instance Method Details

#build!Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/juici/controllers/trigger_controller.rb', line 10

def build!
  environment = BuildEnvironment.new
  Build.new(parent: project.name).tap do |build|
    # The seperation of concerns around this madness is horrifying
    unless environment.load_json!(params['environment'])
      build.warn!("Failed to parse environment")
    end

    build[:command] = build_command
    build[:priority] = build_priority
    build[:environment] = environment.to_hash
    build[:callbacks] = callbacks
    build[:title] = title if title_given?

    build.save!
    $build_queue << build
  end
end

#build_commandObject

These accessors are not really the concern of a “Controller”. On the other hand, this thing is also not a controller realistically, so I’m just going to carry on using this as a mechanism to poke at my model.

I later plan to implement a more strictly bounded interface which bails instead of coercing.



35
36
37
38
39
40
# File 'lib/juici/controllers/trigger_controller.rb', line 35

def build_command
  # TODO Throw 422 not acceptable if missing
  params['command'].tap do |c|
    raise "No command given" if c.nil?
  end
end

#build_priorityObject



42
43
44
45
46
47
48
# File 'lib/juici/controllers/trigger_controller.rb', line 42

def build_priority
  if params['priority'] =~ /^[0-9]+$/
    params['priority'].to_i
  else
    1
  end
end

#callbacksObject



50
51
52
53
54
# File 'lib/juici/controllers/trigger_controller.rb', line 50

def callbacks
  JSON.load(params['callbacks'])
rescue
  []
end

#titleObject



56
57
58
# File 'lib/juici/controllers/trigger_controller.rb', line 56

def title
  params['title']
end

#title_given?Boolean

Returns:

  • (Boolean)


60
61
62
63
# File 'lib/juici/controllers/trigger_controller.rb', line 60

def title_given?
  t = params['title']
  !(t.nil? || t.empty?)
end