Class: OpenProject::Token

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Validations
Defined in:
lib/open_project/token.rb,
lib/open_project/token/armor.rb,
lib/open_project/token/version.rb,
lib/open_project/token/extractor.rb

Defined Under Namespace

Modules: Armor Classes: Error, Extractor, ImportError, ParseError, ValidationError

Constant Summary collapse

VERSION =
"2.1.1"

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Token

Returns a new instance of Token.



73
74
75
# File 'lib/open_project/token.rb', line 73

def initialize(attributes = {})
  load_attributes(attributes)
end

Class Attribute Details

.extractorObject (readonly)

Returns the value of attribute extractor.



20
21
22
# File 'lib/open_project/token.rb', line 20

def extractor
  @extractor
end

.keyObject

Returns the value of attribute key.



20
21
22
# File 'lib/open_project/token.rb', line 20

def key
  @key
end

Instance Attribute Details

#block_changes_atObject

Returns the value of attribute block_changes_at.



54
55
56
# File 'lib/open_project/token.rb', line 54

def block_changes_at
  @block_changes_at
end

#companyObject

Returns the value of attribute company.



52
53
54
# File 'lib/open_project/token.rb', line 52

def company
  @company
end

#domainObject

Returns the value of attribute domain.



52
53
54
# File 'lib/open_project/token.rb', line 52

def domain
  @domain
end

#expires_atObject

Returns the value of attribute expires_at.



53
54
55
# File 'lib/open_project/token.rb', line 53

def expires_at
  @expires_at
end

#issued_atObject

Returns the value of attribute issued_at.



53
54
55
# File 'lib/open_project/token.rb', line 53

def issued_at
  @issued_at
end

#mailObject

Returns the value of attribute mail.



52
53
54
# File 'lib/open_project/token.rb', line 52

def mail
  @mail
end

#notify_admins_atObject

Returns the value of attribute notify_admins_at.



54
55
56
# File 'lib/open_project/token.rb', line 54

def notify_admins_at
  @notify_admins_at
end

#notify_users_atObject

Returns the value of attribute notify_users_at.



54
55
56
# File 'lib/open_project/token.rb', line 54

def notify_users_at
  @notify_users_at
end

#restrictionsObject

Returns the value of attribute restrictions.



55
56
57
# File 'lib/open_project/token.rb', line 55

def restrictions
  @restrictions
end

#starts_atObject

Returns the value of attribute starts_at.



53
54
55
# File 'lib/open_project/token.rb', line 53

def starts_at
  @starts_at
end

#subscriberObject

Returns the value of attribute subscriber.



52
53
54
# File 'lib/open_project/token.rb', line 52

def subscriber
  @subscriber
end

#versionObject (readonly)

Returns the value of attribute version.



51
52
53
# File 'lib/open_project/token.rb', line 51

def version
  @version
end

Class Method Details

.import(data) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/open_project/token.rb', line 31

def import(data)
  raise ImportError, "Missing key." if key.nil?
  raise ImportError, "No token data." if data.nil?

  data = Armor.decode(data)
  json = extractor.read(data)
  attributes = JSON.parse(json)

  new(attributes)
rescue Extractor::Error
  raise ImportError, "Token value could not be read."
rescue JSON::ParserError
  raise ImportError, "Token value is invalid JSON."
rescue Armor::ParseError
  raise ImportError, "Token value could not be parsed."
end

Instance Method Details

#attributesObject



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/open_project/token.rb', line 122

def attributes
  hash = {}

  hash["version"]          = self.version
  hash["subscriber"]       = self.subscriber
  hash["mail"]             = self.mail
  hash["company"]          = self.company
  hash["domain"]           = self.domain

  hash["issued_at"]        = self.issued_at
  hash["starts_at"]        = self.starts_at
  hash["expires_at"]       = self.expires_at       if self.will_expire?

  hash["notify_admins_at"] = self.notify_admins_at if self.will_notify_admins?
  hash["notify_users_at"]  = self.notify_users_at  if self.will_notify_users?
  hash["block_changes_at"] = self.block_changes_at if self.will_block_changes?

  hash["restrictions"]     = self.restrictions     if self.restricted?

  hash
end

#block_changes?Boolean

Returns:

  • (Boolean)


105
106
107
# File 'lib/open_project/token.rb', line 105

def block_changes?
  will_block_changes? && Date.today >= self.block_changes_at
end

#expired?Boolean

Returns:

  • (Boolean)


93
94
95
# File 'lib/open_project/token.rb', line 93

def expired?
  will_expire? && Date.today >= self.expires_at
end

#from_json(json) ⇒ Object



148
149
150
151
152
# File 'lib/open_project/token.rb', line 148

def from_json(json)
  load_attributes(JSON.parse(json))
rescue => e
  raise ParseError, "Failed to load from json: #{e}"
end

#notify_admins?Boolean

Returns:

  • (Boolean)


97
98
99
# File 'lib/open_project/token.rb', line 97

def notify_admins?
  will_notify_admins? && Date.today >= self.notify_admins_at
end

#notify_users?Boolean

Returns:

  • (Boolean)


101
102
103
# File 'lib/open_project/token.rb', line 101

def notify_users?
  will_notify_users? && Date.today >= self.notify_users_at
end

#restricted?(key = nil) ⇒ Boolean

Returns:

  • (Boolean)


114
115
116
117
118
119
120
# File 'lib/open_project/token.rb', line 114

def restricted?(key = nil)
  if key
    restricted? && restrictions.has_key?(key)
  else
    restrictions && restrictions.length >= 1
  end
end

#to_jsonObject



144
145
146
# File 'lib/open_project/token.rb', line 144

def to_json
  JSON.dump(self.attributes)
end

#validate_domain?Boolean

tokens with no version or a version lower than 2.0 don’t have the attributes company or domain

Returns:

  • (Boolean)


110
111
112
# File 'lib/open_project/token.rb', line 110

def validate_domain?
  version && Gem::Version.new(version) >= domain_required_from_version
end

#will_block_changes?Boolean

Returns:

  • (Boolean)


89
90
91
# File 'lib/open_project/token.rb', line 89

def will_block_changes?
  self.block_changes_at
end

#will_expire?Boolean

Returns:

  • (Boolean)


77
78
79
# File 'lib/open_project/token.rb', line 77

def will_expire?
  self.expires_at
end

#will_notify_admins?Boolean

Returns:

  • (Boolean)


81
82
83
# File 'lib/open_project/token.rb', line 81

def will_notify_admins?
  self.notify_admins_at
end

#will_notify_users?Boolean

Returns:

  • (Boolean)


85
86
87
# File 'lib/open_project/token.rb', line 85

def will_notify_users?
  self.notify_users_at
end