Class: Compaa::RackApp::Request

Inherits:
Object
  • Object
show all
Defined in:
lib/compaa/rack_app.rb

Instance Method Summary collapse

Constructor Details

#initialize(request) ⇒ Request

Returns a new instance of Request.



26
27
28
# File 'lib/compaa/rack_app.rb', line 26

def initialize(request)
  @request = request
end

Instance Method Details

#artifacts_jsonObject



80
81
82
83
84
85
86
87
88
89
# File 'lib/compaa/rack_app.rb', line 80

def artifacts_json
  difference_images = DifferenceImage.all.map(&:reference_path)
  generated_images  = GeneratedImage.all.reject(&:has_reference_image?).map(&:reference_path)

  json = {
    artifacts: difference_images + generated_images
  }.to_json

  [ 200, { 'Content-Type' => 'application/json' }, [json] ]
end

#four_oh_fourObject



91
92
93
# File 'lib/compaa/rack_app.rb', line 91

def four_oh_four
  [ 404, { 'Content-Type' => 'text/plain' }, ['Not found'] ]
end

#getObject



38
39
40
41
42
43
44
45
46
47
# File 'lib/compaa/rack_app.rb', line 38

def get
  case @request.path
  when '/'                   then index
  when '/artifacts.json'     then artifacts_json
  when '/compaa.js'          then compaa_js
  when '/blender.js'         then blender_js
  when '/context_blender.js' then context_blender_js
  else                            four_oh_four
  end
end

#indexObject



57
58
59
60
61
62
# File 'lib/compaa/rack_app.rb', line 57

def index
  template = File.read File.expand_path '../assets/index.haml', File.dirname(__FILE__)
  body = Haml::Engine.new(template).render

  [ 200, { 'Content-Type' => 'text/html' }, [body] ]
end

#postObject



49
50
51
52
53
54
55
# File 'lib/compaa/rack_app.rb', line 49

def post
  if @request.path == '/screenshots'
    screenshots
  else
    four_oh_four
  end
end

#responseObject



30
31
32
33
34
35
36
# File 'lib/compaa/rack_app.rb', line 30

def response
  case @request.request_method
  when 'GET'  then get
  when 'POST' then post
  else        four_oh_four
  end
end

#screenshotsObject



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/compaa/rack_app.rb', line 64

def screenshots
  if @request.params.has_key?('filepath')
    path = File.join(Dir.pwd, @request.params['filepath'])
    generated_image = GeneratedImage.new(path)

    generated_image.create_reference_image

    generated_image.delete_difference_image
    generated_image.delete

    [ 200, { 'Content-Type' => 'text/plain' }, ['Success'] ]
  else
    four_oh_four
  end
end