Sha256: f8431a58ce4f33fe5dfd74e1d2a5e7fc651fa1ad236fc3d0383e352c1a84c151

Contents?: true

Size: 1.15 KB

Versions: 3

Compression:

Stored size: 1.15 KB

Contents

require 'net/http'
require 'cgi'

module Kotsms2
  module Network
    def get(host, request_uri, params = {})
      uri = URI('https://' + host + request_uri)
      uri.query = query_string(params.merge(username: @username, password: @password))

      message = Net::HTTP::Get.new(uri.request_uri)
      message['User-Agent'] = @user_agent

      parse(request(uri, message), host)
    end

    def parse(http_response, host)
      case http_response
      when Net::HTTPSuccess
        http_response.body
      when Net::HTTPClientError
        raise ClientError, "#{http_response.code} response from #{host}"
      when Net::HTTPServerError
        raise ServerError, "#{http_response.code} response from #{host}"
      else
        raise Error, "#{http_response.code} response from #{host}"
      end
    end

    def request(uri, message)
      http = Net::HTTP.new(uri.host, Net::HTTP.https_default_port)
      http.use_ssl = true
      http.request(message)
    end

    def query_string(params)
      params.flat_map { |k, vs| Array(vs).map { |v| "#{escape(k)}=#{escape(v)}" } }.join('&')
    end

    def escape(component)
      CGI.escape(component.to_s)
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
kotsms2-1.0.1 lib/kotsms2/network.rb
kotsms2-1.0.0 lib/kotsms2/network.rb
kotsms2-0.5.0 lib/kotsms2/network.rb