class BBC::Redux::Cookie

Redux Cookie util class

A utility class for creating and parsing correct Redux cookies. Can be constructed from an existing client object or a raw cookie string

@example Build cookie from client

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

cookie = BBC::Redux::Cookie.new(client)

cookie.token  #=> String
cookie.client #=> BBC::Redux::Client
cookie.to_s   #=> String

@example Build cookie from existing value

string = "BBC_video=some-token; domain=.bbcredux.com; path=/"

cookie = BBC::Redux::Cookie.new(string)

cookie.token  #=> String
cookie.client #=> BBC::Redux::Client
cookie.to_s   #=> String

@author Matt Haynes <matt.haynes@bbc.co.uk>

Attributes

client[R]

@!attribute [r] client @return [BBC::Redux::Client] client object built from token

string[R]

@!attribute [r] string @return [String] string representation of cookie, for use in HTTP

responses Set-Cookie header
to_s[R]

@!attribute [r] string @return [String] string representation of cookie, for use in HTTP

responses Set-Cookie header
token[R]

@!attribute [r] token @return [String] session token

Public Class Methods

new(value) click to toggle source

Build cookie from eith a client object or string value (the literal value of a prior HTTP cookie)

@param [String|BBC::Redux::Client] value value to build cookie from @raise [ArgumentError] cookie string format incorrect, or non client

object given
# File lib/bbc/redux/cookie.rb, line 56
def initialize(value)
  if value.class == BBC::Redux::Client
    @token  = value.token
    @client = value
    @string = build_string(token)
  elsif value.class == String
    if value =~ /BBC_video=(.*);\s+domain=.bbcredux.com;\s+path=\//
      @token  = $1
      @client = BBC::Redux::Client.new( :token => token )
      @string = build_string(token)
    else
      raise ArgumentError.new('bad cookie format')
    end
  else
    raise ArgumentError.new('provide BBC::Redux::Client or String')
  end
end

Public Instance Methods

==(other_cookie) click to toggle source

@return [Boolean] true if other_cookie is a redux cookie with the same

token
# File lib/bbc/redux/cookie.rb, line 76
def ==(other_cookie)
  self.class == other_cookie.class && self.token == other_cookie.token
end
Also aliased as: eql?
eql?(other_cookie)
Alias for: ==

Private Instance Methods

build_string(token) click to toggle source
# File lib/bbc/redux/cookie.rb, line 84
def build_string(token)
  "BBC_video=#{token}; domain=.bbcredux.com; path=/"
end