bbc_redux 0.4.3.pre → 0.4.4.pre
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/README.md +25 -0
- data/lib/bbc/redux/client.rb +28 -1
- data/lib/bbc/redux/cookie.rb +91 -0
- data/lib/bbc/redux/version.rb +1 -1
- data/lib/bbc/redux.rb +1 -0
- data/spec/bbc/redux/client_spec.rb +20 -1
- data/spec/bbc/redux/cookie_spec.rb +45 -0
- metadata +6 -3
data/README.md
CHANGED
@@ -112,6 +112,9 @@ to message us.
|
|
112
112
|
|
113
113
|
# Search
|
114
114
|
|
115
|
+
# You can also search on these params, see docs for more details
|
116
|
+
# :q, :name, :channel, :before, :after, :date, :longer, :shorter,
|
117
|
+
# :programme_crid, :series_crid,
|
115
118
|
results = client.search(:name => 'Pingu')
|
116
119
|
|
117
120
|
results.created_at #=> DateTime
|
@@ -154,6 +157,28 @@ http://rubydoc.info/github/bbcsnippets/redux-client-ruby/version-4/frames
|
|
154
157
|
|
155
158
|
## Caveats / Known Issues
|
156
159
|
|
160
|
+
### Number of HTTP requests
|
161
|
+
|
162
|
+
All the methods on the `BBC::Redux::Client` object represent a HTTP request to
|
163
|
+
the API, caching is left as an exercise to the reader.
|
164
|
+
|
165
|
+
This means you should at the very least be careful with how you handle the
|
166
|
+
returned objects. This code for example makes four HTTP requests:
|
167
|
+
|
168
|
+
client.asset('5966413090093319525').channel
|
169
|
+
client.asset('5966413090093319525').description
|
170
|
+
client.asset('5966413090093319525').name
|
171
|
+
client.asset('5966413090093319525').reference
|
172
|
+
|
173
|
+
Where as this code makes only one:
|
174
|
+
|
175
|
+
asset = client.asset('5966413090093319525')
|
176
|
+
|
177
|
+
asset.channel
|
178
|
+
asset.description
|
179
|
+
asset.name
|
180
|
+
asset.reference
|
181
|
+
|
157
182
|
### Using a proxy server
|
158
183
|
|
159
184
|
The client uses [Typhoeus](https://github.com/dbalatero/typhoeus) which
|
data/lib/bbc/redux/client.rb
CHANGED
@@ -32,6 +32,10 @@ module BBC
|
|
32
32
|
# @author Matt Haynes <matt.haynes@bbc.co.uk>
|
33
33
|
class Client
|
34
34
|
|
35
|
+
# Raised when you try to login and you account has been marked as
|
36
|
+
# compromised
|
37
|
+
class AccountCompromisedException < Exception; end
|
38
|
+
|
35
39
|
# Raised when backend HTTP API returns a 403, indicates you are either
|
36
40
|
# trying to access some content that is unavailable to you, or your token
|
37
41
|
# and session has expired.
|
@@ -67,6 +71,13 @@ module BBC
|
|
67
71
|
# @option options [Object] :http (Typhoeus) The http client, can be
|
68
72
|
# overidden but expects method .post to return an object looking like
|
69
73
|
# Typhoeus::Response (code, headers, body, etc)
|
74
|
+
#
|
75
|
+
# @raise [ArgumentError] if you provide neither :username / :password nor
|
76
|
+
# :token keys
|
77
|
+
# @raise [AccountCompromisedException] if the account has been flagged as
|
78
|
+
# compromised
|
79
|
+
# @raise [ForbiddenException] if your username or password are incorrect
|
80
|
+
# @raise [HttpException] if backend fails
|
70
81
|
def initialize(options = {})
|
71
82
|
@host = options[:host] || 'https://i.bbcredux.com'
|
72
83
|
@http = options[:http] || Typhoeus
|
@@ -79,9 +90,14 @@ module BBC
|
|
79
90
|
:username => username, :password => password
|
80
91
|
})
|
81
92
|
|
93
|
+
if data['compromised']
|
94
|
+
raise AccountCompromisedException
|
95
|
+
end
|
96
|
+
|
82
97
|
@token = data.fetch('token')
|
83
98
|
else
|
84
|
-
|
99
|
+
err = 'Supply either :token or :username and :password options'
|
100
|
+
raise ArgumentError.new(err)
|
85
101
|
end
|
86
102
|
end
|
87
103
|
end
|
@@ -216,6 +232,17 @@ module BBC
|
|
216
232
|
build :user, :using => data_for(:user)
|
217
233
|
end
|
218
234
|
|
235
|
+
# @return [Boolean] true if other_clinet is a redux client with the same
|
236
|
+
# token, host and http
|
237
|
+
def ==(other_client)
|
238
|
+
self.class == other_client.class && \
|
239
|
+
self.token == other_client.token && \
|
240
|
+
self.host == other_client.host && \
|
241
|
+
self.http == other_client.http
|
242
|
+
end
|
243
|
+
|
244
|
+
alias :eql? :==
|
245
|
+
|
219
246
|
private
|
220
247
|
|
221
248
|
# @private
|
@@ -0,0 +1,91 @@
|
|
1
|
+
module BBC
|
2
|
+
module Redux
|
3
|
+
|
4
|
+
# Redux Cookie util class
|
5
|
+
#
|
6
|
+
# A utility class for creating and parsing correct Redux cookies. Can be
|
7
|
+
# constructed from an existing client object or a raw cookie string
|
8
|
+
#
|
9
|
+
# @example Build cookie from client
|
10
|
+
#
|
11
|
+
# client = BBC::Redux::Client.new({
|
12
|
+
# :username => 'username',
|
13
|
+
# :password => 'password',
|
14
|
+
# })
|
15
|
+
#
|
16
|
+
# cookie = BBC::Redux::Cookie.new(client)
|
17
|
+
#
|
18
|
+
# cookie.token #=> String
|
19
|
+
# cookie.client #=> BBC::Redux::Client
|
20
|
+
# cookie.to_s #=> String
|
21
|
+
#
|
22
|
+
# @example Build cookie from existing value
|
23
|
+
#
|
24
|
+
# string = "BBC_video=some-token; domain=.bbcredux.com; path=/"
|
25
|
+
#
|
26
|
+
# cookie = BBC::Redux::Cookie.new(string)
|
27
|
+
#
|
28
|
+
# cookie.token #=> String
|
29
|
+
# cookie.client #=> BBC::Redux::Client
|
30
|
+
# cookie.to_s #=> String
|
31
|
+
#
|
32
|
+
# @author Matt Haynes <matt.haynes@bbc.co.uk>
|
33
|
+
class Cookie
|
34
|
+
|
35
|
+
# @!attribute [r] token
|
36
|
+
# @return [String] session token
|
37
|
+
attr_reader :token
|
38
|
+
|
39
|
+
# @!attribute [r] string
|
40
|
+
# @return [String] string representation of cookie, for use in HTTP
|
41
|
+
# responses Set-Cookie header
|
42
|
+
attr_reader :string
|
43
|
+
|
44
|
+
alias :to_s :string
|
45
|
+
|
46
|
+
# @!attribute [r] client
|
47
|
+
# @return [BBC::Redux::Client] client object built from token
|
48
|
+
attr_reader :client
|
49
|
+
|
50
|
+
# Build cookie from eith a client object or string value (the literal
|
51
|
+
# value of a prior HTTP cookie)
|
52
|
+
#
|
53
|
+
# @param [String|BBC::Redux::Client] value value to build cookie from
|
54
|
+
# @raise [ArgumentError] cookie string format incorrect, or non client
|
55
|
+
# object given
|
56
|
+
def initialize(value)
|
57
|
+
if value.class == BBC::Redux::Client
|
58
|
+
@token = value.token
|
59
|
+
@client = value
|
60
|
+
@string = build_string(token)
|
61
|
+
elsif value.class == String
|
62
|
+
if value =~ /BBC_video=(.*);\s+domain=.bbcredux.com;\s+path=\//
|
63
|
+
@token = $1
|
64
|
+
@client = BBC::Redux::Client.new( :token => token )
|
65
|
+
@string = build_string(token)
|
66
|
+
else
|
67
|
+
raise ArgumentError.new('bad cookie format')
|
68
|
+
end
|
69
|
+
else
|
70
|
+
raise ArgumentError.new('provide BBC::Redux::Client or String')
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
# @return [Boolean] true if other_cookie is a redux cookie with the same
|
75
|
+
# token
|
76
|
+
def ==(other_cookie)
|
77
|
+
self.class == other_cookie.class && self.token == other_cookie.token
|
78
|
+
end
|
79
|
+
|
80
|
+
alias :eql? :==
|
81
|
+
|
82
|
+
private
|
83
|
+
|
84
|
+
def build_string(token)
|
85
|
+
"BBC_video=#{token}; domain=.bbcredux.com; path=/"
|
86
|
+
end
|
87
|
+
|
88
|
+
end
|
89
|
+
|
90
|
+
end
|
91
|
+
end
|
data/lib/bbc/redux/version.rb
CHANGED
data/lib/bbc/redux.rb
CHANGED
@@ -2,6 +2,7 @@ require_relative 'redux/asset'
|
|
2
2
|
require_relative 'redux/channel'
|
3
3
|
require_relative 'redux/channel_category'
|
4
4
|
require_relative 'redux/client'
|
5
|
+
require_relative 'redux/cookie'
|
5
6
|
require_relative 'redux/crid'
|
6
7
|
require_relative 'redux/end_points'
|
7
8
|
require_relative 'redux/key'
|
@@ -72,9 +72,28 @@ describe BBC::Redux::Client do
|
|
72
72
|
end
|
73
73
|
end
|
74
74
|
|
75
|
+
context 'account is compromised' do
|
76
|
+
|
77
|
+
subject do
|
78
|
+
described_class.new({
|
79
|
+
:http => http_client, :username => 'foo', :password => 'bar'
|
80
|
+
})
|
81
|
+
end
|
82
|
+
|
83
|
+
it 'raises error' do
|
84
|
+
|
85
|
+
resp = double(:resp, :code => 200, :body => '{"compromised": true}')
|
86
|
+
|
87
|
+
expect(http_client).to receive(:post).and_return(resp)
|
88
|
+
|
89
|
+
expect { subject }.to \
|
90
|
+
raise_error(BBC::Redux::Client::AccountCompromisedException)
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
75
94
|
context 'initialized with neither token or username / password' do
|
76
95
|
it 'raises an error' do
|
77
|
-
expect { described_class.new }.to raise_error
|
96
|
+
expect { described_class.new }.to raise_error(ArgumentError)
|
78
97
|
end
|
79
98
|
end
|
80
99
|
|
@@ -0,0 +1,45 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe BBC::Redux::Cookie do
|
4
|
+
|
5
|
+
let(:token) { '1111111-Yf9hnrYmSsmCcWVZ52rquLRoaeDJT9EA' }
|
6
|
+
let(:string) { "BBC_video=#{token}; domain=.bbcredux.com; path=/" }
|
7
|
+
let(:client) { BBC::Redux::Client.new( :token => token ) }
|
8
|
+
|
9
|
+
context 'initialized with string' do
|
10
|
+
subject { described_class.new(string) }
|
11
|
+
its('token') { should eq(token) }
|
12
|
+
its('client') { should eq(client) }
|
13
|
+
its('to_s') { should eq(string) }
|
14
|
+
its('string') { should eq(string) }
|
15
|
+
|
16
|
+
context 'bad string' do
|
17
|
+
it 'should raise error' do
|
18
|
+
expect { described_class.new('foobar') }.to raise_error(ArgumentError)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
context 'initialized with client' do
|
24
|
+
subject { described_class.new(client) }
|
25
|
+
its('client') { should eq(client) }
|
26
|
+
its('token') { should eq(token) }
|
27
|
+
its('to_s') { should eq(string) }
|
28
|
+
its('string') { should eq(string) }
|
29
|
+
end
|
30
|
+
|
31
|
+
context 'initialized with junk' do
|
32
|
+
it 'should raise error' do
|
33
|
+
expect { described_class.new(1234) }.to raise_error(ArgumentError)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
describe '.==' do
|
38
|
+
it 'should be true when token is the same' do
|
39
|
+
a = described_class.new(string)
|
40
|
+
b = described_class.new(client)
|
41
|
+
expect(a).to eq(b)
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: bbc_redux
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.4.
|
4
|
+
version: 0.4.4.pre
|
5
5
|
prerelease: 6
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -11,7 +11,7 @@ authors:
|
|
11
11
|
autorequire:
|
12
12
|
bindir: bin
|
13
13
|
cert_chain: []
|
14
|
-
date: 2014-
|
14
|
+
date: 2014-05-28 00:00:00.000000000 Z
|
15
15
|
dependencies:
|
16
16
|
- !ruby/object:Gem::Dependency
|
17
17
|
name: rspec
|
@@ -159,6 +159,7 @@ files:
|
|
159
159
|
- lib/bbc/redux/channel.rb
|
160
160
|
- lib/bbc/redux/channel_category.rb
|
161
161
|
- lib/bbc/redux/client.rb
|
162
|
+
- lib/bbc/redux/cookie.rb
|
162
163
|
- lib/bbc/redux/crid.rb
|
163
164
|
- lib/bbc/redux/end_points.rb
|
164
165
|
- lib/bbc/redux/key.rb
|
@@ -172,6 +173,7 @@ files:
|
|
172
173
|
- spec/bbc/redux/channel_category_spec.rb
|
173
174
|
- spec/bbc/redux/channel_spec.rb
|
174
175
|
- spec/bbc/redux/client_spec.rb
|
176
|
+
- spec/bbc/redux/cookie_spec.rb
|
175
177
|
- spec/bbc/redux/key_spec.rb
|
176
178
|
- spec/bbc/redux/media_url_spec.rb
|
177
179
|
- spec/bbc/redux/search_results_spec.rb
|
@@ -198,7 +200,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
198
200
|
version: '0'
|
199
201
|
segments:
|
200
202
|
- 0
|
201
|
-
hash:
|
203
|
+
hash: 4200921616866606374
|
202
204
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
203
205
|
none: false
|
204
206
|
requirements:
|
@@ -217,6 +219,7 @@ test_files:
|
|
217
219
|
- spec/bbc/redux/channel_category_spec.rb
|
218
220
|
- spec/bbc/redux/channel_spec.rb
|
219
221
|
- spec/bbc/redux/client_spec.rb
|
222
|
+
- spec/bbc/redux/cookie_spec.rb
|
220
223
|
- spec/bbc/redux/key_spec.rb
|
221
224
|
- spec/bbc/redux/media_url_spec.rb
|
222
225
|
- spec/bbc/redux/search_results_spec.rb
|