Sha256: 75d186bbfe11b8cbe62bda468a9a50319609d83bce03f0c33855ba3b586b6d38

Contents?: true

Size: 1.5 KB

Versions: 1

Compression:

Stored size: 1.5 KB

Contents

module ProconBypassMan
  module Outbound
    class Base
      class Client
        class Result < Struct.new(:stats); end

        def initialize(path: , server: )
          @path = path
          if server.is_a?(Array)
            # TODO エラーが起きたらローテーションする
            @server = server.first
          else
            @server = server
          end
          @hostname = `hostname`.chomp
        end

        def post(body: )
          # TODO ここでvalidationする
          if @server.nil?
            ProconBypassMan.logger.info('送信先が未設定なのでスキップしました')
            return Result.new(false)
          end

          unless body.is_a?(Hash)
            body = { value: body }
          end

          uri = URI.parse("#{@server}#{@path}")
          http = Net::HTTP.new(uri.host, uri.port)
          http.use_ssl = uri.scheme === "https"
          params = { hostname: @hostname }.merge(body)
          response = http.post(
            uri.path,
            params.to_json,
            { "Content-Type" => "application/json" },
          )
          case response.code
          when /^200/
            return Result.new(true)
          else
            ProconBypassMan.logger.error("200以外(#{response.code})が帰ってきました. #{response.body}")
            return Result.new(false)
          end
        rescue => e
          puts e
          ProconBypassMan.logger.error("erro: #{e}")
          Result.new(false)
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
procon_bypass_man-0.1.11 lib/procon_bypass_man/outbound/base.rb