Path Parsing in Rails

Path parsing in rails

There's this method:

>> ActionController::Routing::Routes.recognize_path("/posts/")
=> {:action=>"index", :controller=>"posts"}

If you only have a string with your route (like "posts_path"), then I guess in the context you're using this you should be able to do

ActionController::Routing::Routes.recognize_path(send("posts_path".to_sym))

btw this was educating for me too :)

File paths for parsing in Ruby using Rake

__dir_ method returns directory of the current file you're in, so this should work for you:

File.read(__dir__ + "/../metadata.json")

What is the opposite of url_for in Rails? A function that takes a path and generates the interpreted route?

Rspec has a method 'params_for', which uses Action Controller's Routing Methods to parse paths with methods into routes.

Theirs is a little more robust than this, but it boils down to:

def params_for(path, method)
params = ActionController::Routing::Routes.recognize_path(path, :method => method)
end

Rails: Parse route information from URL

In Rails 3 you can do the following:

Rails.application.routes.recognize_path "/accounts/1"
# {:action=>"show", :controller=>"accounts", :id=>"1"}

Using ActionController::Routing::Routes.recognize_path kept throwing ActionController::RoutingError Exception: No route matches "/accounts/1

parsing string pathname with ruby

Use suffix parameter of File.basename method:

# irb
irb(main):001:0> File.basename('/some/long/path/filename.extension', '.*')
=> "filename"
irb(main):002:0> File.basename('/some/long/path/filename.v1.extension', '.*')
=> "filename.v1"

Ref: http://www.ruby-doc.org/core/classes/File.html#M000026

How to obtain basename in ruby from the given file path in unix or windows format?

Backslashes, while how Windows expresses things, are just a giant nuisance. Within a double-quoted string they have special meaning so you either need to do:

File.basename("C:\\Users\\john\\note.txt")

Or use single quotes that avoid the issue:

File.basename('C:\Users\john\note.txt')

Or use regular slashes which aren't impacted:

File.basename("C:/Users/john/note.txt")

Where Ruby does the mapping for you to the platform-specific path separator.

How to parse a URL and extract the required substring

I'd do it this way:

require 'uri'

uri = URI.parse('http://something.example.com/directory/')
uri.host.split('.').first
=> "something"

URI is built into Ruby. It's not the most full-featured but it's plenty capable of doing this task for most URLs. If you have IRIs then look at Addressable::URI.

How do I use paperclip and pdf-reader to parse PDF before uploading to S3 bucket?

I figured out that the "queued_for_write" object has a path attribute.

file = cv.queued_for_write[:original]

So I can just access it like this:

reader = PDF::Reader.new(file.path)

Ruby/Rails 3.1: Given a URL string, remove path

With URI::join:

require 'uri'
url = "http://foo.example.com:8080/whatsit/foo.bar?x=y"
baseurl = URI.join(url, "/").to_s
#=> "http://foo.example.com:8080/"


Related Topics



Leave a reply



Submit