mortar-api-ruby 0.1.0

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/mortar/api.rb ADDED
@@ -0,0 +1,132 @@
1
+ #
2
+ # Copyright 2012 Mortar Data Inc.
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+ #
16
+ # Portions of this code from heroku.rb (https://github.com/heroku/heroku.rb/),
17
+ # used under an MIT license
18
+ # (https://github.com/heroku/heroku.rb/blob/56fa8ed4cba0ab7e5785d6df75a9de687237124f/README.md#meta).
19
+ #
20
+
21
+ require "excon"
22
+ require "securerandom"
23
+ require "uri"
24
+ require "zlib"
25
+
26
+ __LIB_DIR__ = File.expand_path(File.join(File.dirname(__FILE__), ".."))
27
+ unless $LOAD_PATH.include?(__LIB_DIR__)
28
+ $LOAD_PATH.unshift(__LIB_DIR__)
29
+ end
30
+
31
+ require "mortar/api/vendor/okjson"
32
+
33
+
34
+ require "mortar/api/basicauth"
35
+ require "mortar/api/errors"
36
+ require "mortar/api/version"
37
+
38
+ require "mortar/api/clusters"
39
+ require "mortar/api/illustrate"
40
+ require "mortar/api/describe"
41
+ require "mortar/api/validate"
42
+ require "mortar/api/jobs"
43
+ require "mortar/api/login"
44
+ require "mortar/api/projects"
45
+ require "mortar/api/user"
46
+ require "mortar/api/tasks"
47
+
48
+ srand
49
+
50
+ module Mortar
51
+ class API
52
+
53
+ include Mortar::API::BasicAuth
54
+
55
+ attr_reader :connection
56
+
57
+ def initialize(options={})
58
+ user = options.delete(:user)
59
+ api_key = options.delete(:api_key) || ENV['MORTAR_API_KEY']
60
+ options = {
61
+ :headers => {},
62
+ :host => 'api.mortardata.com',
63
+ :scheme => 'https'
64
+ }.merge(options)
65
+ options[:headers] = {
66
+ 'Accept' => 'application/json',
67
+ 'Accept-Encoding' => 'gzip',
68
+ 'Content-Type' => 'application/json',
69
+ #'Accept-Language' => 'en-US, en;q=0.8',
70
+ 'Authorization' => basic_auth_authorization_header(user, api_key),
71
+ 'User-Agent' => "mortar-api-ruby/#{Mortar::API::VERSION}",
72
+ 'X-Ruby-Version' => RUBY_VERSION,
73
+ 'X-Ruby-Platform' => RUBY_PLATFORM
74
+ }.merge(options[:headers])
75
+ @connection = Excon.new("#{options[:scheme]}://#{options[:host]}", options)
76
+ end
77
+
78
+ def request(params, &block)
79
+ begin
80
+ response = @connection.request(params, &block)
81
+ rescue Excon::Errors::HTTPStatusError => error
82
+ klass = case error.response.status
83
+ when 401 then Mortar::API::Errors::Unauthorized
84
+ when 402 then Mortar::API::Errors::VerificationRequired
85
+ when 403 then Mortar::API::Errors::Forbidden
86
+ when 404 then Mortar::API::Errors::NotFound
87
+ when 408 then Mortar::API::Errors::Timeout
88
+ when 422 then Mortar::API::Errors::RequestFailed
89
+ when 423 then Mortar::API::Errors::Locked
90
+ when /50./ then Mortar::API::Errors::RequestFailed
91
+ else Mortar::API::Errors::ErrorWithResponse
92
+ end
93
+
94
+ reerror = klass.new(error.message, error.response)
95
+ reerror.set_backtrace(error.backtrace)
96
+ raise(reerror)
97
+ end
98
+
99
+ if response.body && !response.body.empty?
100
+ if response.headers['Content-Encoding'] == 'gzip'
101
+ response.body = Zlib::GzipReader.new(StringIO.new(response.body)).read
102
+ end
103
+ begin
104
+ response.body = Mortar::API::OkJson.decode(response.body)
105
+ rescue
106
+ # leave non-JSON body as is
107
+ end
108
+ end
109
+
110
+ # reset (non-persistent) connection
111
+ @connection.reset
112
+
113
+ response
114
+ end
115
+
116
+ def versioned_path(resource)
117
+ no_slash_resource = resource.start_with?("/") ? resource[1,resource.size] : resource
118
+ "/v#{SERVER_API_VERSION}/#{no_slash_resource}"
119
+ end
120
+
121
+ protected
122
+
123
+ def json_encode(object)
124
+ Mortar::API::OkJson.encode(object)
125
+ end
126
+
127
+ def escape(string)
128
+ CGI.escape(string).gsub('.', '%2E')
129
+ end
130
+
131
+ end
132
+ end
@@ -0,0 +1 @@
1
+ require(File.join(File.dirname(__FILE__), "mortar", "api"))
@@ -0,0 +1,29 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "mortar/api/version"
4
+
5
+ Gem::Specification.new do |gem|
6
+ gem.name = "mortar-api-ruby"
7
+ gem.version = Mortar::API::VERSION
8
+
9
+ gem.author = "Mortar Data"
10
+ gem.email = "support@mortardata.com"
11
+ gem.homepage = "http://mortardata.com/"
12
+ gem.summary = "Client for Mortar API"
13
+ gem.description = "Client for Mortar API."
14
+ gem.platform = Gem::Platform::RUBY
15
+ gem.required_ruby_version = '>=1.8.7'
16
+
17
+ gem.files = `git ls-files`.split("\n")
18
+ gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
+ gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
+ gem.require_paths = ["lib"]
21
+
22
+ gem.add_runtime_dependency 'excon', '~>0.15.4'
23
+
24
+ gem.add_development_dependency 'gem-release'
25
+ gem.add_development_dependency 'rake'
26
+ gem.add_development_dependency "rr"
27
+ gem.add_development_dependency "rspec"
28
+
29
+ end
@@ -0,0 +1,31 @@
1
+ #
2
+ # Copyright 2012 Mortar Data Inc.
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+ #
16
+
17
+ require "spec_helper"
18
+ require "mortar/api/basicauth"
19
+
20
+ describe Mortar::API::BasicAuth do
21
+
22
+ include Mortar::API::BasicAuth
23
+
24
+ context "basicauth" do
25
+ it "gets basic auth headers" do
26
+ user = "foo@bar.com"
27
+ password = "mypassword"
28
+ basic_auth_authorization_header(user, password).should == "Basic Zm9vQGJhci5jb206bXlwYXNzd29yZA=="
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,44 @@
1
+ #
2
+ # Copyright 2012 Mortar Data Inc.
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+ #
16
+
17
+ require "base64"
18
+ require "spec_helper"
19
+ require "mortar/api"
20
+
21
+ describe Mortar::API do
22
+
23
+ before(:each) do
24
+ @api = Mortar::API.new
25
+ end
26
+
27
+ after(:each) do
28
+ Excon.stubs.clear
29
+ end
30
+
31
+ context "clusters" do
32
+
33
+ it "gets recent and running clusters" do
34
+ Excon.stub({:method => :get, :path => "/v2/clusters"}) do |params|
35
+ {:body => Mortar::API::OkJson.encode({"clusters" => [{'cluster_id' => '1', 'status_code' => 'running'}, {'cluster_id' => '2', 'status_code' => 'running'}]}), :status => 200}
36
+ end
37
+ response = @api.get_clusters()
38
+ clusters = response.body["clusters"]
39
+ clusters.nil?.should be_false
40
+ clusters.length.should == 2
41
+ end
42
+
43
+ end
44
+ end
@@ -0,0 +1,62 @@
1
+ #
2
+ # Copyright 2012 Mortar Data Inc.
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+ #
16
+
17
+ require "base64"
18
+ require "spec_helper"
19
+ require "mortar/api"
20
+
21
+
22
+ describe Mortar::API do
23
+
24
+ before(:each) do
25
+ @api = Mortar::API.new
26
+ end
27
+
28
+ after(:each) do
29
+ Excon.stubs.clear
30
+ end
31
+
32
+ context "describe" do
33
+ it "posts a describe" do
34
+ describe_id = "7b93e4d3ab034188a0c2be418d3d24ed"
35
+ project_name = "my_project"
36
+ pigscript_name = "my_pigscript"
37
+ pigscript_alias = "my_alias"
38
+ git_ref = "e20395b8b06fbf52e86665b0660209673f311d1a"
39
+ parameters = {"key" => "value"}
40
+ body = Mortar::API::OkJson.encode({"project_name" => project_name,
41
+ "pigscript_name" => pigscript_name,
42
+ "alias" => pigscript_alias,
43
+ "git_ref" => git_ref,
44
+ "parameters" => parameters})
45
+ Excon.stub({:method => :post, :path => "/v2/describes", :body => body}) do |params|
46
+ {:body => Mortar::API::OkJson.encode({'describe_id' => describe_id}), :status => 200}
47
+ end
48
+ response = @api.post_describe(project_name, pigscript_name, pigscript_alias, git_ref, :parameters => parameters)
49
+ response.body['describe_id'].should == describe_id
50
+ end
51
+
52
+ it "gets a describe" do
53
+ describe_id = "7b93e4d3ab034188a0c2be418d3d24ed"
54
+ Excon.stub({:method => :get, :path => "/v2/describes/7b93e4d3ab034188a0c2be418d3d24ed"}) do |params|
55
+ {:body => Mortar::API::OkJson.encode({'describe_id' => describe_id, 'status' => Mortar::API::Describe::STATUS_PROGRESS}), :status => 200}
56
+ end
57
+ response = @api.get_describe(describe_id)
58
+ response.body['describe_id'].should == describe_id
59
+ response.body['status'].should == Mortar::API::Describe::STATUS_PROGRESS
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,62 @@
1
+ #
2
+ # Copyright 2012 Mortar Data Inc.
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+ #
16
+
17
+ require "base64"
18
+ require "spec_helper"
19
+ require "mortar/api"
20
+
21
+
22
+ describe Mortar::API do
23
+
24
+ before(:each) do
25
+ @api = Mortar::API.new
26
+ end
27
+
28
+ after(:each) do
29
+ Excon.stubs.clear
30
+ end
31
+
32
+ context "illustrate" do
33
+ it "posts an illustrate" do
34
+ illustrate_id = "7b93e4d3ab034188a0c2be418d3d24ed"
35
+ project_name = "my_project"
36
+ pigscript_name = "my_pigscript"
37
+ pigscript_alias = "my_alias"
38
+ git_ref = "e20395b8b06fbf52e86665b0660209673f311d1a"
39
+ parameters = {"key" => "value"}
40
+ body = Mortar::API::OkJson.encode({"project_name" => project_name,
41
+ "pigscript_name" => pigscript_name,
42
+ "alias" => pigscript_alias,
43
+ "git_ref" => git_ref,
44
+ "parameters" => parameters})
45
+ Excon.stub({:method => :post, :path => "/v2/illustrates", :body => body}) do |params|
46
+ {:body => Mortar::API::OkJson.encode({'illustrate_id' => illustrate_id}), :status => 200}
47
+ end
48
+ response = @api.post_illustrate(project_name, pigscript_name, pigscript_alias, git_ref, :parameters => parameters)
49
+ response.body['illustrate_id'].should == illustrate_id
50
+ end
51
+
52
+ it "gets an illustrate" do
53
+ illustrate_id = "7b93e4d3ab034188a0c2be418d3d24ed"
54
+ Excon.stub({:method => :get, :path => "/v2/illustrates/7b93e4d3ab034188a0c2be418d3d24ed"}) do |params|
55
+ {:body => Mortar::API::OkJson.encode({'illustrate_id' => illustrate_id, 'status' => Mortar::API::Illustrate::STATUS_PROGRESS}), :status => 200}
56
+ end
57
+ response = @api.get_illustrate(illustrate_id)
58
+ response.body['illustrate_id'].should == illustrate_id
59
+ response.body['status'].should == Mortar::API::Illustrate::STATUS_PROGRESS
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,112 @@
1
+ #
2
+ # Copyright 2012 Mortar Data Inc.
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+ #
16
+
17
+ require "base64"
18
+ require "spec_helper"
19
+ require "mortar/api"
20
+
21
+
22
+ describe Mortar::API do
23
+
24
+ before(:each) do
25
+ @api = Mortar::API.new
26
+ end
27
+
28
+ after(:each) do
29
+ Excon.stubs.clear
30
+ end
31
+
32
+ context "jobs" do
33
+ it "posts a job for an existing cluster" do
34
+ job_id = "7b93e4d3ab034188a0c2be418d3d24ed"
35
+ project_name = "my_project"
36
+ pigscript_name = "my_pigscript"
37
+ git_ref = "e20395b8b06fbf52e86665b0660209673f311d1a"
38
+ cluster_id = "f82c774f7ccd429e91db996838cb6c4a"
39
+ parameters = {"my_first_param" => 1, "MY_SECOND_PARAM" => "TWO"}
40
+ body = Mortar::API::OkJson.encode({"project_name" => project_name,
41
+ "pigscript_name" => pigscript_name,
42
+ "git_ref" => git_ref,
43
+ "cluster_id" => cluster_id,
44
+ "parameters" => parameters})
45
+ Excon.stub({:method => :post, :path => "/v2/jobs", :body => body}) do |params|
46
+ {:body => Mortar::API::OkJson.encode({'job_id' => job_id}), :status => 200}
47
+ end
48
+ response = @api.post_job_existing_cluster(project_name, pigscript_name, git_ref, cluster_id, :parameters => parameters)
49
+ response.body['job_id'].should == job_id
50
+ end
51
+
52
+ it "posts a job for a new cluster, defaulting to keep_alive of true" do
53
+ job_id = "7b93e4d3ab034188a0c2be418d3d24ed"
54
+ project_name = "my_project"
55
+ pigscript_name = "my_pigscript"
56
+ git_ref = "e20395b8b06fbf52e86665b0660209673f311d1a"
57
+ cluster_size = 5
58
+ body = Mortar::API::OkJson.encode({"project_name" => project_name,
59
+ "pigscript_name" => pigscript_name,
60
+ "git_ref" => git_ref,
61
+ "cluster_size" => cluster_size,
62
+ "keep_alive" => true,
63
+ "parameters" => {}})
64
+ Excon.stub({:method => :post, :path => "/v2/jobs", :body => body}) do |params|
65
+ {:body => Mortar::API::OkJson.encode({'job_id' => job_id}), :status => 200}
66
+ end
67
+ response = @api.post_job_new_cluster(project_name, pigscript_name, git_ref, cluster_size)
68
+ response.body['job_id'].should == job_id
69
+ end
70
+
71
+ it "accepts keep_alive of false" do
72
+ job_id = "7b93e4d3ab034188a0c2be418d3d24ed"
73
+ project_name = "my_project"
74
+ pigscript_name = "my_pigscript"
75
+ git_ref = "e20395b8b06fbf52e86665b0660209673f311d1a"
76
+ cluster_size = 5
77
+ body = Mortar::API::OkJson.encode({"project_name" => project_name,
78
+ "pigscript_name" => pigscript_name,
79
+ "git_ref" => git_ref,
80
+ "cluster_size" => cluster_size,
81
+ "keep_alive" => false,
82
+ "parameters" => {}})
83
+ Excon.stub({:method => :post, :path => "/v2/jobs", :body => body}) do |params|
84
+ {:body => Mortar::API::OkJson.encode({'job_id' => job_id}), :status => 200}
85
+ end
86
+ response = @api.post_job_new_cluster(project_name, pigscript_name, git_ref, cluster_size, :keepalive => false)
87
+ response.body['job_id'].should == job_id
88
+ end
89
+
90
+ it "gets a job" do
91
+ job_id = "7b93e4d3ab034188a0c2be418d3d24ed"
92
+ status = Mortar::API::Jobs::STATUS_RUNNING
93
+ Excon.stub({:method => :get, :path => "/v2/jobs/7b93e4d3ab034188a0c2be418d3d24ed"}) do |params|
94
+ {:body => Mortar::API::OkJson.encode({'job_id' => job_id, 'status' => status}), :status => 200}
95
+ end
96
+ response = @api.get_job(job_id)
97
+ response.body['job_id'].should == job_id
98
+ response.body['status'].should == status
99
+ end
100
+
101
+ it "gets recent and running jobs" do
102
+ Excon.stub({:method => :get, :path => "/v2/jobs"}) do |params|
103
+ {:body => Mortar::API::OkJson.encode({"jobs" => [{'job_id' => '1', 'status' => 'running'}, {'job_id' => '2', 'status' => 'running'}]}), :status => 200}
104
+ end
105
+ response = @api.get_jobs(0, 10)
106
+ jobs = response.body["jobs"]
107
+ jobs.nil?.should be_false
108
+ jobs.length.should == 2
109
+ end
110
+
111
+ end
112
+ end
@@ -0,0 +1,53 @@
1
+ #
2
+ # Copyright 2012 Mortar Data Inc.
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+ #
16
+
17
+ require "spec_helper"
18
+ require "mortar/api"
19
+
20
+ describe Mortar::API do
21
+
22
+ include Mortar::API::BasicAuth
23
+
24
+ before(:each) do
25
+ @api = Mortar::API.new
26
+ end
27
+
28
+ after(:each) do
29
+ Excon.stubs.clear
30
+ end
31
+
32
+ context "login" do
33
+ it "logs in successfully" do
34
+ email = "fake@nowhere.com"
35
+ password = "fakepass"
36
+ api_key = "6db6Wm9ZUeCl0NVNdkhptksCh0T9i6bv1dYZXaKz"
37
+ Excon.stub({:method => :post, :path => "/v2/login"}) do |params|
38
+ params[:headers]["Authorization"].should == basic_auth_authorization_header(email, password)
39
+ {:body => Mortar::API::OkJson.encode({"api_key" => api_key}), :status => 200}
40
+ end
41
+ response = @api.post_login(email, password)
42
+ response.body["api_key"].should == api_key
43
+ end
44
+
45
+ it "fails login with bad api_key" do
46
+ Excon.stub({:method => :post, :path => "/v2/login"}) do |params|
47
+ {:status => 401}
48
+ end
49
+ expect {@api.post_login("wronguser@fake.org", "wrongpass")}.to raise_error(Mortar::API::Errors::Unauthorized)
50
+ end
51
+
52
+ end
53
+ end
@@ -0,0 +1,83 @@
1
+ #
2
+ # Copyright 2012 Mortar Data Inc.
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+ #
16
+
17
+ require "base64"
18
+ require "spec_helper"
19
+ require "mortar/api"
20
+
21
+
22
+ describe Mortar::API do
23
+
24
+ before(:each) do
25
+ @api = Mortar::API.new
26
+ end
27
+
28
+ after(:each) do
29
+ Excon.stubs.clear
30
+ end
31
+
32
+ context "projects" do
33
+
34
+ it "gets all projects" do
35
+ project1 = {'name' => "Project 1",
36
+ 'status' => Mortar::API::Projects::STATUS_ACTIVE,
37
+ 'github_repo_name' => "Project-1"}
38
+ project2 = {'name' => "Project 2",
39
+ 'status' => Mortar::API::Projects::STATUS_ACTIVE,
40
+ 'github_repo_name' => "Project-2"}
41
+
42
+ Excon.stub({:method => :get, :path => "/v2/projects"}) do |params|
43
+ {:body => Mortar::API::OkJson.encode({'projects' => [project1, project2]}),
44
+ :status => 200}
45
+ end
46
+ response = @api.get_projects
47
+ response.body['projects'][0]['name'].should == project1['name']
48
+ response.body['projects'][1]['name'].should == project2['name']
49
+ response.body['projects'][0]['status'].should == project1['status']
50
+ response.body['projects'][1]['status'].should == project2['status']
51
+ end
52
+
53
+ it "gets a project" do
54
+ project_id = "7b93e4d3ab034188a0c2be418d3d24ed"
55
+ project1 = {'name' => "Project 1",
56
+ 'status' => Mortar::API::Projects::STATUS_ACTIVE,
57
+ 'github_repo_name' => "Project-1",
58
+ '_id' => project_id}
59
+
60
+ Excon.stub({:method => :get, :path => "/v2/projects/%s" % project_id}) do |params|
61
+ {:body => Mortar::API::OkJson.encode(project1),
62
+ :status => 200}
63
+ end
64
+ response = @api.get_project(project_id)
65
+ response.body['name'].should == project1['name']
66
+ response.body['status'].should == project1['status']
67
+ response.body['github_repo_name'].should == project1['github_repo_name']
68
+ end
69
+
70
+ it "posts a project" do
71
+ project_name = "my_new_project"
72
+ project_id = "7b93e4d3ab034188a0c2be418d3d24ed"
73
+ body = Mortar::API::OkJson.encode({"project_name" => project_name})
74
+ Excon.stub({:method => :post, :path => "/v2/projects", :body => body}) do |params|
75
+ {:body => Mortar::API::OkJson.encode({'project_name' => project_name, "project_id" => project_id}), :status => 200}
76
+ end
77
+ response = @api.post_project(project_name)
78
+ response.body['project_id'].should == project_id
79
+ response.body['project_name'].should == project_name
80
+ end
81
+
82
+ end
83
+ end
@@ -0,0 +1,43 @@
1
+ #
2
+ # Copyright 2012 Mortar Data Inc.
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+ #
16
+
17
+ require "spec_helper"
18
+ require "mortar/api"
19
+
20
+ describe Mortar::API do
21
+
22
+ before(:each) do
23
+ @api = Mortar::API.new
24
+ end
25
+
26
+ after(:each) do
27
+ Excon.stubs.clear
28
+ end
29
+
30
+ context "tasks" do
31
+
32
+ it "gets task" do
33
+ task_id = "b480950f1fbf4d8a96235038d6badb7d"
34
+
35
+ Excon.stub({:method => :get, :path => "/v2/tasks/#{task_id}"}) do |params|
36
+ {:body => Mortar::API::OkJson.encode({"task_id" => task_id, "status_code" => "some_status"}), :status => 200}
37
+ end
38
+ response = @api.get_task(task_id)
39
+ response.body["status_code"].should == "some_status"
40
+ end
41
+
42
+ end
43
+ end