class LibFinder::LibWorker

Public Class Methods

get_dependencies(gems) click to toggle source
# File lib/libFinder.rb, line 59
def self.get_dependencies(gems)
        #sending the post request to the webservice
        response=HTTParty.post("https://whispering-journey-17183.herokuapp.com/find_dependencies",
                :body => {gems:gems,
                                  os_id:self.get_os
                                 }.to_json,
                :headers => { 'Content-Type' => 'application/json' } 
                )

        dependencies = JSON.parse(response.body)
        #parsing the response from string to jsn format
        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_os() click to toggle source
# File lib/libFinder.rb, line 78
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) click to toggle source
# File lib/libFinder.rb, line 88
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 " #in mac os
                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
        system(command)

end
is_rails() click to toggle source
# File lib/libFinder.rb, line 19
 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_file() click to toggle source
# File lib/libFinder.rb, line 34
def self.parse_gem_file
        gems = []
        #reading the file line by line
        File.readlines(Dir.pwd+"/Gemfile").each do |line|
                #spliting the the read line by spaces
                line_data = line.split
                #creating a regular expression that is used to match the versions
                regex=/\d+\.\d+\.*\d*/x

                if line_data[0] == "gem" 
                        #if the line starts with the word gem then it is a gem and i will be parsed
                        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
sniff() click to toggle source
# 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)
                #checking if we are in a rails file or not
                self.parse_gem_file
        else
                puts "please run this command in a rails app root folder"
        end

end