Class: Stilts::Sender

Inherits:
Object
  • Object
show all
Defined in:
lib/stilts/sender.rb

Constant Summary collapse

TRANSFORM_HEADERS =
{
  'Content-Type'    => 'application/json',
  'Accept'          => 'application/json'
}
HTTP_ERRORS =
[Timeout::Error,
  Errno::EINVAL,
  Errno::ECONNRESET,
  EOFError,
  Net::HTTPBadResponse,
  Net::HTTPHeaderSyntaxError,
  Net::ProtocolError,
Errno::ECONNREFUSED].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Sender

Returns a new instance of Sender.



19
20
21
22
23
# File 'lib/stilts/sender.rb', line 19

def initialize(options = {})
  [:protocol, :host, :port, :secure, :http_open_timeout, :http_read_timeout].each do |option|
    instance_variable_set("@#{option}", options[option])
  end
end

Instance Attribute Details

#user_agentObject

Returns the value of attribute user_agent.



17
18
19
# File 'lib/stilts/sender.rb', line 17

def user_agent
  @user_agent
end

Instance Method Details

#log(level, message, response = nil) ⇒ Object



43
44
45
46
47
# File 'lib/stilts/sender.rb', line 43

def log(level, message, response = nil)
  logger.send level, LOG_PREFIX + message if logger
  Stilts.report_environment_info
  Stilts.report_response_body(response.body) if response && response.respond_to?(:body)
end

#loggerObject



49
50
51
# File 'lib/stilts/sender.rb', line 49

def logger
  Stilts.configuration.logger
end

#send_image_transform_data(data) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/stilts/sender.rb', line 25

def send_image_transform_data(data)
  http              = Net::HTTP.new(@host, @port)

  http.read_timeout = @http_read_timeout
  http.open_timeout = @http_open_timeout
  http.use_ssl      = false
  headers           = TRANSFORM_HEADERS.merge({'User-Agent' => user_agent})

  response = begin
    http.post("/transform/#{Stilts.configuration.api_key}", data, headers)
  rescue *HTTP_ERRORS => e
    log :error, "Timeout while contacting the server. #{e}"
    nil
  end

  Stilts.receiver.receive(response)
end