Class: BBC::Redux::Client
- Inherits:
-
Object
- Object
- BBC::Redux::Client
- Defined in:
- lib/bbc/redux/client.rb
Overview
Redux API Client
Defined Under Namespace
Classes: AccountCompromisedException, ForbiddenException, HttpException, JsonParseException, NotFoundException
Instance Attribute Summary collapse
-
#host ⇒ String
readonly
API HTTP host.
-
#http ⇒ Object
readonly
Http client, by default this is Typhoeus.
-
#token ⇒ String
readonly
Token for current session.
Instance Method Summary collapse
-
#==(other_client) ⇒ Boolean
(also: #eql?)
True if other_clinet is a redux client with the same token, host and http.
-
#asset(identifier) ⇒ BBC::Redux::Asset?
Fetch asset object.
-
#channel_categories ⇒ Array<BBC::Redux::ChannelCategory>
Fetch available channel categories for this session.
-
#channels ⇒ Array<BBC::Redux::Channel>
Fetch available channels for this session.
-
#initialize(options = {}) ⇒ Client
constructor
Client must be initialized with either a username and password combination or a token.
-
#logout ⇒ nil
Logout of redux, invalidates your token.
-
#schedule(date, channels = nil) ⇒ Array<BBC::Redux::Asset>
Return all programmes for the schedule date, everything from 0600 for 24 hours afterwards.
-
#search(params = {}) ⇒ BBC::Redux::SearchResults
Perform a search of Redux Archive.
-
#user ⇒ BBC::Redux::User?
Fetch user object for current session.
Constructor Details
#initialize(options = {}) ⇒ Client
Client must be initialized with either a username and password combination or a token
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/bbc/redux/client.rb', line 84 def initialize( = {}) @host = [:host] || 'https://i.bbcredux.com' @http = [:http] || Typhoeus @token = [:token] || begin username = [:username] password = [:password] if username && password data = data_for(:login, { :username => username, :password => password }) if data['compromised'] raise AccountCompromisedException end @token = data.fetch('token') else err = 'Supply either :token or :username and :password options' raise ArgumentError.new(err) end end end |
Instance Attribute Details
#host ⇒ String (readonly)
Returns API HTTP host.
64 65 66 |
# File 'lib/bbc/redux/client.rb', line 64 def host @host end |
#http ⇒ Object (readonly)
Returns http client, by default this is Typhoeus.
56 57 58 |
# File 'lib/bbc/redux/client.rb', line 56 def http @http end |
#token ⇒ String (readonly)
Returns token for current session.
60 61 62 |
# File 'lib/bbc/redux/client.rb', line 60 def token @token end |
Instance Method Details
#==(other_client) ⇒ Boolean Also known as: eql?
Returns true if other_clinet is a redux client with the same token, host and http.
256 257 258 259 260 261 |
# File 'lib/bbc/redux/client.rb', line 256 def ==(other_client) self.class == other_client.class && \ self.token == other_client.token && \ self.host == other_client.host && \ self.http == other_client.http end |
#asset(identifier) ⇒ BBC::Redux::Asset?
Fetch asset object
112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/bbc/redux/client.rb', line 112 def asset(identifier) rex = /\A[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}\Z/ if identifier =~ rex params = { :uuid => identifier } else params = { :reference => identifier } end build :asset, :using => data_for(:asset, params) end |
#channel_categories ⇒ Array<BBC::Redux::ChannelCategory>
Fetch available channel categories for this session
134 135 136 |
# File 'lib/bbc/redux/client.rb', line 134 def channel_categories build :channel_categories, :using => data_for(:channel_categories) end |
#channels ⇒ Array<BBC::Redux::Channel>
Fetch available channels for this session
127 128 129 |
# File 'lib/bbc/redux/client.rb', line 127 def channels build :channels, :using => data_for(:channels) end |
#logout ⇒ nil
Logout of redux, invalidates your token. After calling this you cannot make any further requests with this client
141 142 143 144 |
# File 'lib/bbc/redux/client.rb', line 141 def logout data_for(:logout) return nil end |
#schedule(date, channels = nil) ⇒ Array<BBC::Redux::Asset>
Return all programmes for the schedule date, everything from 0600 for 24 hours afterwards. May make multiple requests to backend to retreive all the data.
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 |
# File 'lib/bbc/redux/client.rb', line 154 def schedule(date, channels = nil) date = DateTime.parse date.strftime('%Y-%m-%dT06:00:00Z00:00') query = { :after => date.strftime('%Y-%m-%dT%H:%M:%S'), :before => ( date + 1 ).strftime('%Y-%m-%dT05:59:59'), :channel => channels, :offset => 0, :limit => 100, :sort_by =>'time', :sort_order => 'ascending', :repeats => true, } results = search(query) assets = [ ] while true do assets.concat(results.assets) if results.has_more? next_query = results.query.merge({ :offset => results.query[:offset] + 100 }) results = self.search(next_query) else break end end assets end |
#search(params = {}) ⇒ BBC::Redux::SearchResults
Perform a search of Redux Archive
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 |
# File 'lib/bbc/redux/client.rb', line 216 def search(params = {}) mapper = lambda do |val| if val.class == Date || val.class == DateTime || val.class == Time val.strftime('%Y-%m-%dT%H:%M:%S') elsif val.class == Channel val.name elsif val.class == TrueClass '1' elsif val.class == FalseClass '0' else val.to_s end end new_params = params.map do |key, val| if val.class == Array [ key, val.map(&mapper) ] else [ key, mapper.call(val) ] end end data = data_for(:search_results, Hash[new_params]).merge({ 'query' => params }) build :search_results, :using => data end |
#user ⇒ BBC::Redux::User?
Fetch user object for current session
250 251 252 |
# File 'lib/bbc/redux/client.rb', line 250 def user build :user, :using => data_for(:user) end |