Class: Supso::Project

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

Constant Summary collapse

MISSING_DATA =

Validities

:missing_token
MISSING_TOKEN =
:missing_token
DIFFERENT_API_TOKEN =
:different_api_token
INVALID_TOKEN =
:missing_token
MISSING_ORGANIZATION =
:missing_organization
DIFFERENT_ORGANIZATION =
:different_organization
VALID =
:valid

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, api_token, options = {}) ⇒ Project

Returns a new instance of Project.



18
19
20
21
22
23
24
25
26
# File 'lib/supso/project.rb', line 18

def initialize(name, api_token, options = {})
  @name = name
  @api_token = api_token
  @options = options
  @client_data = self.load_client_data
  @client_token = self.load_client_token
  @source = options[:source] || options['source']
  @aliases = options[:aliases] || options['aliases'] || []
end

Class Attribute Details

.projectsObject

Returns the value of attribute projects.



174
175
176
# File 'lib/supso/project.rb', line 174

def projects
  @projects
end

Instance Attribute Details

#aliasesObject

Returns the value of attribute aliases.



7
8
9
# File 'lib/supso/project.rb', line 7

def aliases
  @aliases
end

#api_tokenObject

Returns the value of attribute api_token.



7
8
9
# File 'lib/supso/project.rb', line 7

def api_token
  @api_token
end

#client_dataObject

Returns the value of attribute client_data.



7
8
9
# File 'lib/supso/project.rb', line 7

def client_data
  @client_data
end

#client_tokenObject

Returns the value of attribute client_token.



7
8
9
# File 'lib/supso/project.rb', line 7

def client_token
  @client_token
end

#nameObject

Returns the value of attribute name.



7
8
9
# File 'lib/supso/project.rb', line 7

def name
  @name
end

#sourceObject

Returns the value of attribute source.



7
8
9
# File 'lib/supso/project.rb', line 7

def source
  @source
end

Class Method Details

.add(name, api_token, options = {}) ⇒ Object



181
182
183
184
185
186
187
188
189
# File 'lib/supso/project.rb', line 181

def self.add(name, api_token, options = {})
  # Correct for common mistakes:
  options[:aliases] = options['aliases'] if options['aliases'] && !options[:aliases]
  options[:source] = options['source'] if options['source'] && !options[:source]

  options[:source] ||= 'add'
  project = Project.new(name, api_token, options)
  self.projects << project
end

.aliases_match?(aliases1 = [], aliases2 = []) ⇒ Boolean

Returns:

  • (Boolean)


207
208
209
210
211
212
213
214
215
216
# File 'lib/supso/project.rb', line 207

def self.aliases_match?(aliases1 = [], aliases2 = [])
  aliases2.each do |first_alias|
    if aliases1.any? { |second_alias| second_alias['name'] == first_alias['name'] &&
        second_alias['platform'] == first_alias['platform'] }
      return true
    end
  end

  false
end

.detect_all_projects!Object



191
192
193
194
# File 'lib/supso/project.rb', line 191

def self.detect_all_projects!
  Util.require_all_gems!
  Javascript.detect_all_projects!
end

.get_from_references(references) ⇒ Object



218
219
220
221
222
223
224
225
226
227
228
# File 'lib/supso/project.rb', line 218

def self.get_from_references(references)
  result_projects = []
  references.each do |name|
    project = Project.projects.find { |find_project| find_project.name == name }
    if project.nil?
      project = Project.new(name, nil)
    end
    result_projects << project
  end
  result_projects
end

.save_project_directory_readme!Object



196
197
198
199
200
201
202
203
204
205
# File 'lib/supso/project.rb', line 196

def self.save_project_directory_readme!
  readme_path = "#{ Supso.project_supso_config_root }/README.txt"
  if !File.exists?(readme_path)
    readme_contents = File.open("#{ Supso.gem_root }/lib/templates/project_dir_readme.txt", 'r').read
    Util.ensure_path_exists!(readme_path)
    file = File.open(readme_path, 'w')
    file << readme_contents
    file.close
  end
end

Instance Method Details

#data_filenameObject



32
33
34
# File 'lib/supso/project.rb', line 32

def data_filename
  self.filename('json')
end

#filename(filetype) ⇒ Object



28
29
30
# File 'lib/supso/project.rb', line 28

