marketing_api 0.0.1

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/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+ gemspec
3
+
4
+
data/Gemfile.lock ADDED
@@ -0,0 +1,34 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ marketing_api (0.0.1)
5
+ json
6
+
7
+ GEM
8
+ remote: http://rubygems.org/
9
+ specs:
10
+ addressable (2.3.5)
11
+ crack (0.4.0)
12
+ safe_yaml (~> 0.9.0)
13
+ diff-lcs (1.2.4)
14
+ json (1.8.0)
15
+ rspec (2.13.0)
16
+ rspec-core (~> 2.13.0)
17
+ rspec-expectations (~> 2.13.0)
18
+ rspec-mocks (~> 2.13.0)
19
+ rspec-core (2.13.1)
20
+ rspec-expectations (2.13.0)
21
+ diff-lcs (>= 1.1.3, < 2.0)
22
+ rspec-mocks (2.13.1)
23
+ safe_yaml (0.9.3)
24
+ webmock (1.12.3)
25
+ addressable (>= 2.2.7)
26
+ crack (>= 0.3.2)
27
+
28
+ PLATFORMS
29
+ ruby
30
+
31
+ DEPENDENCIES
32
+ marketing_api!
33
+ rspec (~> 2.13.0)
34
+ webmock
data/README.md ADDED
@@ -0,0 +1,41 @@
1
+ marketing_api_gem
2
+ =================
3
+
4
+ The gem is used as a remote API (using Net::HTTP) for the service called marketing (https://github.com/fess89/marketing).
5
+
6
+ Installation
7
+ ------------
8
+
9
+ mkdir marketing_api_gem
10
+
11
+ cd marketing_api_gem
12
+
13
+ git clone https://github.com/fess89/marketing_api_gem .
14
+
15
+ rake build
16
+
17
+ Installation for testing
18
+ ------------------------
19
+ mkdir marketing_api_gem
20
+
21
+ cd marketing_api_gem
22
+
23
+ git clone https://github.com/fess89/marketing_api_gem .
24
+
25
+ Then alternatively:
26
+
27
+ rake dev (this doesn't work for me)
28
+
29
+ or
30
+
31
+ gem install rspec
32
+
33
+ gem install webmock
34
+
35
+ rake build
36
+
37
+ rake (this runs tests)
38
+
39
+
40
+
41
+
data/Rakefile ADDED
@@ -0,0 +1,28 @@
1
+ #gem installation
2
+ task :build do
3
+ #getting gem info
4
+ gemspec = eval(File.read("marketing_api.gemspec"))
5
+ system "gem build marketing_api.gemspec"
6
+ #system "gem install marketing_api-#{gemspec.version.to_s}.gem --no-ri --no-rdoc"
7
+ end
8
+
9
+ #installation for development purposes
10
+ task :dev do
11
+ #getting gem info
12
+ gemspec = eval(File.read("marketing_api.gemspec"))
13
+ system "gem build marketing_api.gemspec"
14
+ system "gem install marketing_api-#{gemspec.version.to_s}.gem --no-ri --no-rdoc --development -V"
15
+ end
16
+
17
+ #by default, rake runs tests
18
+ desc 'Default: run specs.'
19
+ task :default => :spec
20
+
21
+ desc "Run specs"
22
+ require "rspec/core/rake_task"
23
+ RSpec::Core::RakeTask.new do |task|
24
+ task.pattern = "spec/*_spec.rb"
25
+ task.rspec_opts = Dir.glob("[0-9][0-9][0-9]_*").collect { |x| "-I#{x}" }.sort
26
+ task.rspec_opts << '--color'
27
+ task.rspec_opts << '-f documentation'
28
+ end
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ begin
4
+ require 'marketing_api'
5
+ rescue LoadError
6
+ require 'rubygems'
7
+ require 'marketing_api'
8
+ end
@@ -0,0 +1,55 @@
1
+ require 'net/http'
2
+ require 'uri'
3
+
4
+ class MarketingAPI
5
+
6
+ attr_accessor :server, :port
7
+
8
+ def initialize(server="web.ict.kth.se/~chyrkov", port=80)
9
+ @server = server
10
+ @port = port
11
+ end
12
+
13
+ #it is easy to go with get
14
+ def get_optin(id)
15
+ uri = URI.parse("http://web.ict.kth.se/~chyrkov:80/#{id}")
16
+ response = Net::HTTP.get_response(uri)
17
+ if response.code == "200"
18
+ return response.body
19
+ else
20
+ return nil
21
+ end
22
+ end
23
+
24
+ def create_optin(json_string)
25
+ req = Net::HTTP::Post.new("/create", initheader = {'Content-Type' =>'application/json'})
26
+ req.body = json_string
27
+ response = Net::HTTP.new(@server, @port).start {|http| http.request(req) }
28
+ if response.code == "200"
29
+ return true
30
+ else
31
+ return nil
32
+ end
33
+ end
34
+
35
+ def update_optin(json_string)
36
+ req = Net::HTTP::Post.new("/update", initheader = {'Content-Type' =>'application/json'})
37
+ req.body = json_string
38
+ response = Net::HTTP.new(@server, @port).start {|http| http.request(req) }
39
+ if response.code == "200"
40
+ return true
41
+ else
42
+ return nil
43
+ end
44
+ end
45
+
46
+ def deactivate_optin(id)
47
+ req = Net::HTTP::Post.new("/deactivate/#{id}", initheader = {'Content-Type' =>'application/json'})
48
+ response = Net::HTTP.new(@server, @port).start {|http| http.request(req) }
49
+ if response.code == "200"
50
+ return true
51
+ else
52
+ return nil
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,30 @@
1
+ $:.push File.expand_path("../lib", __FILE__)
2
+ require 'marketing_api'
3
+
4
+ Gem::Specification.new do |s|
5
+ s.name = 'marketing_api'
6
+ s.version = '0.0.1'
7
+ s.date = '2013-07-02'
8
+ s.summary = "Working with Marketing class by means of remote API"
9
+ s.description = "Test task given by Alexander Simonov"
10
+ s.authors = ["Oleksii Chyrkov"]
11
+ s.email = 'chyrkov@kth.se'
12
+ s.homepage = ""
13
+
14
+ s.files = `git ls-files`.split("\n")
15
+ s.test_files = `git ls-files -- spec/*`.split("\n")
16
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
17
+ s.require_paths = ["lib"]
18
+
19
+ s.license = "MIT"
20
+ s.platform = Gem::Platform::RUBY
21
+
22
+ #json is needed to interact with the remote API
23
+ s.add_runtime_dependency "json"
24
+
25
+ #rspec is only for testing
26
+ s.add_development_dependency "rspec", "~> 2.13.0"
27
+
28
+ #so is webmock
29
+ s.add_development_dependency "webmock"
30
+ end
data/spec/api_spec.rb ADDED
@@ -0,0 +1,184 @@
1
+ require 'webmock/rspec'
2
+ require 'json'
3
+ require 'spec_helper'
4
+ require 'marketing_api'
5
+
6
+ describe MarketingAPI, "#marketing_remote_api" do
7
+
8
+ api = MarketingAPI.new
9
+ WebMock.disable_net_connect!
10
+
11
+ success_reply =
12
+ {
13
+ "id"=>21, "email"=>"chyrkov@kth.se",
14
+ "mobile"=>"380632721593", "first_name"=>"Oleksii",
15
+ "last_name"=>"Chyrkov", "permission_type"=>"one-time",
16
+ "channel"=>"sms", "company_name"=>"Apple",
17
+ "message"=>"Found optin", "code"=>200
18
+ }
19
+
20
+ not_found_reply =
21
+ {
22
+ "code"=> 404,
23
+ "message"=>"Optin not found"
24
+ }
25
+
26
+ bad_request_reply =
27
+ {
28
+ "code"=> 401,
29
+ "message"=>"Bad request"
30
+ }
31
+
32
+ already_exists_reply =
33
+ {
34
+ "code"=> 302,
35
+ "message"=>"Bad request"
36
+ }
37
+
38
+ params =
39
+ {
40
+ :email => "chyrkov@kth.se",
41
+ :mobile => "380632721593",
42
+ :company_name => "Apple",
43
+ :first_name => "Oleksii",
44
+ :last_name => "Chyrkov",
45
+ :permission_type => "one-time",
46
+ :channel => "sms"
47
+ }
48
+
49
+ #TODO: return JSON here
50
+ it "should return valid JSON when getting an existing Optin" do
51
+ id = rand(10)
52
+ stub_request(:get, "#{api.server}:#{api.port}/#{id}").to_return(
53
+ :status => 200,
54
+ :body => success_reply)
55
+ json = api.get_optin(id).to_json
56
+ #JSON validation is defined in the helper
57
+ json.valid_json?.should eq(true)
58
+ end
59
+
60
+ it "should return true when creating an Optin with good params" do
61
+ stub_request(:post, api.server+":80/create").with(:body => params.to_json)
62
+ .to_return(:status => 200, :body => success_reply)
63
+ api.create_optin(params.to_json).should eq(true)
64
+ end
65
+
66
+ it "should return true when updating an existing Optin" do
67
+ params[:id] = rand(10)
68
+ stub_request(:post, api.server+":80/update").with(:body => params.to_json)
69
+ .to_return(:status => 200,
70
+ :body => success_reply)
71
+ api.update_optin(params.to_json).should eq(true)
72
+ end
73
+
74
+ it "should return true when deactivating an existing Optin" do
75
+ id = rand(10)
76
+ success_reply =
77
+ {
78
+ :message => "Optin with id #{id} deactivated",
79
+ :code => 200
80
+ }
81
+ stub_request(:post, api.server+":80/deactivate/#{id}").to_return(:status => 200,
82
+ :body => success_reply)
83
+ api.deactivate_optin(id).should eq(true)
84
+ end
85
+
86
+ it "should return nil when creating from invalid JSON" do
87
+ #random string instead of JSON request
88
+ request = (0..60).map{ ('a'..'z').to_a[rand(26)] }.join
89
+ stub_request(:post, api.server+":80/create").with(:body => request)
90
+ .to_return(:status => 503,
91
+ :body => bad_request_reply)
92
+ api.create_optin(request).should eq(nil)
93
+ end
94
+
95
+ it "should return nil when updating from invalid JSON" do
96
+ #random string instead of JSON request
97
+ request = (0..60).map{ ('a'..'z').to_a[rand(26)] }.join
98
+ stub_request(:post, api.server+":80/update").with(:body => request)
99
+ .to_return(:status => 503,
100
+ :body => bad_request_reply)
101
+ api.update_optin(request).should eq(nil)
102
+ end
103
+
104
+ it "should return nil when creating an Optin with no email" do
105
+ params[:email] = ""
106
+ stub_request(:post, api.server+":80/create").with(:body => params.to_json)
107
+ .to_return(:status => 404, :body => bad_request_reply)
108
+ api.create_optin(params.to_json).should eq(nil)
109
+ end
110
+
111
+ it "should return nil when creating an Optin with no company name" do
112
+ params[:company_name] = ""
113
+ stub_request(:post, api.server+":80/create").with(:body => params.to_json)
114
+ .to_return(:status => 404, :body => bad_request_reply)
115
+ api.create_optin(params.to_json).should eq(nil)
116
+ end
117
+
118
+
119
+ it "should return nil when creating an Optin with no first name" do
120
+ params[:first_name] = nil
121
+ stub_request(:post, api.server+":80/create").with(:body => params.to_json)
122
+ .to_return(:status => 404, :body => bad_request_reply)
123
+ api.create_optin(params.to_json).should eq(nil)
124
+ end
125
+
126
+ it "should return nil when creating an Optin with no last name" do
127
+ params[:last_name] = nil
128
+ stub_request(:post, api.server+":80/create").with(:body => params.to_json)
129
+ .to_return(:status => 404, :body => bad_request_reply)
130
+ api.create_optin(params.to_json).should eq(nil)
131
+ end
132
+
133
+ it "should return nil when creating an Optin with wrong permission type" do
134
+ #random string in permission type
135
+ params[:permission_type] = (0..9).map{ ('a'..'z').to_a[rand(26)] }.join
136
+ stub_request(:post, api.server+":80/create").with(:body => params.to_json)
137
+ .to_return(:status => 404, :body => bad_request_reply)
138
+ api.create_optin(params.to_json).should eq(nil)
139
+ end
140
+
141
+ it "should return nil when creating an Optin with wrong channel" do
142
+ #random string in channel
143
+ params[:channel] = (0..9).map{ ('a'..'z').to_a[rand(26)] }.join
144
+ stub_request(:post, api.server+":80/create").with(:body => params.to_json)
145
+ .to_return(:status => 404, :body => bad_request_reply)
146
+ api.create_optin(params.to_json).should eq(nil)
147
+ end
148
+
149
+ it "should return nil when getting a non-existent Optin" do
150
+ #IDs so big probably do not exist
151
+ id = rand(30000..40000)
152
+ stub_request(:get, "#{api.server}:#{api.port}/#{id}").to_return(
153
+ :status => 404,
154
+ :body => not_found_reply)
155
+ api.get_optin(id).should eq(nil)
156
+ end
157
+
158
+ it "should return nil when updating a non-existent Optin" do
159
+ params[:id] = rand(30000..40000).to_s
160
+ stub_request(:post, api.server+":80/update").with(:body => params.to_json)
161
+ .to_return(:status => 404,
162
+ :body => not_found_reply)
163
+ api.update_optin(params.to_json).should eq(nil)
164
+ end
165
+
166
+ it "should return nil when deactivating a non-existent Optin" do
167
+ id = rand(30000..40000)
168
+ stub_request(:post, api.server+":80/deactivate/#{id}").to_return(:status => 404,
169
+ :body => not_found_reply)
170
+ api.deactivate_optin(id).should eq(nil)
171
+ end
172
+
173
+ it "should return nil when creating a duplicate company/channel pair Optin" do
174
+ params[:id] = rand(10)
175
+ stub_request(:post, api.server+":80/update").with(:body => params.to_json)
176
+ .to_return(:status => 200, :body => success_reply)
177
+ api.update_optin(params.to_json).should eq(true)
178
+ stub_request(:post, api.server+":80/update").with(:body => params.to_json)
179
+ .to_return(:status => 302,
180
+ :body => already_exists_reply)
181
+ api.update_optin(params.to_json).should eq(nil)
182
+ end
183
+
184
+ end
@@ -0,0 +1,9 @@
1
+ class String
2
+ def valid_json?
3
+ JSON.parse(self)
4
+ return true
5
+ rescue JSON::ParserError
6
+ return false
7
+ end
8
+ end
9
+
metadata ADDED
@@ -0,0 +1,105 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: marketing_api
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Oleksii Chyrkov
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-07-02 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: json
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rspec
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: 2.13.0
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: 2.13.0
46
+ - !ruby/object:Gem::Dependency
47
+ name: webmock
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ description: Test task given by Alexander Simonov
63
+ email: chyrkov@kth.se
64
+ executables:
65
+ - marketing_api.rb
66
+ extensions: []
67
+ extra_rdoc_files: []
68
+ files:
69
+ - Gemfile
70
+ - Gemfile.lock
71
+ - README.md
72
+ - Rakefile
73
+ - bin/marketing_api.rb
74
+ - lib/marketing_api.rb
75
+ - marketing_api.gemspec
76
+ - spec/api_spec.rb
77
+ - spec/spec_helper.rb
78
+ homepage: ''
79
+ licenses:
80
+ - MIT
81
+ post_install_message:
82
+ rdoc_options: []
83
+ require_paths:
84
+ - lib
85
+ required_ruby_version: !ruby/object:Gem::Requirement
86
+ none: false
87
+ requirements:
88
+ - - ! '>='
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ required_rubygems_version: !ruby/object:Gem::Requirement
92
+ none: false
93
+ requirements:
94
+ - - ! '>='
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ requirements: []
98
+ rubyforge_project:
99
+ rubygems_version: 1.8.25
100
+ signing_key:
101
+ specification_version: 3
102
+ summary: Working with Marketing class by means of remote API
103
+ test_files:
104
+ - spec/api_spec.rb
105
+ - spec/spec_helper.rb