Module: Supso::Util
- Defined in:
- lib/supso/util.rb
Class Method Summary collapse
- .camelcase_to_underscore(str) ⇒ Object
- .deep_merge(first, second) ⇒ Object
- .detect_project_root ⇒ Object
- .ensure_path_exists!(full_path) ⇒ Object
- .has_command?(command) ⇒ Boolean
- .http_get(url) ⇒ Object
- .http_post(url, data = {}) ⇒ Object
- .is_email?(email) ⇒ Boolean
- .pluralize(count, word) ⇒ Object
- .project_root?(path) ⇒ Boolean
- .require_all_gems! ⇒ Object
- .sanitize_command(command) ⇒ Object
- .underscore_to_camelcase(str) ⇒ Object
- .which(command) ⇒ Object
Class Method Details
.camelcase_to_underscore(str) ⇒ Object
134 135 136 137 138 139 140 |
# File 'lib/supso/util.rb', line 134 def Util.camelcase_to_underscore(str) str.gsub(/::/, '/') .gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2') .gsub(/([a-z\d])([A-Z])/,'\1_\2') .tr("-", "_") .downcase end |
.deep_merge(first, second) ⇒ Object
7 8 9 10 |
# File 'lib/supso/util.rb', line 7 def Util.deep_merge(first, second) merger = proc { |key, v1, v2| Hash === v1 && Hash === v2 ? v1.merge(v2, &merger) : v2 } first.merge(second, &merger) end |
.detect_project_root ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/supso/util.rb', line 12 def Util.detect_project_root project_root = Dir.getwd while true if project_root == "" project_root = nil break end if Util.project_root?(project_root) break end detect_project_root_splits = project_root.split("/") detect_project_root_splits = detect_project_root_splits[0..detect_project_root_splits.length - 2] project_root = detect_project_root_splits.join("/") end if project_root == nil || project_root == '' project_root = Dir.getwd end project_root end |
.ensure_path_exists!(full_path) ⇒ Object
47 48 49 50 51 52 53 |
# File 'lib/supso/util.rb', line 47 def Util.ensure_path_exists!(full_path) split_paths = full_path.split('/') just_file_path = split_paths.pop directory_path = split_paths.join('/') FileUtils.mkdir_p(directory_path) FileUtils.touch("#{ directory_path }/#{ just_file_path }") end |
.has_command?(command) ⇒ Boolean
55 56 57 |
# File 'lib/supso/util.rb', line 55 def Util.has_command?(command) !!Util.which(command) end |
.http_get(url) ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/supso/util.rb', line 69 def Util.http_get(url) json_headers = { "Content-Type" => "application/json", "Accept" => "application/json", } uri = URI.parse(url) http = Net::HTTP.new(uri.host, uri.port) if url.start_with?('https://') http.use_ssl = true end response = http.get(uri.path, json_headers) if response.code.to_i == 200 return JSON.parse(response.body) else raise StandardError.new("Error #{ response } for #{ url }") end end |
.http_post(url, data = {}) ⇒ Object
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/supso/util.rb', line 88 def Util.http_post(url, data = {}) json_headers = { "Content-Type" => "application/json", "Accept" => "application/json", } data[:version] = Supso::VERSION uri = URI.parse(url) http = Net::HTTP.new(uri.host, uri.port) if url.start_with?('https://') http.use_ssl = true end response = http.post(uri.path, data.to_json, json_headers) if response.code.to_i == 200 return JSON.parse(response.body) else raise StandardError.new("Error #{ response } for #{ url }") end end |
.is_email?(email) ⇒ Boolean
110 111 112 |
# File 'lib/supso/util.rb', line 110 def Util.is_email?(email) !!/\A[^@]+@([^@\.]+\.)+[^@\.]+\z/.match(email) end |
.pluralize(count, word) ⇒ Object
114 115 116 117 118 119 120 |
# File 'lib/supso/util.rb', line 114 def Util.pluralize(count, word) if count == 1 word else "#{ word }s" end end |
.project_root?(path) ⇒ Boolean
36 37 38 39 40 41 42 43 44 45 |
# File 'lib/supso/util.rb', line 36 def Util.project_root?(path) base_file_names = ['Gemfile', 'package.json', '.supso', 'environment.yml', 'requirements.txt', 'setup.py'] for name in base_file_names if File.exist?(path + '/' + name) return true end end false end |
.require_all_gems! ⇒ Object
122 123 124 125 126 127 128 |
# File 'lib/supso/util.rb', line 122 def Util.require_all_gems! begin Bundler.require(:default, :development, :test, :production) rescue Gem::LoadError, Bundler::GemfileNotFound # Keep going end end |
.sanitize_command(command) ⇒ Object
65 66 67 |
# File 'lib/supso/util.rb', line 65 def Util.sanitize_command(command) command.gsub(/[^-_\w]/, '') end |
.underscore_to_camelcase(str) ⇒ Object
130 131 132 |
# File 'lib/supso/util.rb', line 130 def Util.underscore_to_camelcase(str) str.split('_').map{ |chunk| chunk.capitalize }.join end |
.which(command) ⇒ Object
59 60 61 62 63 |
# File 'lib/supso/util.rb', line 59 def Util.which(command) command = Util.sanitize_command(command) response = `which #{ command }` response && response.length > 0 ? response : nil end |