Class: Iated::BrowserTokenDB

Inherits:
Object
  • Object
show all
Defined in:
lib/iated/browser_token_db.rb

Overview

The database for storing authorized browser tokens

Instance Method Summary collapse

Constructor Details

#initialize(fname) ⇒ BrowserTokenDB

Returns a new instance of BrowserTokenDB.



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/iated/browser_token_db.rb', line 9

def initialize fname
  @fname = Pathname.new fname
  @tokens = {}

  # Write an empty file.
  if @fname.exist?
    read
  else
    write
  end
end

Instance Method Details

#add(user_agent) ⇒ String

Add a token to the database. This stores it persistently.

Returns:

  • (String)

    the new token



24
25
26
27
28
29
# File 'lib/iated/browser_token_db.rb', line 24

def add user_agent
  token = generate_token
  @tokens[token] = user_agent.to_s
  write
  return token
end

#has_token?(token) ⇒ Boolean

Does the token exist in the db?

Returns:

  • (Boolean)

    Does this token exist in the store?



33
34
35
# File 'lib/iated/browser_token_db.rb', line 33

def has_token? token
  @tokens.key? token
end

#tokensArray

List of all tokens in DB.

Returns:

  • (Array)

    All tokens stored in DB.



39
40
41
# File 'lib/iated/browser_token_db.rb', line 39

def tokens
  @tokens.keys
end

#user_agent(token) ⇒ String

What is the user_agent for the token?

Returns:

  • (String)

    The user agent



45
46
47
# File 'lib/iated/browser_token_db.rb', line 45

def user_agent token
  @tokens[token]
end