Class: LibFinder::LibWorker

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/libFinder.rb

Class Method Summary collapse

Class Method Details

.get_dependencies(gems) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/libFinder.rb', line 54

def self.get_dependencies(gems)
    
  response=HTTParty.post("http://localhost:3000/find_dependencies",
    :body => {gems:gems,
          os_id:self.get_os
         }.to_json,
    :headers => { 'Content-Type' => 'application/json' } 
    )

  dependencies = JSON.parse(response.body)
  if dependencies.count>0
    #calles the downloading and installing of the missing deps method
    #if the dependencies are greater than one
    self.install_libs(dependencies)
  end
    
end

.get_osObject



72
73
74
75
76
77
78
79
80
# File 'lib/libFinder.rb', line 72

def self.get_os
  if (/linux/ =~ RbConfig::CONFIG["host_os"]) != nil
    return 1 #this is the id of the os that sits in the database 
  end

  if (/darwin/ =~ RbConfig::CONFIG["host_os"]) != nil
    return 2 #this is the id of the os that sits in the database 
  end
end

.install_libs(deps) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/libFinder.rb', line 82

def self.install_libs(deps)
  #this function recives the missing dependencies that are retrived
  #by the web service and start to download them based on the current os
  command ="" # this is the line that is responsible for building the system command string
  if self.get_os == 1
    command += "sudo apt-get install "  #in ubunto and debian distros
    deps.each do |dep|
      #a loop to go through all the dependencies to build the command string
      command += dep["name"]+" " #adding the lib name to the command
    end 

  elsif self.get_os == 2
    #if the current os is mac
    command += "brew install "
    deps.each do |dep|
      #a loop to go through all the dependencies to build the command string
      command += dep["name"]+" " #adding the lib name to the command
    end 
  end 
  #running the command
  puts command
  system(command)

end

.is_railsObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/libFinder.rb', line 15

def self.is_rails
  base_path = Dir.pwd
  controllers = File.exist?(base_path+"/app/controllers")
  #check if the file has a controllers folder or not
  models = File.exist?(base_path+"/app/models")
  #check if the file has a models folder or not
  if controllers && models
    #if both are true then it is a rails app 
    return true 
  else 
    puts "this is not a rails application file please change directory to a rails application file"
    return false
  end
end

.parse_gem_fileObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/libFinder.rb', line 30

def self.parse_gem_file
  gems = []
  File.readlines(Dir.pwd+"/Gemfile").each do |line|
    line_data = line.split
    regex=/\d+\.\d+\.*\d*/x



    if line_data[0] == "gem" 

      gem_info = {name: line_data[1].gsub(/[', ]/, '')  ,version:""}
      line_data.each do |data|
        if data.match regex
          gem_info[:version] = data.gsub(/[',]/,'');
          break
        end
      end
      gems.push(gem_info)
    end
  end
  self.get_dependencies(gems)
end

.sniffObject



8
9
10
11
12
13
# File 'lib/libFinder.rb', line 8

def self.sniff
  # a function to start the gem working used sniff cause i saw my dog doing so when searching for something :D
  if(self.is_rails)
    self.parse_gem_file
  end
end