proteus_client 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -18,99 +18,101 @@ module ProteusClient
18
18
  class Proxy
19
19
 
20
20
  def initialize(options = {})
21
- options = merge_with_default_options(options)
22
- @api_key = options[:api_key]
23
- @domain = RestClient::Resource.new(options[:domain])
21
+ options = {
22
+ api_key: ProteusClient.config.api_key,
23
+ domain: ProteusClient.config.domain
24
+ }.merge(options)
25
+
26
+ raise "APIKeyNotDefined" unless options[:api_key]
27
+ raise "DomainNotDefined" unless options[:domain]
28
+
29
+ @api_key = options[:api_key]
30
+ @domain = RestClient::Resource.new(options[:domain])
24
31
  end
25
32
 
26
33
  def test_api
27
- response = submit_request(:get,'/',{})
34
+ response = submit_request(:get, '/')
28
35
 
29
- {message: response.body}
36
+ { message: response.body }
30
37
  end
31
38
 
32
- def create_video_allocation(count=0,params={})
33
- params = {count: count} if count > 0
34
-
35
- body = submit_request_and_parse_json(:post,'/video_allocations',params)
39
+ def create_video_allocation
40
+ video_allocation =
41
+ submit_request_and_parse_json(:post, '/video_allocations')
42
+ format_video_allocation(video_allocation)
43
+ end
36
44
 
37
- return parse_video_allocation(body) if count==0
38
- body.map { |e| parse_video_allocation(e) }
45
+ def create_video_allocations(count)
46
+ video_allocations =
47
+ submit_request_and_parse_json(:post, '/video_allocations',
48
+ count: count)
49
+ video_allocations.map { |v| format_video_allocation(v) }
39
50
  end
40
51
 
41
- def create_video(video_allocation_id,type)
42
- body = submit_request_and_parse_json(:post, '/videos',
43
- {video_allocation_id: video_allocation_id,
44
- type: type})
52
+ def create_video(video_allocation_id, type)
53
+ body =
54
+ submit_request_and_parse_json(:post, '/videos',
55
+ video_allocation_id: video_allocation_id,
56
+ type: type)
45
57
 
46
- {video_id: body['_id'],url: body['allocation_url'] }
58
+ { video_id: body['_id'], url: body['allocation_url'] }
47
59
  end
48
60
 
49
- def create_video_versions(video_id,settings=nil)
50
- params = {}
51
- params = {version_settings: settings} unless settings.nil?
61
+ def create_video_versions(video_id, settings = nil)
62
+ params = {}
63
+ params[:version_settings] = settings if settings
52
64
 
53
- submit_request(:post, "/videos/#{video_id}/versions",params)
65
+ submit_request(:post, "/videos/#{video_id}/versions", params)
54
66
 
55
- {message: 'success'}
67
+ { message: 'success' }
56
68
  end
57
69
 
58
70
  def get_video(video_id)
59
- submit_request_and_parse_json(:get,"/videos/#{video_id}",{})
71
+ submit_request_and_parse_json(:get, "/videos/#{video_id}")
60
72
  end
61
73
 
62
- def get_videos(*ids)
63
- submit_request_and_parse_json(:get_raw,"/videos",{ids: ids})
74
+ def get_videos(video_ids)
75
+ submit_request_and_parse_json(:get_raw, "/videos", ids: video_ids)
64
76
  end
65
77
 
66
78
  def get_video_allocation(video_allocation_id)
67
- submit_request_and_parse_json(:get,"/video_allocations/#{video_allocation_id}",{})
79
+ url = "/video_allocations/#{video_allocation_id}"
80
+ submit_request_and_parse_json(:get, url)
68
81
  end
69
82
 
70
83
  private
71
84
 
72
- def merge_with_default_options(options)
73
- opts = {
74
- api_key: ProteusClient.config.api_key,
75
- domain: ProteusClient.config.domain
76
- }.merge(options)
77
-
78
- raise "APIKeyNotDefined" unless opts[:api_key]
79
- raise "DomainNotDefined" unless opts[:domain]
80
-
81
- opts
82
- end
83
-
84
- def submit_request_and_parse_json(method,route,params)
85
- response = submit_request(method,route,params)
85
+ def submit_request_and_parse_json(method, route, params = {})
86
+ response = submit_request(method, route, params)
86
87
  JSON.parse(response)
