class Emarsys::Broadcast::HTTP

Public Class Methods

new(config) click to toggle source
# File lib/emarsys/broadcast/http.rb, line 7
def initialize(config)
  validate_config config
  @config = config
end

Public Instance Methods

get(path) click to toggle source
# File lib/emarsys/broadcast/http.rb, line 20
def get(path)
  request(path, nil, :get)
end
post(path, xml) click to toggle source
# File lib/emarsys/broadcast/http.rb, line 12
def post(path, xml)
  request(path, xml, :post)
end
put(path, xml) click to toggle source
# File lib/emarsys/broadcast/http.rb, line 16
def put(path, xml)
  request(path, xml, :put)
end

Private Instance Methods

request(path, data, method) click to toggle source
# File lib/emarsys/broadcast/http.rb, line 27
def request(path, data, method)
  https = Net::HTTP.new(@config.api_host, Net::HTTP.https_default_port)
  https.read_timeout = @config.api_timeout
  https.use_ssl = true
  https.verify_mode = OpenSSL::SSL::VERIFY_NONE

  https.start do |http|
    case method.downcase.to_sym
    when :post
      req = Net::HTTP::Post.new(path)
    when :put
      req = Net::HTTP::Put.new(path)
    when :get
      req = Net::HTTP::Get.new(path)
    end
    req.basic_auth(@config.api_user, @config.api_password)
    req.body = data
    req.content_type = "application/xml"

    res = http.request(req)

    case res
    when Net::HTTPSuccess
      return res.body
    else
      puts res.body
      res.error!
    end
  end
end
validate_config(config) click to toggle source
# File lib/emarsys/broadcast/http.rb, line 58
def validate_config(config)
  raise ConfigurationError, 'configuration is nil, did you forget to configure the gem?' unless config
  raise ConfigurationError, 'api_host must be configured' unless string_present? config.api_host
  raise ConfigurationError, 'api_user must be configured' unless string_present? config.api_user
  raise ConfigurationError, 'api_password must be configured' unless string_present? config.api_password
  unless within_range? config.api_port, 1..65535
    raise ConfigurationError, 'api_port must be integer between 1 and 65535' 
  end
end