Class: ActionServer

Inherits:
Sinatra::Base
  • Object
show all
Defined in:
lib/action_server.rb

Constant Summary collapse

QUEUE_NAME =
'action'

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.cache_dirObject

Returns the value of attribute cache_dir.



14
15
16
# File 'lib/action_server.rb', line 14

def cache_dir
  @cache_dir
end

.queueObject (readonly)

Returns the value of attribute queue.



14
15
16
# File 'lib/action_server.rb', line 14

def queue
  @queue
end

.rabbitmq_configObject

Returns the value of attribute rabbitmq_config.



14
15
16
# File 'lib/action_server.rb', line 14

def rabbitmq_config
  @rabbitmq_config
end

Class Method Details

.channelObject



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/action_server.rb', line 24

def channel
  @channel ||= begin
    connection = Bunny.new(
      host:     rabbitmq_config[:host],
      user:     rabbitmq_config[:user],
      password: rabbitmq_config[:password],
      vhost:    rabbitmq_config[:vhost]
    )
    connection.start
    connection.create_channel
  end
end

.exchangeObject



37
38
39
# File 'lib/action_server.rb', line 37

def exchange
  @exchange ||= channel.default_exchange
end

Instance Method Details

#cache_dirObject



42
43
44
# File 'lib/action_server.rb', line 42

def cache_dir
  self.class.cache_dir
end

#downloaded_file(src, dest) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/action_server.rb', line 75

def downloaded_file(src, dest)
  downloaded_file = nil
  request = Typhoeus::Request.new(src, followlocation: true)

  request.on_headers do |response|
    if response.code == 200
      downloaded_file = File.open(dest, 'wb')
    else
      error response.code
    end
  end

  request.on_body do |chunk|
    downloaded_file.write(chunk) if downloaded_file
  end

  request.on_complete do |response|
    downloaded_file.close if downloaded_file
  end

  request.run
end

#extract_headers(request) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/action_server.rb', line 56

def extract_headers(request)
  headers = {}

  request.env.each do |name, val|
    header = name.match(/^HTTP_(.*)$/)
    if header && header[1]
      headers[header[1]] = val
    end
  end

  headers
end

#extract_ip(request) ⇒ Object



51
52
53
54
# File 'lib/action_server.rb', line 51

def extract_ip(request)
  ip = request['HTTP_X_FORWARDED_FOR'] || request.ip
  ip.to_s
end

#md5(string) ⇒ Object



69
70
71
72
73
# File 'lib/action_server.rb', line 69

def md5(string)
  md5 = Digest::MD5.new
  md5.update(string)
  md5.hexdigest
end

#publish(message) ⇒ Object



47
48
49
# File 'lib/action_server.rb', line 47

def publish(message)
  self.class.exchange.publish(message, routing_key: QUEUE_NAME)
end