87
88
  end
88
89
 
89
- def submit_request(method,route,params)
90
- params = {:api_key => @api_key}.merge(params)
90
+ def submit_request(method, route, params = {})
91
+ params = { :api_key => @api_key }.merge(params)
91
92
 
92
93
  case method
93
- when :get
94
- @domain[route].get({params: params})
95
- when :get_raw
96
- params_str = RestClient::Payload::UrlEncoded.new(params).to_s
97
- @domain["#{route}?#{params_str}"].get({})
98
- when :post
99
- @domain[route].post(params, :content_type => :json)
100
- else
101
- raise 'Undefined method'
94
+ when :get
95
+ @domain[route].get(params: params)
96
+ when :get_raw
97
+ string_params = RestClient::Payload::UrlEncoded.new(params).to_s
98
+ @domain["#{route}?#{string_params}"].get()
99
+ when :post
100
+ @domain[route].post(params, content_type: :json)
101
+ else
102
+ raise 'Undefined method'
102
103
  end
103
104
  end
104
105
 
105
- def parse_video_allocation(video_allocation)
106
- urls = [{type: 'direct', url: video_allocation['url_direct']}]
107
- urls << {type: 'stream', url: video_allocation['url_stream']}
108
-
109
- urls << {type: 'upload', url: video_allocation['url_upload']}
106
+ def format_video_allocation(video_allocation)
107
+ urls = []
108
+
109
+ urls << { type: 'direct', url: video_allocation['url_direct'] }
110
+ urls << { type: 'stream', url: video_allocation['url_stream'] }
111
+ urls << { type: 'upload', url: video_allocation['url_upload'] }
110
112
 
111
- {video_allocation_id: video_allocation['_id'],urls: urls }
113
+ { video_allocation_id: video_allocation['_id'], urls: urls }
112
114
  end
113
115
 
114
116
  end
115
117
 
116
- end
118
+ end
@@ -1,6 +1,7 @@
1
- require 'spec_helper'
2
- require 'stubs'
1
+ require 'simplecov'
2
+ SimpleCov.start
3
3
 
4
+ require 'stubs'
4
5
  require_relative '../lib/proteus_client'
5
6
 
6
7
  describe ProteusClient::Proxy do
@@ -12,8 +13,8 @@ describe ProteusClient::Proxy do
12
13
  end
13
14
 
14
15
  let(:proteus_client) do
15
- ProteusClient.config.domain = 'http://localhost:9292'
16
- ProteusClient.config.api_key = 'api_key'
16
+ ProteusClient.config.domain = 'http://localhost:9292'
17
+ ProteusClient.config.api_key = 'api_key'
17
18
  described_class.new()
18
19
  end
19
20
 
@@ -23,8 +24,9 @@ describe ProteusClient::Proxy do
23
24
  end
24
25
 
25
26
  context 'call to test_api' do
27
+
26
28
  before(:all) do
27
- stub_response(:get,'/',200,'Welcome to proteus',{})
29
+ stub_response(:get, '/', 200, 'Welcome to proteus')
28
30
  @response = proteus_client.test_api
29
31
  end
30
32
 
@@ -36,60 +38,73 @@ describe ProteusClient::Proxy do
36
38
 
37
39
  context 'call to create_video_allocation'do
38
40
 
39
- context 'with no count argument' do
41
+ before(:all) do
42
+ stub_create_video_allocation
43
+ @response = proteus_client.create_video_allocation()
44
+ end
40
45
 
41
- before(:all) do
42
- stub_create_video_allocation
43
- @response = proteus_client.create_video_allocation()
44
- end
46
+ it "returns a hash" do
47
+ @response.class.should == Hash
48
+ end
45
49
 
46
- it "returns a hash" do
47
- @response.class.should == Hash
48
- end
50
+ it "returns video_allocation_id" do
51
+ @response[:video_allocation_id].should == '4fe0f752831a9d08bb000010'
52
+ end
49
53
 
50
- it "returns video_allocation_id" do
51
- @response[:video_allocation_id].should == '4fe0f752831a9d08bb000010'
52
- end
54
+ it "returns urls" do
55
+ @response[:urls][0].should ==
56
+ { type: 'direct', url: 'direct/4fe0f752831a9d08bb000010.flv' }
57
+ @response[:urls][1].should ==
58
+ { type: 'stream', url: 'stream/4fe0f752831a9d08bb000010.flv' }
59
+ @response[:urls][2].should ==
60
+ { type: 'upload', url: 'upload/4fe0f752831a9d08bb000010.flv' }
61
+ end
53
62
 
