class MarketingAPI

Attributes

port[RW]
server[RW]

Public Class Methods

new(server="web.ict.kth.se/~chyrkov", port=80) click to toggle source

Initializes the API with optional server and port The protocol is HTTP and it cannot be changed

# File lib/marketing_api.rb, line 10
def initialize(server="web.ict.kth.se/~chyrkov", port=80)
        @server = server
        @port = port
end

Public Instance Methods

create_optin(json_string) click to toggle source

Creates an optin from JSON Returns true if it is created, nil otherwise Params:

+json_string

JSON to turn into an optin

# File lib/marketing_api.rb, line 44
def create_optin(json_string)
        if !uri_valid?
                return nil
        end
        req = Net::HTTP::Post.new("/create", initheader = {'Content-Type' =>'application/json'})
        req.body = json_string
        response = Net::HTTP.new(@server, @port).start {|http| http.request(req) }
        if response.code == "200"
                return true
        else
                return nil
        end
end
deactivate_optin(id) click to toggle source

Deactivates an optin Params:

+id

ID of the optin in the DB

Returns true if optin is deactivated, nil if optin is not found or deactivation failed

# File lib/marketing_api.rb, line 80
def deactivate_optin(id)
        if !uri_valid?
                return nil
        end
        req = Net::HTTP::Post.new("/deactivate/#{id}", initheader = {'Content-Type' =>'application/json'})
        response = Net::HTTP.new(@server, @port).start {|http| http.request(req) }
        if response.code == "200"
                return true
        else
                return nil
        end
end
get_optin(id) click to toggle source

Gets an optin Params:

+id

ID of the optin in the DB

Returns optin as JSON if it is found, nil otherwise

# File lib/marketing_api.rb, line 27
def get_optin(id)
        if !uri_valid?
                return nil
        end
        uri = URI.parse("http://#{@server}:#{@port}/#{id }")
        response = Net::HTTP.get_response(uri)
        if response.code == "200"
                return response.body
        else
                return nil
        end
end
update_optin(json_string) click to toggle source

Updates an optin from JSON Returns true if it is updated, nil if optin is not found or update failed Params:

+json_string

JSON to turn into an optin

# File lib/marketing_api.rb, line 62
def update_optin(json_string)
        if !uri_valid?
                return nil
        end
        req = Net::HTTP::Post.new("/update", initheader = {'Content-Type' =>'application/json'})
        req.body = json_string
        response = Net::HTTP.new(@server, @port).start {|http| http.request(req) }
        if response.code == "200"
                return true
        else
                return nil
        end
end
uri_valid?() click to toggle source

Validates URI based on @server and @port

# File lib/marketing_api.rb, line 16
def uri_valid?
        URI.parse("http://#{@server}:#{@port}")
        return true
        rescue URI::InvalidURIError => err
                return nil
end