def filename(filetype)
  "#{ Supso.project_supso_config_root }/projects/#{ self.name }.#{ filetype }"
end

#identification_dataObject



36
37
38
39
40
41
42
43
# File 'lib/supso/project.rb', line 36

def identification_data
  {
      name: self.name,
      api_token: self.api_token,
      aliases: self.aliases,
      source: self.source,
  }
end

#load_client_dataObject



61
62
63
64
65
66
67
# File 'lib/supso/project.rb', line 61

def load_client_data
  if File.exist?(self.data_filename)
    JSON.parse(File.read(self.data_filename))
  else
    {}
  end
end

#load_client_tokenObject



69
70
71
72
73
74
75
# File 'lib/supso/project.rb', line 69

def load_client_token
  if self.token_file_exists?
    File.read(self.token_filename)
  else
    nil
  end
end

#organization_idObject



85
86
87
# File 'lib/supso/project.rb', line 85

def organization_id
  self.client_data['organization_id']
end

#puts_infoObject



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/supso/project.rb', line 45

def puts_info
  puts "#{ self.name }"

  if self.source
    human_readable_source = self.source == 'add' ? 'add (ruby)' : self.source
    puts "  Source: #{ human_readable_source }"
  end

  if self.valid?
    puts "  Valid: Yes"
  else
    puts "  Valid: No"
    puts "  Reason: #{ self.validity_explanation }"
  end
end

#save_data!Object



169
170
171
# File 'lib/supso/project.rb', line 169

def save_data!
  self.save_project_data
end

#save_project_data!Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/supso/project.rb', line 89

def save_project_data!
  if self.client_data.empty?
    if File.exists?(self.data_filename)
      File.delete(self.data_filename)
    end
    if File.exists?(self.token_filename)
      File.delete(self.token_filename)
    end
  else
    Project.save_project_directory_readme!

    Util.ensure_path_exists!(self.data_filename)
    file = File.open(self.data_filename, 'w')
    file << self.client_data.to_json
    file.close

    Util.ensure_path_exists!(self.token_filename)
    file = File.open(self.token_filename, 'w')
    file << self.client_token
    file.close
  end
end

#token_file_exists?Boolean

Returns:

  • (Boolean)


81
82
83
# File 'lib/supso/project.rb', line 81

def token_file_exists?
  File.exist?(self.token_filename)
end

#token_filenameObject



77
78
79
# File 'lib/supso/project.rb', line 77

def token_filename
  self.filename('token')
end

#valid?Boolean

Returns:

  • (Boolean)


112
113
114
# File 'lib/supso/project.rb', line 112

def valid?
  self.validity == VALID
end

#validityObject



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/supso/project.rb', line 116

def validity
  if !self.client_token
    return MISSING_TOKEN
  end

  if !self.client_data
    return MISSING_DATA
  end

  if self.client_data['project_api_token'] != self.api_token
    return DIFFERENT_API_TOKEN
  end

  if !Organization.current_organization
    return MISSING_ORGANIZATION
  end

  if self.organization_id != Organization.current_organization.id
    return DIFFERENT_ORGANIZATION
  end

  public_key = OpenSSL::PKey::RSA.new File.read("#{ Supso.gem_root }/lib/other/supso2.pub")
  digest = OpenSSL::Digest::SHA256.new

  if !public_key.verify(digest, Base64.decode64(self.client_token), self.client_data.to_json)
    return INVALID_TOKEN
  end

  return VALID
end

#validity_explanationObject



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/supso/project.rb', line 147

def validity_explanation
  case self.validity
  when MISSING_TOKEN
    "Missing client token. Run `supso update` to update the token."
  when MISSING_DATA
    "Missing client data. Run `supso update` to update the data."
  when DIFFERENT_API_TOKEN
    "Different api token. The project's api token is different from the project api token listed in your client data. Make sure your projects are all up-to-date and you have the latest client token from `supso update`."
  when MISSING_ORGANIZATION
    "Missing organization. Run `supso update` to update your organization file."
  when DIFFERENT_ORGANIZATION
    "Different organization. The client token uses organization id #{ organization_id }, but " +
        "your current organization id is #{ Organization.current_organization['id'] } (#{ Organization.current_organization['name'] })."
  when INVALID_TOKEN
    "Invalid client token. Run `supso update` to update the token."
  when VALID
    "Valid."
  else
    "Invalid."
  end
end