54
- it "returns urls" do
55
- @response[:urls][0].should == {type: 'direct', url:'direct/4fe0f752831a9d08bb000010.flv'}
56
- @response[:urls][1].should == {type: 'stream', url:'stream/4fe0f752831a9d08bb000010.flv'}
57
- @response[:urls][2].should == {type: 'upload', url:'upload/4fe0f752831a9d08bb000010.flv'}
58
- end
63
+ end
59
64
 
60
- end
65
+ context 'call to create_video_allocations' do
61
66
 
62
- context 'with count argument' do
67
+ before(:all) do
68
+ stub_create_video_allocations
69
+ @response = proteus_client.create_video_allocations(2)
70
+ end
63
71
 
64
- before(:all) do
65
- stub_create_video_allocations
66
- @response = proteus_client.create_video_allocation(2)
67
- end
72
+ it "returns an array with two hashes" do
73
+ @response.class.should == Array
74
+ @response.size.should == 2
75
+ @response[0].class.should == Hash
76
+ end
68
77
 
69
- it "returns an array with two hashes" do
70
- @response.class.should == Array
71
- @response.size.should == 2
72
- @response[0].class.should == Hash
73
- end
78
+ it "returns video_allocation_id" do
79
+ @response[0][:video_allocation_id].should == '4fe0f752831a9d08bb000010'
80
+ end
74
81
 
82
+ it "returns urls" do
83
+ @response[0][:urls][0].should ==
84
+ { type: 'direct', url: 'direct/4fe0f752831a9d08bb000010.flv' }
85
+ @response[0][:urls][1].should ==
86
+ { type: 'stream', url: 'stream/4fe0f752831a9d08bb000010.flv' }
87
+ @response[0][:urls][2].should ==
88
+ { type: 'upload', url: 'upload/4fe0f752831a9d08bb000010.flv' }
75
89
  end
76
90
 
77
91
  end
78
92
 
79
93
  context 'call to create_video'do
80
94
 
81
- before(:all) do
82
- stub_create_video
83
- @response = proteus_client.create_video('4fe0f752831a9d08bb000010','direct')
84
- end
95
+ before(:all) do
96
+ stub_create_video
97
+ @response = proteus_client.
98
+ create_video('4fe0f752831a9d08bb000010','direct')
99
+ end
85
100
 
86
- it "returns a hash" do
87
- @response.class.should == Hash
88
- end
101
+ it "returns a hash" do
102
+ @response.class.should == Hash
103
+ end
89
104
 
90
- it "returns video url" do
91
- @response[:url].should == 'direct/4fe0f752831a9d08bb000010.flv'
92
- end
105
+ it "returns video url" do
106
+ @response[:url].should == 'direct/4fe0f752831a9d08bb000010.flv'
107
+ end
93
108
 
94
109
  end
95
110
 
@@ -98,9 +113,10 @@ describe ProteusClient::Proxy do
98
113
  context 'with valid id' do
99
114
 
100
115
  before(:all) do
101
- stub_response(:post,'/videos/4fe0f752831a9d08bb000010/versions',
102
- 200,'',{})
103
- @response = proteus_client.create_video_versions('4fe0f752831a9d08bb000010')
116
+ stub_response_with_params(:post, '/videos/4fe0f752831a9d08bb000010/versions',
117
+ 200, '',{})
118
+ @response = proteus_client.
119
+ create_video_versions('4fe0f752831a9d08bb000010')
104
120
  end
105
121
 
106
122
  it "returns success message" do
@@ -112,9 +128,10 @@ describe ProteusClient::Proxy do
112
128
  context 'with valid id and type' do
113
129
 
114
130
  before(:all) do
115
- stub_response(:post,'/videos/4fe0f752831a9d08bb000010/versions',
116
- 200,'',{version_settings: 'mobile'})
117
- @response = proteus_client.create_video_versions('4fe0f752831a9d08bb000010','mobile')
131
+ stub_response_with_params(:post, '/videos/4fe0f752831a9d08bb000010/versions',
132
+ 200, '', version_settings: 'mobile')
133
+ @response = proteus_client.
134
+ create_video_versions('4fe0f752831a9d08bb000010','mobile')
118
135
  end
