Module: ToolPouch::Trail
- Defined in:
- lib/tool_pouch/trail.rb
Class Method Summary collapse
- .build_querystring(form_data) ⇒ Object
- .directories(path) ⇒ Object
- .extension(path) ⇒ Object
- .filename_without_extension(path, options = {}) ⇒ Object
- .filename_without_path(path) ⇒ Object
- .get_all_files_with_extension(path, extensions = []) ⇒ Object
- .get_domain(url) ⇒ Object
- .get_file(root, filenames) ⇒ Object
- .get_files(root, filenames, options = {}) ⇒ Object
- .get_first_file_with_extension(path, extensions = []) ⇒ Object
- .get_first_match(path, value) ⇒ Object
- .get_querystring(url) ⇒ Object
- .get_section(url, options = {}) ⇒ Object
- .is_file_with_extension?(path) ⇒ Boolean
- .is_url?(path) ⇒ Boolean
- .last_directory(path) ⇒ Object
- .open_and_merge(directories, options = {}) ⇒ Object
- .order_querystring_alpha(url) ⇒ Object
- .partial_exists?(partial_short_path) ⇒ Boolean
- .refresh_file_content(path, content) ⇒ Object
- .slice_trailing_slash(path) ⇒ Object
- .strip_scheme(uri) ⇒ Object
- .trailing_slash?(path) ⇒ Boolean
- .yml_content(file) ⇒ Object
Class Method Details
.build_querystring(form_data) ⇒ Object
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/tool_pouch/trail.rb', line 4 def self.build_querystring(form_data) query_string = '' if form_data params = [] if not form_data.empty? form_data.each do |d| if d[1].is_a?(Hash) d[1].each do |key, value| params << "#{d[0]}[#{key}]=#{CGI.escape(CGI.unescape(value.to_s))}" end else params << "#{d[0]}=#{CGI.escape(CGI.unescape(d[1].to_s))}" end end end query_string = params.sort.join('&') end return query_string end |
.directories(path) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/tool_pouch/trail.rb', line 24 def self.directories(path) directory = '' if path and path.class == String last_slash_index = path.rindex('/') if last_slash_index.to_i > 0 directory = path[0, last_slash_index] end end return directory end |
.extension(path) ⇒ Object
37 38 39 40 41 42 43 44 45 46 |
# File 'lib/tool_pouch/trail.rb', line 37 def self.extension(path) file_extension = '' if path file_extension_with_dot = File.extname(path) if not file_extension_with_dot.empty? file_extension = file_extension_with_dot[1, file_extension_with_dot.length] end end return file_extension end |
.filename_without_extension(path, options = {}) ⇒ Object
48 49 50 51 52 53 54 |
# File 'lib/tool_pouch/trail.rb', line 48 def self.filename_without_extension(path, ={}) .reverse_merge!(:full_path => true) filename = [:full_path] ? path : filename_without_path(path) return (filename and filename.rindex('.')) ? filename[0, filename.rindex('.')] : filename end |
.filename_without_path(path) ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/tool_pouch/trail.rb', line 56 def self.filename_without_path(path) filename_without_path = path if path #Handle properly path without filename if trailing_slash?(path) filename_without_path = '' else filename_without_path = File.basename(path) end end return filename_without_path end |
.get_all_files_with_extension(path, extensions = []) ⇒ Object
88 89 90 91 92 93 94 95 96 |
# File 'lib/tool_pouch/trail.rb', line 88 def self.get_all_files_with_extension(path, extensions=[]) extensions = [extensions] unless extensions.is_a? Array extensions.collect!{|x| ".#{x}"} files = [] Find.find(path) { |f| files << f if File.file?(f) and extensions.include? File.extname(f) } return files end |
.get_domain(url) ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/tool_pouch/trail.rb', line 69 def self.get_domain(url) domain = nil if url # add protocol so it becomes a valid URI url = ('http://' + url) unless url =~ /^https?:\/\// # remove %'s, which we don't need and could cause an URL to become invalid url.gsub!('%', '') begin # get the host part, and remove the www. if present domain = Addressable::URI.parse(url).host.gsub(/^www\./i, '') rescue "" end end return domain end |
.get_file(root, filenames) ⇒ Object
98 99 100 101 102 |
# File 'lib/tool_pouch/trail.rb', line 98 def self.get_file(root, filenames) files = get_files(root, filenames) return files.empty? ? nil : files.first end |
.get_files(root, filenames, options = {}) ⇒ Object
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/tool_pouch/trail.rb', line 104 def self.get_files(root, filenames, ={}) .reverse_merge!(:perfect_match => true) filenames = [filenames] unless filenames.is_a? Array root = slice_trailing_slash(root) files = [] Find.find(root) do |f| if [:perfect_match] files << f.gsub(root + '/', '') if File.file?(f) and filenames.include?(filename_without_path(f)) else found = false filename_only = filename_without_path(f) filenames.each{ |m| found = true if filename_only.match(/#{m}/) } files << f.gsub(root + '/', '') if File.file?(f) and found end end return files end |
.get_first_file_with_extension(path, extensions = []) ⇒ Object
125 126 127 |
# File 'lib/tool_pouch/trail.rb', line 125 def self.get_first_file_with_extension(path, extensions=[]) return get_all_files_with_extension(path, extensions).first end |
.get_first_match(path, value) ⇒ Object
129 130 131 132 133 134 135 136 137 138 |
# File 'lib/tool_pouch/trail.rb', line 129 def self.get_first_match(path, value) File.open(path) do |io| io.each do |line| line.chomp! return line if line.include? value end end return nil end |
.get_querystring(url) ⇒ Object
140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/tool_pouch/trail.rb', line 140 def self.get_querystring(url) raw = URI.split(url)[7] qs = {} if raw raw.split('&').each do |full_param| key, value = full_param.split('=') qs[key] = value end end return qs end |
.get_section(url, options = {}) ⇒ Object
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
# File 'lib/tool_pouch/trail.rb', line 155 def self.get_section(url, ={}) section = nil if url if [:base_path] section = url.gsub([:base_path] + '/', '').split('/')[0] else section = url.split('/')[-1, 1].to_s end # Remove format if it was specified section = File.basename(section, '.*') end return section end |
.is_file_with_extension?(path) ⇒ Boolean
171 172 173 174 175 176 177 178 179 180 |
# File 'lib/tool_pouch/trail.rb', line 171 def self.is_file_with_extension?(path) file_with_extension = false if path extension = File.extname(path) if extension and not extension.empty? file_with_extension = true end end return file_with_extension end |
.is_url?(path) ⇒ Boolean
182 183 184 |
# File 'lib/tool_pouch/trail.rb', line 182 def self.is_url?(path) return (path.to_s.include?('http://') or path.to_s.include?('https://')) ? true : false end |
.last_directory(path) ⇒ Object
186 187 188 189 190 |
# File 'lib/tool_pouch/trail.rb', line 186 def self.last_directory(path) path += 'dummystring' if trailing_slash?(path) return path.split('/')[-2, 1] end |
.open_and_merge(directories, options = {}) ⇒ Object
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 |
# File 'lib/tool_pouch/trail.rb', line 192 def self.open_and_merge(directories, ={}) .reverse_merge!(:extensions => '') directories = [directories] if not directories.is_a? Array case [:extensions] when 'yml' then content = HashTree.new else content = [] end directories.each do |directory| Find.find(directory) do |p| next unless File.file?(p) case [:extensions] when 'yml' then content.merge(yml_content(p)) when 'xml' then content << File.open(p, "r").readlines.join end end end case [:extensions] when 'yml' then content.get else return content.join("\n") end end |
.order_querystring_alpha(url) ⇒ Object
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 |
# File 'lib/tool_pouch/trail.rb', line 219 def self.order_querystring_alpha(url) ordered_url = url if url and not url.strip.empty? qs = URI.split(url)[7] if qs ordered_url = url.split('?')[0] + '?' parameters = qs.split('&') escaped_parameters = [] # Escape each parameter to ensure coherence with build_query_string for parameter in parameters name_value = parameter.split('=') if name_value.size == 2 escaped_value = CGI.escape(CGI.unescape(name_value[1])) escaped_parameters << "#{name_value[0]}=#{escaped_value}" else escaped_parameters << "#{name_value[0]}" end end ordered_url << escaped_parameters.sort.join('&') return ordered_url end end return url end |
.partial_exists?(partial_short_path) ⇒ Boolean
245 246 247 248 249 |
# File 'lib/tool_pouch/trail.rb', line 245 def self.partial_exists?(partial_short_path) short_path = partial_short_path.split('/') short_path[short_path.length-1] = '_' + short_path.last return File.exists?(RAILS_ROOT + '/app/views' + short_path.join('/') + '.html.erb') end |
.refresh_file_content(path, content) ⇒ Object
251 252 253 254 255 |
# File 'lib/tool_pouch/trail.rb', line 251 def self.refresh_file_content(path, content) FileUtils.mkdir_p directories(path) FileUtils.rm(path) if File.exists?(path) File.open(path, 'w') {|f| f.write(content) } end |
.slice_trailing_slash(path) ⇒ Object
257 258 259 |
# File 'lib/tool_pouch/trail.rb', line 257 def self.slice_trailing_slash(path) return (trailing_slash?(path)) ? path.chop : path end |
.strip_scheme(uri) ⇒ Object
261 262 263 |
# File 'lib/tool_pouch/trail.rb', line 261 def self.strip_scheme(uri) uri.sub(/^#{Addressable::URI.parse(URI.encode(uri)).scheme}:\/\//,"") end |
.trailing_slash?(path) ⇒ Boolean
265 266 267 268 269 270 271 272 273 274 |
# File 'lib/tool_pouch/trail.rb', line 265 def self.trailing_slash?(path) trailing_slash = false if path and path.length > 0 if path.slice(-1,1) == '/' trailing_slash = true end end return trailing_slash end |
.yml_content(file) ⇒ Object
276 277 278 |
# File 'lib/tool_pouch/trail.rb', line 276 def self.yml_content(file) return YAML.load_file(file) end |