Simple URL cleaning - ruby

I'm trying to do some basic url cleaning, so that
www.google.com
www.google.com/
http://google.com
http://google.com/
https://google.com
https://google.com/
are replaced by http://www.google.com (or https://www.google.com in case when https:// is at the beginning).
Basically I'd like to check if there is http/https at the beginning and / at the end in one regexp.
I was trying something like this:
"https://google.com".match(/^(http:\/\/|https:\/\/)(.*)(\/)*$/) in this case I get:
=> #<MatchData "https://google.com" 1:"https://" 2:"google.com" 3:nil>
which is good.
Unfortunately for:
"https://google.com/".match(/^(http:\/\/|https:\/\/)(.*)(\/)*$/) I get:
=> #<MatchData "https://google.com/" 1:"https://" 2:"google.com/" 3:nil> and would like to have 2:"google.com" 3:"/"
Any idea how to do this?

It's obvious if you spot the mistake ;)
You were trying:
^(http:\/\/|https:\/\/)(.*)(\/)*$
The answer is to use:
^(http:\/\/|https:\/\/)(.*?)(\/)*$
This makes the operator "non-greedy", so the tailing forward slash doesn't get swallowed up by the "." operator.
EDIT:
In fact, you should really be using:
^(http:\/\/|https:\/\/)?(www\.)?(.*?)(\/)*$
That way, you will also match your first two examples, which don't have a "http(s)://" in them. You are also splitting out the value/existence of the "www" part. In action: http://www.rubular.com/r/VUoIUqCzzX
EDIT2:
I was bored and wanted to perfect this :P
Here you go:
^(https?:\/\/)?(?:www\.)?(.*?)\/?$
Now, all you need to do is replace your website with the first match (or "http://", if nil), then "www.", then the second match.
In action: http://www.rubular.com/r/YLeO5cXcck
(18 months later) EDIT:
Check out my awesome ruby gem that will help solve your problems!
https://github.com/tom-lord/regexp-examples
/(https?:\/\/)?(?:www\.)?google\.com\/?/.examples # =>
["google.com",
"google.com/",
"www.google.com",
"www.google.com/",
"http://google.com",
"http://google.com/",
"http://www.google.com",
"http://www.google.com/",
"https://google.com",
"https://google.com/",
"https://www.google.com",
"https://www.google.com/"]
/(https?:\/\/)?(?:www\.)?google\.com\/?/.examples.map(&:subgroups) # =>
[[],
[],
[],
[],
["http://"],
["http://"],
["http://"],
["http://"],
["https://"],
["https://"],
["https://"],
["https://"]]

Related

expect(Class).to receive(:x).with(hash_including(y: :z)) doesn't work

