Sha256: 11d031ddd5e2594359276f8d8a72cb7a5a1828bc244e25bc15dc6bba35ba0616

Contents?: true

Size: 1.02 KB

Versions: 2

Compression:

Stored size: 1.02 KB

Contents

# coding: utf-8
module Bearychat
  class Notifier
    class HttpClient
      class << self
        def post(uri, params)
          HttpClient.new(uri, params).call
        end
      end

      attr_reader :uri, :params, :http_options

      def initialize(url, params) 
        @uri = url
        @http_options = params.delete(:http_options) || {}
        @params = params
      end

      def call
        http_post
      end

      private
      def http_post
        url = URI.parse(uri)
        post_obj = Net::HTTP::Post.new(url.path)
        post_obj.set_form_data(params)

        socket = Net::HTTP.new(url.host, url.port)
        socket.use_ssl = true if url.scheme.downcase == "https"
        
        http_options.each do |opt, val|
          if socket.respond_to? "#{opt}="
            socket.send "#{opt}=", val
          else
            warn "Net::HTTP doesn't respond to `#{opt}=`, ignoring that option"
          end
        end

        response = socket.start {|http| http.request(post_obj) }
      end
    end 
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
bearychat-notifier-0.0.6 lib/bearychat-notifier/http_client.rb
bearychat-notifier-0.0.5 lib/bearychat-notifier/http_client.rb