-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathmodels.rb
More file actions
75 lines (66 loc) · 2.43 KB
/
models.rb
File metadata and controls
75 lines (66 loc) · 2.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# frozen_string_literal: true
module Flagsmith
module Engine
# EnvironmentModel
class Environment
attr_reader :id, :api_key, :name
attr_accessor :project, :feature_states, :amplitude_config, :segment_config,
:mixpanel_config, :heap_config, :identity_overrides
# rubocop:disable Metrics/ParameterLists
def initialize(id:, api_key:, project:, name: nil, feature_states: [], identity_overrides: [])
@id = id
@api_key = api_key
@name = name
@project = project
@feature_states = feature_states
@identity_overrides = identity_overrides
end
# rubocop:enable Metrics/ParameterLists
class << self
# rubocop:disable Metrics/MethodLength
def build(json)
project = Flagsmith::Engine::Project.build(json[:project])
feature_states = json[:feature_states].map do |fs|
Flagsmith::Engine::FeatureState.build(fs)
end
identity_overrides = json.fetch(:identity_overrides, []).map do |io|
Flagsmith::Engine::Identity.build(io)
end
new(**json.slice(:id, :api_key, :name).merge(
project: project,
feature_states: feature_states,
identity_overrides: identity_overrides
))
end
# rubocop:enable Metrics/MethodLength
end
end
module Environments
# EnvironmentApiKeyModel
class ApiKey
attr_reader :id, :key, :created_at, :name, :client_api_key
attr_accessor :expires_at, :active
def initialize(params = {})
@id = params.fetch(:id)
@key = params.fetch(:key)
@name = params.fetch(:name)
@client_api_key = params.fetch(:client_api_key)
@created_at = params.fetch(:created_at, Time.now)
@expires_at = params.fetch(:expires_at, nil)
@active = params.fetch(:active, true)
end
def valid?
active && (!expires_at || expires_at > Time.now)
end
class << self
def build(json)
attributes = json.slice(:id, :key, :name, :client_api_key, :active)
attributes = attributes.merge(expires_at: Date.parse(json[:created_at])) unless json[:created_at].nil?
attributes = attributes.merge(expires_at: Date.parse(json[:expires_at])) unless json[:expires_at].nil?
new(**attributes)
end
end
end
end
end
end