119
136
 
120
137
  it "returns true" do
@@ -137,9 +154,9 @@ describe ProteusClient::Proxy do
137
154
  end
138
155
 
139
156
  it "returns video with correct format" do
140
- @response['url'].class.should == String
141
- @response['duration'].class.should == Fixnum
142
- @response['versions'].class.should == Array
157
+ @response['url'].class.should == String
158
+ @response['duration'].class.should == Fixnum
159
+ @response['versions'].class.should == Array
143
160
  @response['versions'][0].class.should == Hash
144
161
  end
145
162
 
@@ -149,20 +166,22 @@ describe ProteusClient::Proxy do
149
166
 
150
167
  before(:all) do
151
168
  stub_get_videos
152
- @response = proteus_client.get_videos('4fe0f752831a9d08bb000010','4fe0f752831a9d08bb000010')
169
+ @response = proteus_client.get_videos(%w{4fe0f752831a9d08bb000010
170
+ 4fe0f752831a9d08bb000010})
153
171
  end
154
172
 
155
173
  it "returns an array with hashes" do
156
- @response.class.should == Array
157
- @response.size.should == 2
174
+ @response.class.should == Array
175
+ @response.size.should == 2
158
176
  @response.first.class.should == Hash
159
177
  end
160
178
 
161
179
  it "returns videos with correct format" do
162
180
  video = @response.first
163
- video['url'].class.should == String
164
- video['duration'].class.should == Fixnum
165
- video['versions'].class.should == Array
181
+
182
+ video['url'].class.should == String
183
+ video['duration'].class.should == Fixnum
184
+ video['versions'].class.should == Array
166
185
  video['versions'][0].class.should == Hash
167
186
  end
168
187
 
@@ -174,7 +193,8 @@ describe ProteusClient::Proxy do
174
193
 
175
194
  before(:all) do
176
195
  stub_get_video_allocation
177
- @response = proteus_client.get_video_allocation('4fe0f752831a9d08bb000010')
196
+ @response = proteus_client.
197
+ get_video_allocation('4fe0f752831a9d08bb000010')
178
198
  end
179
199
 
180
200
  it "returns a hash" do
@@ -195,23 +215,27 @@ describe ProteusClient::Proxy do
195
215
 
196
216
  it 'call with :get works returns a correct response' do
197
217
  stub_resource(:get)
198
- proteus_client.send(:submit_request,:get,'/',{}).class.should == String
218
+ proteus_client.send(:submit_request, :get, '/', {})
219
+ .class.should == String
199
220
  end
200
221
 
201
222
  it 'call with :post works returns a correct response' do
202
223
  stub_resource(:post)
203
- proteus_client.send(:submit_request,:post,'/',{}).class.should == String
224
+ proteus_client.send(:submit_request, :post, '/', {})
225
+ .class.should == String
204
226
  end
205
227
 
206
228
  it 'call with :get works returns a correct response' do
207
229
  stub_resource(:get)
208
- proteus_client.send(:submit_request,:get_raw,'/',{}).class.should == String
230
+ proteus_client.send(:submit_request, :get_raw, '/', {})
231
+ .class.should == String
209
232
  end
210
233
 
211
234
  it 'call with not defined method works raises exception' do
212
- lambda { proteus_client.send(:submit_request,:put,'/',{}) }.should raise_error
235
+ lambda { proteus_client.send(:submit_request, :put, '/', {}) }.
236
+ should raise_error
213
237
  end
214
238
 
215
239
  end
216
240
 
217
- end
241
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: proteus_client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,8 +9,72 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-06-22 00:00:00.000000000 Z
13
- dependencies: []
12
+ date: 2012-07-06 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: 1.7.3
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: 1.7.3
30
+ - !ruby/object:Gem::Dependency
31
+ name: rest-client
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: 1.6.7
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: 1.6.7
46
+ - !ruby/object:Gem::Dependency
47
+ name: rspec
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
+ - !ruby/object:Gem::Dependency
63
+ name: simplecov
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
14
78
  description: It provides an interface to use proteus features
15
79
  email: 1.27201@gmail.com
16
80
  executables: []