marketing_api 0.0.2 → 0.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/marketing_api.rb +39 -2
- data/marketing_api.gemspec +1 -1
- metadata +1 -2
- data/marketing_api-0.0.1.gem +0 -0
data/lib/marketing_api.rb
CHANGED
@@ -5,14 +5,30 @@ class MarketingAPI
|
|
5
5
|
|
6
6
|
attr_accessor :server, :port
|
7
7
|
|
8
|
+
# Initializes the API with optional server and port
|
9
|
+
# The protocol is HTTP and it cannot be changed
|
8
10
|
def initialize(server="web.ict.kth.se/~chyrkov", port=80)
|
9
11
|
@server = server
|
10
12
|
@port = port
|
11
13
|
end
|
12
14
|
|
13
|
-
#
|
15
|
+
# Validates URI based on @server and @port
|
16
|
+
def uri_valid?
|
17
|
+
URI.parse("http://#{@server}:#{@port}")
|
18
|
+
return true
|
19
|
+
rescue URI::InvalidURIError => err
|
20
|
+
return nil
|
21
|
+
end
|
22
|
+
|
23
|
+
# Gets an optin
|
24
|
+
# Params:
|
25
|
+
# +id:: ID of the optin in the DB
|
26
|
+
# Returns optin as JSON if it is found, nil otherwise
|
14
27
|
def get_optin(id)
|
15
|
-
|
28
|
+
if !uri_valid?
|
29
|
+
return nil
|
30
|
+
end
|
31
|
+
uri = URI.parse("http://#{@server}:#{@port}/#{id }")
|
16
32
|
response = Net::HTTP.get_response(uri)
|
17
33
|
if response.code == "200"
|
18
34
|
return response.body
|
@@ -21,7 +37,14 @@ class MarketingAPI
|
|
21
37
|
end
|
22
38
|
end
|
23
39
|
|
40
|
+
# Creates an optin from JSON
|
41
|
+
# Returns true if it is created, nil otherwise
|
42
|
+
# Params:
|
43
|
+
# +json_string:: JSON to turn into an optin
|
24
44
|
def create_optin(json_string)
|
45
|
+
if !uri_valid?
|
46
|
+
return nil
|
47
|
+
end
|
25
48
|
req = Net::HTTP::Post.new("/create", initheader = {'Content-Type' =>'application/json'})
|
26
49
|
req.body = json_string
|
27
50
|
response = Net::HTTP.new(@server, @port).start {|http| http.request(req) }
|
@@ -32,7 +55,14 @@ class MarketingAPI
|
|
32
55
|
end
|
33
56
|
end
|
34
57
|
|
58
|
+
# Updates an optin from JSON
|
59
|
+
# Returns true if it is updated, nil if optin is not found or update failed
|
60
|
+
# Params:
|
61
|
+
# +json_string:: JSON to turn into an optin
|
35
62
|
def update_optin(json_string)
|
63
|
+
if !uri_valid?
|
64
|
+
return nil
|
65
|
+
end
|
36
66
|
req = Net::HTTP::Post.new("/update", initheader = {'Content-Type' =>'application/json'})
|
37
67
|
req.body = json_string
|
38
68
|
response = Net::HTTP.new(@server, @port).start {|http| http.request(req) }
|
@@ -43,7 +73,14 @@ class MarketingAPI
|
|
43
73
|
end
|
44
74
|
end
|
45
75
|
|
76
|
+
# Deactivates an optin
|
77
|
+
# Params:
|
78
|
+
# +id:: ID of the optin in the DB
|
79
|
+
# Returns true if optin is deactivated, nil if optin is not found or deactivation failed
|
46
80
|
def deactivate_optin(id)
|
81
|
+
if !uri_valid?
|
82
|
+
return nil
|
83
|
+
end
|
47
84
|
req = Net::HTTP::Post.new("/deactivate/#{id}", initheader = {'Content-Type' =>'application/json'})
|
48
85
|
response = Net::HTTP.new(@server, @port).start {|http| http.request(req) }
|
49
86
|
if response.code == "200"
|
data/marketing_api.gemspec
CHANGED
@@ -3,7 +3,7 @@ require 'marketing_api'
|
|
3
3
|
|
4
4
|
Gem::Specification.new do |s|
|
5
5
|
s.name = 'marketing_api'
|
6
|
-
s.version = '0.0.
|
6
|
+
s.version = '0.0.3'
|
7
7
|
s.date = '2013-07-02'
|
8
8
|
s.summary = "Working with Marketing class by means of remote API"
|
9
9
|
s.description = "Test task given by Alexander Simonov"
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: marketing_api
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -72,7 +72,6 @@ files:
|
|
72
72
|
- Rakefile
|
73
73
|
- bin/marketing_api.rb
|
74
74
|
- lib/marketing_api.rb
|
75
|
-
- marketing_api-0.0.1.gem
|
76
75
|
- marketing_api.gemspec
|
77
76
|
- spec/api_spec.rb
|
78
77
|
- spec/spec_helper.rb
|
data/marketing_api-0.0.1.gem
DELETED
Binary file
|