Class: MarketingAPI

Inherits:
Object
  • Object
show all
Defined in:
lib/marketing_api.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(server = "web.ict.kth.se/~chyrkov", port = 80) ⇒ MarketingAPI

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



10
11
12
13
# File 'lib/marketing_api.rb', line 10

def initialize(server="web.ict.kth.se/~chyrkov", port=80)
	@server = server
	@port = port
end

Instance Attribute Details

#portObject

Returns the value of attribute port.



6
7
8
# File 'lib/marketing_api.rb', line 6

def port
  @port
end

#serverObject

Returns the value of attribute server.



6
7
8
# File 'lib/marketing_api.rb', line 6

def server
  @server
end

Instance Method Details

#create_optin(json_string) ⇒ Object

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

+json_string

JSON to turn into an optin



44
45
46
47
48
49
50
51
52
53
54
55
56
# 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) ⇒ Object

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



80
81
82
83
84
85
86
87
88
89
90
91
# 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) ⇒ Object

Gets an optin Params:

+id

ID of the optin in the DB

Returns optin as JSON if it is found, nil otherwise



27
28
29
30
31
32
33
34
35
36
37
38
# 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) ⇒ Object

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



62
63
64
65
66
67
68
69
70
71
72
73
74
# 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?Boolean

Validates URI based on @server and @port

Returns:

  • (Boolean)


16
17
18
19
20
21
# File 'lib/marketing_api.rb', line 16

def uri_valid?
	URI.parse("http://#{@server}:#{@port}")
	return true
	rescue URI::InvalidURIError => err
		return nil
end