Class: BulletTrainClient

Inherits:
Object
  • Object
show all
Defined in:
lib/bullet-train.rb

Constant Summary collapse

@@apiUrl =
""
@@environmentKey =
""

Instance Method Summary collapse

Constructor Details

#initialize(apiKey = nil, apiUrl = "https://api.bullet-train.io/api/v1/") ⇒ BulletTrainClient

Returns a new instance of BulletTrainClient.



9
10
11
12
# File 'lib/bullet-train.rb', line 9

def initialize(apiKey = nil, apiUrl = "https://api.bullet-train.io/api/v1/")
  @@environmentKey = apiKey
  @@apiUrl = apiUrl
end

Instance Method Details

#getFlagsObject



37
38
39
# File 'lib/bullet-train.rb', line 37

def getFlags()
  processFlags(getJSON("flags/"))
end

#getFlagsForUser(identity = nil) ⇒ Object



33
34
35
# File 'lib/bullet-train.rb', line 33

def getFlagsForUser(identity = nil)
  processFlags(getJSON("flags/#{identity}"))
end

#getJSON(method = nil) ⇒ Object



14
15
16
17
18
# File 'lib/bullet-train.rb', line 14

def getJSON(method = nil)
  response = open(@@apiUrl + "" + method.concat("?format=json"),
                  "x-environment-key" => @@environmentKey).read
  return JSON.parse(response)
end

#getValue(key, userId = nil) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/bullet-train.rb', line 41

def getValue(key, userId = nil)
  flags = nil
  # Get the features
  if userId != nil
    flags = getFlagsForUser(userId)
  else
    flags = getFlags()
  end
  # Return the value
  return flags[key]["value"]
end

#hasFeature(key, userId = nil) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/bullet-train.rb', line 53

def hasFeature(key, userId = nil)
  # Get the features
  flags = nil
  if userId != nil
    flags = getFlagsForUser(userId)
  else
    flags = getFlags()
  end

  # Work out if this feature exists
  if flags[key] == nil
    return false
  else
    return flags[key]["enabled"]
  end
end

#processFlags(inputFlags) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/bullet-train.rb', line 20

def processFlags(inputFlags)
  flags = {}

  for feature in inputFlags
    featureName = feature["feature"]["name"].downcase.gsub(/\s+/, "_")
    enabled = feature["enabled"]
    state = feature["feature_state_value"]
    flags[featureName] = {"enabled" => enabled, "value" => state}
  end

  return flags
end