Class: BBC::Redux::Cookie

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

Overview

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

Examples:

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

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:

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value) ⇒ Cookie

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

Parameters:

Raises:

  • (ArgumentError)

    cookie string format incorrect, or non client object given


56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# 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

Instance Attribute Details

#clientBBC::Redux::Client (readonly)

Returns client object built from token

Returns:


48
49
50
# File 'lib/bbc/redux/cookie.rb', line 48

def client
  @client
end

#stringString (readonly) Also known as: to_s

Returns string representation of cookie, for use in HTTP responses Set-Cookie header

Returns:

  • (String)

    string representation of cookie, for use in HTTP responses Set-Cookie header


42
43
44
# File 'lib/bbc/redux/cookie.rb', line 42

def string
  @string
end

#tokenString (readonly)

Returns session token

Returns:

  • (String)

    session token


37
38
39
# File 'lib/bbc/redux/cookie.rb', line 37

def token
  @token
end

Instance Method Details

#==(other_cookie) ⇒ Boolean Also known as: eql?

Returns true if other_cookie is a redux cookie with the same token

Returns:

  • (Boolean)

    true if other_cookie is a redux cookie with the same token


76
77
78
# File 'lib/bbc/redux/cookie.rb', line 76

def ==(other_cookie)
  self.class == other_cookie.class && self.token == other_cookie.token
end