I want to check that Pandoc.convert is called with to: :docx option like this:
options = {to: :docx}
PandocRuby.convert("some string", options)
I have the following expectation in a spec:
expect(PandocRuby).to receive(:convert).with(hash_including(to: :docx))
The spec fails like this:
Failure/Error: expect(PandocRuby).to receive(:convert).with(hash_including(to: :docx))
(PandocRuby (class)).convert(hash_including(:to=>:docx))
expected: 1 time with arguments: (hash_including(:to=>:docx))
received: 0 times
But when debugging, options is like this:
[2] pry(#<ReportDocumentsController>)> options
=> {
:to => :docx,
:reference_docx => "/Users/josh/Documents/Work/Access4All/Projects/a4aa2/src/public/uploads/report_template/reference_docx/1/reference.docx"
}
I think I'm using the wrong RSpec matcher (or the right one in the wrong way), but I can't get it working.
You just need to expect all of the method arguments:
expect(PandocRuby).to receive(:convert).with("some string", hash_including(to: :docx))
Or you could use a matcher to be less specific about the first argument, e.g.
expect(PandocRuby).to receive(:convert).with(an_instance_of(String), hash_including(to: :docx))

Open URI - Invalid URI Error, encoding/escaping not affecting

I'm building out a YahooFinance Api and keep hitting a brick wall when trying to use open URI.
Code:
uri = ("http://ichart.finance.yahoo.com/table.csv?s=#{URI.escape(code)}&a=#{start_month}&b=#{start_day}&c=#{start_year}&d=#{end_month}&e=#{end_day}&f=#{end_year}&g=d&ignore=.csv")
puts "#{uri}"
conn = open(uri)
Error:
`split': bad URI(is not URI?): http://ichart.finance.yahoo.com/table.csv?s=%255EIXIC&a=00&b=1&c=1994&d=09&e=14&f=2014&g=d&ignore=.csv} (URI::InvalidURIError)
I have tried URI.unescape(code) which outputs code as ^IXIC, as well as leaving any URI methods out and code will come through as %5EIXIC.
After reading around on stack overflow, I've tried both of these methods to no avail:
uri = URI.parse(URI.encode(url.strip))
safeurl = URI.encode(url.strip)
Even after looking through the code for another ruby yahoo-finance gem, here, I can't seem to find a solution. Any help is greatly appreciated. Thanks
EDIT: I am able to use open(uri) when I manually enter in the url in single quotes. Do double quotes, (used for inserting ruby objects), play a role here?
Don't try to inject variables into URLs. If they contain characters that need to be encoded per the spec, they won't be by interpolation. Instead, take advantage of the right tools for the job, like Ruby's URI class or the Addressable::URI gem.
See "How to post a URL containting curly braces and colons" for how to do this using well tested wheels.
In your situation, something like this will work:
require 'uri'
code = 'qwer3456*&^%'
start_month = 1
start_day = 1
start_year = 2014
end_month = 12
end_day = 31
end_year = 2015
uri = URI.parse("http://ichart.finance.yahoo.com/table.csv")
uri.query = URI.encode_www_form(
{
'g' => 'd',
'ignore' => '.csv',
's' => code,
'a' => start_month,
'b' => start_day,
'c' => start_year,
'd' => end_month,
'e' => end_day,
'f' => end_year
}
)
uri.to_s # => "http://ichart.finance.yahoo.com/table.csv?g=d&ignore=.csv&s=qwer3456*%26%5E%25&a=1&b=1&c=2014&d=12&e=31&f=2015"
The code works for me though I don't think the API endpoint is correct:
[1] pry(main)> uri = URI("http://ichart.finance.yahoo.com/table.csv?s=%255EIXIC&a=00&b=1&c=1994&d=09&e=14&f=2014&g=d&ignore=.csv")
=> #<URI::HTTP:0x007fd63a2fff40 URL:http://ichart.finance.yahoo.com/table.csv?s=%255EIXIC&a=00&b=1&c=1994&d=09&e=14&f=2014&g=d&ignore=.csv>
[3] pry(main)> Net::HTTP.get(uri)
=> "<!doctype html public \"-//W3C//DTD HTML 4.01//EN\" \"http://www.w3.org/TR/html4/strict.dtd\">\n<html><head><title>Yahoo! - 404 Not Found</title><style>\n/* nn4 hide */ \n/*/*/\nbody {font:small/1.2em arial,helvetica,clean,sans-serif;font:x-small;text-align:center;}table {font-size:inherit;font:x-small;}\nhtml>body {font:83%/1.2em arial,helvetica,clean,sans-serif;}input {font-size:100%;vertical-align:middle;}p, form {margin:0;padding:0;}\np {padding-bottom:6px;margin-bottom:10px;}#doc {width:48.5em;margin:0 auto;border:1px solid #fff;text-align:center;}#ygma {text-align:right;margin-bottom:53px}\n#ygma img {float:left;}#ygma div {border-bottom:1px solid #ccc;padding-bottom:8px;margin-left:152px;}#bd {clear:both;text-align:left;width:75%;margin:0 auto 20px;}\nh1 {font-size:135%;text-align:center;margin:0 0 15px;}legend {display:none;}fieldset {border:0 solid #fff;padding:.8em 0 .8em 4.5em;}\nform {position:relative;background:#eee;margin-bottom:15px;border:1px solid #ccc;border-width:1px 0;}\n#s1p {width:15em;margin-right:.1em;}\nform span {position:absolute;left:70%;top:.8em;}form a {font:78%/1.2em arial;display:block;padding-left:.8em;white-space:nowrap;background: url(http://l.yimg.com/a/i/s/bullet.gif) no-repeat left center;} \nform .sep {display:none;}.more {text-align:center;}#ft {padding-top:10px;border-top:1px solid #999;}#ft p {text-align:center;font:78% arial;}\n/* end nn4 hide */\n</style></head>\n<body><div id=\"doc\">\n<div id=\"ygma\"><img\nsrc=http://l.yimg.com/a/i/yahoo.gif\nwidth=147 height=31 border=0 alt=\"Yahoo!\"><div><a\nhref=\"http://us.rd.yahoo.com/404/*http://www.yahoo.com\">Yahoo!</a>\n - Help</div></div>\n<div id=\"bd\"><h1>Sorry, the page you requested was not found.</h1>\n<p>Please check the URL for proper spelling and capitalization. If\nyou're having trouble locating a destination on Yahoo!, try visiting the\n<strong><a\nhref=\"http://us.rd.yahoo.com/404/*http://www.yahoo.com\">Yahoo! home\npage</a></strong> or look through a list of <strong><a\nhref=\"http://us.rd.yahoo.com/404/*http://docs.yahoo.com/docs/family/more/\">Yahoo!'s\nonline services</a></strong>. Also, you may find what you're looking for\nif you try searching below.</p>\n<form name=\"s1\" action=\"http://us.rd.yahoo.com/404/*-http://search.yahoo.com/search\"><fieldset>\n<legend><label for=\"s1p\">Search the Web</label></legend>\n<input type=\"text\" size=30 name=\"p\" id=\"s1p\" title=\"enter search terms here\">\n<input type=\"submit\" value=\"Search\">\n<span>advanced search <span class=sep>|</span> most popular</span>\n</fieldset></form>\n<p class=\"more\">Please try <strong><a\nhref=\"http://us.rd.yahoo.com/404/*http://help.yahoo.com\">Yahoo!\nHelp Central</a></strong> if you need more assistance.</p>\n</div><div id=\"ft\"><p>Copyright © 2014 Yahoo! Inc.\nAll rights reserved. <a\nhref=\"http://us.rd.yahoo.com/404/*http://privacy.yahoo.com\">Privacy\nPolicy</a> - <a\nhref=\"http://us.rd.yahoo.com/404/*http://docs.yahoo.com/info/terms/\">Terms\nof Service</a></p></div>\n</div></body></html>\n"
Looks like your problem is the ignore=.csv part.
I mean this is probably trying to encode it as a domain extension. Probably you should remove the dot to solve the problem.

RubyMine RoR I18n config/locals/en.yml syntax error "file should have single root"

I don't know if this is just a goof syntax error, or if this is something I should legitimately be concerned about. I've searched online for this error and have found very little concerning it. So my guess is that this is either something so "no duh" that hardly anybody ends up with this error, or that it's so obscure that - again - hardly anybody ends up with it. Nonetheless, here we go:
This is what I'm using:
Ruby 2.1.1p76
Rails 4.0.5
SafeYAML 1.0.3
RubyMine 6.3
[should you need anymore information, please ask]
The error I'm getting exactly is: "Rails i18n locale file should have single root"
Here is a shortened version of my en.yml file (I'll include what I believe to be the problem areas)
<%
object_names = **{**
:administrator => 'Administrator',
:activity_log => 'Site Activity',
:answer => 'Answer',
:approval => 'Approval',
:user => 'Member',
:video => 'Video',
:vote => 'Like'
}
section_names = **{**
:approvals => 'Items Awaiting Review',
:advertisements => 'Advertisements',
:ad_placements => 'Ad Placements',
:awarded_badges => 'Badges',
:badges => 'Badges',
:videos => 'Videos'
}
anonymous = 'Anonymous**'
%>
en:
homepage:
mine: "My %{site_name}"
site_name: "%{site_name}"
site_condition_name: "%{site_condition_name}"
titles:
main: "%{site_name}"
delimiters:
minor:** ' **-** '
major: ' | '
scopes:
popular: Popular
newest: Newest
active: Active
my_feed: My Friends
my_activity: What I've Done
tracked_items: "Only <%= object_names[:tracked_item].pluralize %>"
everyone: Everyone
user: "By %{name}"
view: "By %{view}"
sent: "Sent <%= object_names[:message].pluralize %>"
page: "Page %{page}"
letter: "Starting With %{letter}"
query: "%{query}"
category: "%{category}"
**meta_description:**
main: "%{site_name} is a social network that connects people"
footer:
about: "About %{site_name}"
about_alliance_health: About Alliance Health
community_advocates: Community Advocates
terms_of_use: Terms of Use
oh - wow. Ok, it looks like bold isn't going to work inside the code markers. But I'm sure you guys can decipher what's going on. Basically anything that's bold (or delimited with a double asterisk '**') is where the IDE is marking the code with this particular error. So, as you can see, it seems rather chaotic and nonsensical. But I often find that when such things happen, it's usually one tiny little character somewhere that's throwing everything else off.
Now, I'm no YAML expert - in fact, I hardly even know the stuff (which is something I'm planning on changing here in the near future) so this may be something along the "no duh" lines. However, it is interesting to note that the bulk of the error-marked syntax starts with the last single-quote of the word: " 'anonymous' ", all the way down to: " minor: ", skipping the first following single quote, and picking up again on the hyphen, afterwhich there are no more errors for the rest of the nearly 5,500 lines of this file.
Thanks to anybody who helps out. I've been dorking around with this for far too long, and with very little online information on this particular issue. So any help is much appreciated :)
Thanks!
So - I'm not sure this will ever help anybody ... but in the case that it may, this is how I re-engineered the script to avoid the errors. I simply removed the usual delimiters that were causing the parsing confusion. And #mu is too short, you were correct. This is an erb.yml file, though I'm not sure why the original creator left the dual extension off. At any rate - thank you to everybody who offered any suggestions and ideas :)
<%
object_names = Hash.new
object_names[:administrator] = %q<Administrator>
object_names[:activity_log] = %q<Site Activity>
object_names[:answer] = %q<Answer>
object_names[:approval] = %q<Approval>
object_names[:user] = %q<Member>
object_names[:video] = %q<Video>
object_names[:vote] = %q<Like>
section_names = Hash.new
section_names[:approvals] = %q<Items Awaiting Review>
section_names[:advertisements] = %q<Advertisements>
section_names[:ad_placements] = %q<Ad Placements>
section_names[:awarded_badges] = %q<Badges>
section_names[:badges] = %q<Badges>
section_names[:videos] = %q<Videos>
%>
en:
homepage:
mine: "My %{site_name}"
site_name: "%{site_name}"
site_condition_name: "%{site_condition_name}"
titles:
main: "%{site_name}"
delimiters:
minor: ' - '
major: ' | '
scopes:
popular: Popular
newest: Newest
active: Active
my_feed: My Friends
my_activity: What I've Done
tracked_items: "Only <%= object_names[:tracked_item].pluralize %>"
everyone: Everyone
user: "By %{name}"
view: "By %{view}"
sent: "Sent <%= object_names[:message].pluralize %>"
page: "Page %{page}"
letter: "Starting With %{letter}"
query: "%{query}"
category: "%{category}"
meta_description:
main: "%{site_name} is a social network that connects people"
footer:
about: "About %{site_name}"
about_alliance_health: About Alliance Health
community_advocates: Community Advocates
terms_of_use: Terms of Use

rails routes using the do part for iteration

I have a very basic question, am looking at some rails code similar to following but not able to interpret it, what are the REST urls and corresponding actions inferred from this? Can someone please help understand as I did not find any examples of similar routes.
map.resources :myresources do |item|
item.resources :v, :controller => 'my_controller' do |v|
v.resource :abc
end
end
Thanks in advance!!
This is actually the old routing style.
Now you can just write the code this way:
resources :myresources do
resources :v, :controller => "my_controller" do
resource :abc
end
end
With this code you will get these routes:
myresource_v_abc POST /myresources/:myresource_id/v/:v_id/abc(.:format) abcs#create
new_myresource_v_abc GET /myresources/:myresource_id/v/:v_id/abc/new(.:format) abcs#new
edit_myresource_v_abc GET /myresources/:myresource_id/v/:v_id/abc/edit(.:format) abcs#edit
GET /myresources/:myresource_id/v/:v_id/abc(.:format) abcs#show
PUT /myresources/:myresource_id/v/:v_id/abc(.:format) abcs#update
DELETE /myresources/:myresource_id/v/:v_id/abc(.:format) abcs#destroy
myresource_v_index GET /myresources/:myresource_id/v(.:format) my_controller#index
POST /myresources/:myresource_id/v(.:format) my_controller#create
new_myresource_v GET /myresources/:myresource_id/v/new(.:format) my_controller#new
edit_myresource_v GET /myresources/:myresource_id/v/:id/edit(.:format) my_controller#edit
myresource_v GET /myresources/:myresource_id/v/:id(.:format) my_controller#show
PUT /myresources/:myresource_id/v/:id(.:format) my_controller#update
DELETE /myresources/:myresource_id/v/:id(.:format) my_controller#destroy
myresources GET /myresources(.:format) myresources#index
POST /myresources(.:format) myresources#create
new_myresource GET /myresources/new(.:format) myresources#new
edit_myresource GET /myresources/:id/edit(.:format) myresources#edit
myresource GET /myresources/:id(.:format) myresources#show
PUT /myresources/:id(.:format) myresources#update
DELETE /myresources/:id(.:format) myresources#destroy

Escape forward slash in Ruby url helper

Setuping a staticMatic project using /index.html:
#slug = current_page.gsub(/\.html/, '')
returns "/index(.html)", but should be /index
Changing term corrects: - #slug = current_page.gsub("/", "").gsub(".html", "") as found in:
https://github.com/adamstac/staticmatic-bootstrap/blob/master/src/helpers/application_helper.rb
To delete the beginning "/" after you've stripped the html simply execute this (which will do both in one command):
current_page.gsub(/\.html/, '').gsub(/\//,''))

Resources