Sha256: 4a88b2113c6ed0f3bcb9bd923cacaed44f7f7ea374f52795c93e67cda65503ed

Contents?: true

Size: 1.36 KB

Versions: 6

Compression:

Stored size: 1.36 KB

Contents

module EROI
  module Request
    class Get
      API_URL = 'http://emailer.emailroi.com/dbadmin/xml_retrieve2.pl'

      def self.api_available?
        url = URI.parse(Request::Get::API_URL)
        request = Net::HTTP::Get.new(url.path)
        response = Net::HTTP.start(url.host, url.port) { |http| http.request(request) }
        response.class == Net::HTTPOK
      end

      def self.send(client, fields)
        uri = URI.parse(API_URL)
        uri.query = fields.merge({
          :user_token => client.user_token,
          :api_password => client.api_password }).collect { |k,v| "#{k}=#{v}" }.join('&')
        Response::Get.new(Crack::XML.parse(Net::HTTP.get(uri)))
      end
    end

    class Post
      API_URL = 'http://emailer.emailroi.com/dbadmin/xml_post.pl'

      def self.api_available?
        url = URI.parse(Request::Post::API_URL)
        request = Net::HTTP::Get.new(url.path)
        response = Net::HTTP.start(url.host, url.port) { |http| http.request(request) }
        response.class == Net::HTTPOK
      end

      def self.send(client, xml)
        response = Net::HTTP.post_form(
           URI.parse(API_URL),
           { :user_token => client.user_token,
             :api_password => client.api_password,
             :xml_body => xml }).body
        Response::Post.new(Crack::XML.parse("<Response>#{response}</Response>"))
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
eroi-0.2.1 lib/eroi/request.rb
eroi-0.2.0 lib/eroi/request.rb
eroi-0.1.4 lib/eroi/request.rb
eroi-0.1.3 lib/eroi/request.rb
eroi-0.1.2 lib/eroi/request.rb
eroi-0.1.1 lib/eroi/request.rb