Class: HipTail::SQLite3AuthorityProvider

Inherits:
AuthorityProvider show all
Defined in:
lib/hiptail/authority/sqlite3_provider.rb

Constant Summary collapse

SQL_GET =
<<-'END_SQL'
  SELECT * FROM hiptail_authority WHERE oauth_id = ? LIMIT 1
END_SQL
SQL_REGISTER =
<<-'END_SQL'
  REPLACE INTO hiptail_authority
    ( oauth_id, oauth_secret, authorization_url, token_url, room_id, group_id, api_base, created_at )
    VALUES ( :oauth_id, :oauth_secret, :authorization_url, :token_url, :room_id, :group_id, :api_base, :created_at )
END_SQL
SQL_UNREGISTER =
<<-'END_SQL'
  DELETE FROM hiptail_authority WHERE oauth_id = ?
END_SQL

Instance Method Summary collapse

Methods inherited from AuthorityProvider

#[], #[]=

Constructor Details

#initialize(db) ⇒ HipTail::SQLite3AuthorityProvider



6
7
8
9
10
11
# File 'lib/hiptail/authority/sqlite3_provider.rb', line 6

def initialize(db)
  @authorities = {}
  @db = db

  build
end

Instance Method Details

#buildvoid

This method returns an undefined value.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/hiptail/authority/sqlite3_provider.rb', line 14

def build
  @db.execute_batch <<-'END_SQL'
    CREATE TABLE IF NOT EXISTS hiptail_authority (
        oauth_id          VARCHAR(255) NOT NULL PRIMARY KEY,
        oauth_secret      VARCHAR(255) NOT NULL,
        authorization_url VARCHAR(255) NOT NULL,
        token_url         VARCHAR(255) NOT NULL,
        room_id           INT UNSIGNED,
        group_id          INT UNSIGNED NOT NULL,
        api_base          VARCHAR(255) NOT NULL,
        created_at        INTEGER NOT NULL
    );
  END_SQL
end

#get(oauth_id) ⇒ HipTail::Authority

This method is abstract.

Parameters:

  • oauth_id (String)

Returns:



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/hiptail/authority/sqlite3_provider.rb', line 36

def get(oauth_id)
  unless @authorities.include?(oauth_id)
    begin
      last_rah = @db.results_as_hash
      @db.results_as_hash = true
      @db.execute(SQL_GET, oauth_id) do |row|
        data = row.to_a.select { |f| f[0].is_a?(String) }.map { |f| [ f[0].to_sym, f[1] ] }
        @authorities[oauth_id] = HipTail::Authority.new(Hash[*data.flatten(1)])
        break
      end
    ensure
      @db.results_as_hash = last_rah
    end
  end

  @authorities[oauth_id]
end

#register(oauth_id, authority) ⇒ HipTail::Authority

Parameters:

Returns:



63
64
65
66
67
68
69
70
71
72
# File 'lib/hiptail/authority/sqlite3_provider.rb', line 63

def register(oauth_id, authority)
  @authorities[oauth_id] = authority

  row_data = authority.as_hash
  [ :api_base, :authorization_url, :token_url ].each do |key|
    row_data[key] = row_data[key].to_s
  end
  row_data[:created_at] = Time.now.to_i
  @db.execute(SQL_REGISTER, row_data)
end

#unregister(oauth_id) ⇒ void

This method returns an undefined value.

Parameters:

  • oauth_id (String)


80
81
82
83
84
# File 'lib/hiptail/authority/sqlite3_provider.rb', line 80

def unregister(oauth_id)
  @authorities.delete(oauth_id)

  @db.execute(SQL_UNREGISTER, oauth_id)
end