Class: SuperSource::Project

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

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.



9
10
11
12
13
14
15
16
# File 'lib/super_source/project.rb', line 9

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

Class Attribute Details

.projectsObject

Returns the value of attribute projects.



100
101
102
# File 'lib/super_source/project.rb', line 100

def projects
  @projects
end

Instance Attribute Details

#api_tokenObject

Returns the value of attribute api_token.



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

def api_token
  @api_token
end

#client_dataObject

Returns the value of attribute client_data.



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

def client_data
  @client_data
end

#client_tokenObject

Returns the value of attribute client_token.



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

def client_token
  @client_token
end

#nameObject

Returns the value of attribute name.



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

def name
  @name
end

Class Method Details

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



107
108
109
110
111
# File 'lib/super_source/project.rb', line 107

def self.add(name, api_token, options = {})
  project = Project.new(name, api_token, options)
  self.projects << project
  project.ensure_required!
end

Instance Method Details

#data_filenameObject



51
52
53
# File 'lib/super_source/project.rb', line 51

def data_filename
  self.filename('json')
end

#ensure_required!Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/super_source/project.rb', line 18

def ensure_required!
  if SuperSource.project_root.nil?
    self.report_problem!("Could not detect a Supported Source project root in working directory or any parent " +
            "directories. Are you sure you're running this from the right place?",
        MissingProjectRoot)
    return
  end

  if !self.token_file_exists?
    self.report_problem!("Missing Supported Source token for " +
                "project: #{ name }.#{ Exceptions.help_message }", MissingProjectToken)
    return
  end

  if !self.valid?
    self.report_problem!("Invalid Supported Source token for project: #{ name }.#{ Exceptions.help_message }",
        InvalidProjectToken)
    return
  end
end

#filename(filetype) ⇒ Object



47
48
49
# File 'lib/super_source/project.rb', line 47

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

#load_client_dataObject



55
56
57
58
59
60
61
# File 'lib/super_source/project.rb', line 55

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

#load_client_tokenObject



63
64
65
66
67
68
69
# File 'lib/super_source/project.rb', line 63

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

#report_problem!(message, error_class) ⇒ Object



39
40
41
42
43
44
45
# File 'lib/super_source/project.rb', line 39

def report_problem!(message, error_class)
  if @options[:level] == :warn
    warn("WARNING: " + message)
  elsif @options[:level] == :error
    raise error_class.new(message)
  end
end

#token_file_exists?Boolean

Returns:

  • (Boolean)


75
76
77
# File 'lib/super_source/project.rb', line 75

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

#token_filenameObject



71
72
73
# File 'lib/super_source/project.rb', line 71

def token_filename
  self.filename('token')
end

#valid?Boolean

Returns:

  • (Boolean)


79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/super_source/project.rb', line 79

def valid?
  if !self.client_token || !self.client_data
    return false
  end

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

  if !Organization.current_organization ||
      self.client_data['organization_id'] != Organization.current_organization.id
    return false
  end

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

  public_key.verify(digest, Base64.decode64(self.client_token), self.client_data.to_json)
end