Sha256: e45ea2bccbc2ce9b255072baf018bfdae3146f91926538039fa5888666abdca0

Contents?: true

Size: 1.94 KB

Versions: 1

Compression:

Stored size: 1.94 KB

Contents

require 'net/http'
require 'net/https'
require 'cgi'

module CitrusMail
  class Client
    attr_accessor :api_key, :encoding

    def initialize(api_key, options={})
      @api_key = api_key
      @encoding = options[:encoding] if options[:encoding]
    end

    def get_list(key)
      List.new(self, key)
    end

    #actions = [modify, get_empty_fields, change_state, confirm, remove, add_subscriber]
    def path_for(action)
      "/main.php?moduleName=fm_api&action=#{action}"
    end

    def connection
      @connection ||= begin
        uri = URI.parse(HOST)
        connection = Net::HTTP.new(uri.host, uri.port)
        if uri.scheme == "https" # enable SSL/TLS
          connection.use_ssl = true
          connection.verify_mode = OpenSSL::SSL::VERIFY_NONE
        end
        connection
      end
    end

    def http_get(url, params)
      response = connection.start do |http|
        url_with_params = url + "&" + process_params(params).map { |k, v| "#{urlencode(k.to_s)}=#{urlencode(v.to_s)}" }.join('&')
        get = Net::HTTP::Get.new(url_with_params)
        http.request(get)
      end
      Net::HTTPOK
      CitrusMail::Response.build_from_http_response(response)
    end

    def http_post(path, params)
      response = connection.start do |http|
        post = Net::HTTP::Post.new(path)
        post.set_form_data(process_params(params))
        http.request(post)
      end
      CitrusMail::Response.build_from_http_response(response)
    end

    def process_params(params)
      hash = {:api_key => api_key}
      hash[:encoding] = @encoding if @encoding
      hash.merge!(params)
      flatten_params(hash)
    end

    def flatten_params(params)
      params.inject({}) do |h, (key, value)|
        if value.is_a?(Hash)
          value.each_pair { |k, v| h["#{key}[#{k}]"] = v.to_s }
        else
          h[key] = value.to_s
        end
        h
      end
    end

    def urlencode(str)
      CGI.escape(str).gsub('+', '%20')
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
citrus-mail-0.2.1 lib/citrus_mail/client.rb