Class: GoFlippy::Client

Inherits:
Object
  • Object
show all
Includes:
Logger
Defined in:
lib/goflippy-ruby/client.rb

Constant Summary

Constants included from Logger

Logger::MESSAGE_PREFIX

Instance Method Summary collapse

Methods included from Logger

debug, error, fatal, info, logger, logger=, unknown, warn

Constructor Details

#initialize(api_key, opts = {}) ⇒ Client

Returns a new instance of Client.



5
6
7
8
9
10
11
# File 'lib/goflippy-ruby/client.rb', line 5

def initialize(api_key, opts = {})
  @api_key = api_key
  @config = opts&.empty? ? Config.default : Config.new(opts)
  @store = MemoryStore.new
  @http_client = HttpClient.new(@api_key, @config)
  @poller = Poller.new(@config.polling_interval, @http_client, @store)
end

Instance Method Details

#enabled?(key, uid, default = false) ⇒ Boolean

Returns:

  • (Boolean)


18
19
20
21
22
23
24
25
26
27
# File 'lib/goflippy-ruby/client.rb', line 18

def enabled?(key, uid, default = false)
  uids = @store.find(key)
  cache = uids&.dig(uid.to_sym)
  if cache == nil
    enabled = @http_client.get("/users/#{uid}/features/#{key}")&.dig(:enabled)
    @store.put(key, { uid: uid.to_sym, enabled: enabled })
    return enabled
  end
  cache
end

#register(uid, groups = []) ⇒ Object



29
30
31
32
33
34
35
36
# File 'lib/goflippy-ruby/client.rb', line 29

def register(uid, groups = [])
  @store.put(uid, true)
  params = {
      uid: uid,
      groups: groups
  }
  @http_client.post('/users', params)
end

#startObject



13
14
15
16
# File 'lib/goflippy-ruby/client.rb', line 13

def start
  Logger.info('Start GoFlippy client')
  @poller.start
end