Class: Stilts::Sender
- Inherits:
-
Object
- Object
- Stilts::Sender
- 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
-
#user_agent ⇒ Object
Returns the value of attribute user_agent.
Instance Method Summary collapse
-
#initialize(options = {}) ⇒ Sender
constructor
A new instance of Sender.
- #log(level, message, response = nil) ⇒ Object
- #logger ⇒ Object
- #send_image_transform_data(data) ⇒ Object
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( = {}) [:protocol, :host, :port, :secure, :http_open_timeout, :http_read_timeout].each do |option| instance_variable_set("@#{option}", [option]) end end |
Instance Attribute Details
#user_agent ⇒ Object
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, , response = nil) logger.send level, LOG_PREFIX + if logger Stilts.report_environment_info Stilts.report_response_body(response.body) if response && response.respond_to?(:body) end |
#logger ⇒ Object
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 |