Class: BBC::Redux::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/bbc/redux/client.rb

Overview

Redux API Client

Examples:

Initialize client with either username and password or token


client = BBC::Redux::Client.new({
  :username => 'username',
  :password => 'password',
})

client = BBC::Redux::Client.new :token => 'some-token'

Using the client to retrieve data


client.asset('5966413090093319525') #=> BBC::Redux::Asset
client.channel_categories           #=> Array<BBC::Redux::ChannelCategory>
client.channels                     #=> Array<BBC::Redux::Channel>
client.schedule(Date.today)         #=> Array<BBC::Redux::Asset>
client.search(:name => 'Pingu')     #=> BBC::Redux::SearchResults
client.user                         #=> BBC::Redux::User

Call logout once finished to destroy your session


client.logout

Author:

Defined Under Namespace

Classes: AccountCompromisedException, ForbiddenException, HttpException, JsonParseException, NotFoundException

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Client

Client must be initialized with either a username and password combination or a token

Parameters:

  • options (Hash) (defaults to: {})

    the options to create client with

Options Hash (options):

  • :username (String)

    username of a redux account

  • :password (String)

    password of a redux account

  • :token (String)

    token for an existing redux session

  • :host (String) — default: https://i.bbcredux.com

    api host

  • :http (Object) — default: Typhoeus

    The http client, can be overidden but expects method .post to return an object looking like Typhoeus::Response (code, headers, body, etc)

Raises:



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(options = {})
  @host  = options[:host] || 'https://i.bbcredux.com'
  @http  = options[:http] || Typhoeus
  @token = options[:token] || begin
    username = options[:username]
    password = options[: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

#hostString (readonly)

Returns API HTTP host.

Returns:

  • (String)

    API HTTP host



64
65
66
# File 'lib/bbc/redux/client.rb', line 64

def host
  @host
end

#httpObject (readonly)

Returns http client, by default this is Typhoeus.

Returns:

  • (Object)

    http client, by default this is Typhoeus



56
57
58
# File 'lib/bbc/redux/client.rb', line 56

def http
  @http
end

#tokenString (readonly)

Returns token for current session.

Returns:

  • (String)

    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.

Returns:

  • (Boolean)

    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

Parameters:

  • identifier (String)

    either disk reference or uuid

Returns:

See Also:



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_categoriesArray<BBC::Redux::ChannelCategory>

Fetch available channel categories for this session

Returns:

See Also:



134
135
136
# File 'lib/bbc/redux/client.rb', line 134

def channel_categories
  build :channel_categories, :using => data_for(:channel_categories)
end

#channelsArray<BBC::Redux::Channel>

Fetch available channels for this session

Returns:

See Also:



127
128
129
# File 'lib/bbc/redux/client.rb', line 127

def channels
  build :channels, :using => data_for(:channels)
end

#logoutnil

Logout of redux, invalidates your token. After calling this you cannot make any further requests with this client

Returns:

  • (nil)


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.

Parameters:

  • date (Date, DateTime, Time)

    query this schedule date

  • channel (String, Array<String>, Channel, Array<Channel>, nil)

    optionally limit schedule query to one or an array of channels

Returns:



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

Parameters:

  • params (Hash) (defaults to: {})

    your search parameters

Options Hash (params):

  • :q (String)

    free text query

  • :name (String)

    query on programme name

  • :channel (String, Array<String>, Channel, Array<Channel>)

    query on channel, e.g. ‘bbcone’. Can provide an array to search on multiple channels.

  • :limit (Integer)

    number of results to return. Default 10

  • :offset (Integer)

    offset of the start of results

  • :before (Date, DateTime, Time)

    only return broadcasts before date

  • :after (Date, DateTime, Time)

    only return broadcasts after date

  • :date (Date, DateTime, Time)

    everything from 0600 on given date for 24hrs

  • :longer (Integer)

    constraint on the duration, in seconds

  • :shorter (Integer)

    constraint on the duration, in seconds

  • :programme_crid (String)

    TV Anytime CRID

  • :series_crid (String)

    TV Anytime CRID

  • :repeats (TrueClass, FalseClass)

    include repeats

  • :sort_by (String)

    ‘time’ or nil

  • :sort_order (String)

    ‘ascending’ or ‘descending’

Returns:

See Also:



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

#userBBC::Redux::User?

Fetch user object for current session

Returns:

See Also:



250
251
252
# File 'lib/bbc/redux/client.rb', line 250

def user
  build :user, :using => data_for(:user)
end