Class: HipTail::SQLite3AuthorityProvider

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

Constant Summary collapse

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

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



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 "    CREATE TABLE IF NOT EXISTS hiptail_authority (\n        oauth_id          VARCHAR(255) NOT NULL PRIMARY KEY,\n        oauth_secret      VARCHAR(255) NOT NULL,\n        authorization_url VARCHAR(255) NOT NULL,\n        token_url         VARCHAR(255) NOT NULL,\n        room_id           INT UNSIGNED,\n        group_id          INT UNSIGNED NOT NULL,\n        api_base          VARCHAR(255) NOT NULL,\n        created_at        INTEGER NOT NULL\n    );\n  END_SQL\nend\n"

#get(oauth_id) ⇒ HipTail::Authority

This method is abstract.


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



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



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