Class: FlagsEvaluation

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

Constant Summary collapse

@@cache =
Hash.new
@@route =
'https://featureflags.mybluemix.net'
@@verbose =
false

Class Method Summary collapse

Class Method Details

.cached(token, user_id) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/flags_sdk_ort.rb', line 24

def FlagsEvaluation.cached(token, user_id)
  print_if_verbose("Check if value was previously cached")

  if @@cache[token] != nil && @@cache[token][user_id] != nil
    print_if_verbose("The result was cached, checking if value has not expired (<= 120 seconds)")

    current_date = DateTime.now
    _, old_date = @@cache[token][user_id]
    
    elapsed_seconds = ((current_date - old_date) * 24 * 60 * 60).to_i 
    
    print_if_verbose("The elapsed seconds are " + elapsed_seconds.to_s)
    return elapsed_seconds <= 120
  end

  return false
end

.evaluate(token, user_id) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/flags_sdk_ort.rb', line 58

def FlagsEvaluation.evaluate(token, user_id)
  if !cached(token, user_id)
    print_if_verbose("A request is going to be made because there is no valid result cached for this flag and token")
    
    complete_route = @@route + '/flags/' + token.to_s + '/user/' + user_id.to_s + "/evaluate"

    response = HTTParty.get(complete_route, timeout: 1)

    if response.code == 200
      body = JSON.parse(response.body)

      if body["eval"] != nil
        self.save_to_cache(token, user_id, body["eval"])

        return body["eval"]
      else
        return false
      end
    else
      return false
    end
  else
    print_if_verbose("The evaluation result is going to be obtained from cache")

    return retrieve_from_cache(token, user_id)
  end
end


18
19
20
21
22
# File 'lib/flags_sdk_ort.rb', line 18

def FlagsEvaluation.print_if_verbose(message)
  if @@verbose
    puts message
  end
end

.retrieve_from_cache(token, user_id) ⇒ Object



50
51
52
53
54
55
56
# File 'lib/flags_sdk_ort.rb', line 50

def FlagsEvaluation.retrieve_from_cache(token, user_id)
  print_if_verbose("Retriving the value from cache")

  value, _ = @@cache[token][user_id]

  return value
end

.save_to_cache(token, user_id, result) ⇒ Object



42
43
44
45
46
47
48
# File 'lib/flags_sdk_ort.rb', line 42

def FlagsEvaluation.save_to_cache(token, user_id, result)
  print_if_verbose("Information for this request is going to be saved in the cache")

  @@cache[token] = {user_id => [result, DateTime.now]}

  print_if_verbose("The following values were added to the cache: token: " + token + ", user_id: " + user_id + ", result: " + @@cache[token][user_id].to_s)
end

.set_route(route) ⇒ Object



9
10
11
12
# File 'lib/flags_sdk_ort.rb', line 9

def FlagsEvaluation.set_route(route)
  @@route = route
  @@cache = Hash.new
end

.set_verbose(verbose) ⇒ Object



14
15
16
# File 'lib/flags_sdk_ort.rb', line 14

def FlagsEvaluation.set_verbose(verbose)
  @@verbose = verbose
end