I am trying to use Regex to get the company name out of the email address. I am splitting the email two times. Is there a better solution for this?
c = "user#company_name.com"
(c.split("#").last).split(".").first
Another solution given.
str = "user#company_name.com"[/[^#]+(?=\.)/]
See working demo
Answer to the question in the post (before it was edited)
Judging from your code, it seems like you want to extract the top level domain (although it contradicts with the title, which does not make sense). Assuming so, this will give you the top level domain.
"user#company_name.com"[/[^.]+\z/]
# => "com"
Solution to a different problem that the OP additionally mentions in the comment to this answer
"user#company_name.com"[/(?<=#)[^.]+/]
# => "company_name"
This will give you company name.
(.*)#(\w+)\.(.*)
Related
I have User model embeds_one :profile and Profile model has name. I need to run LIKE query on profile name. I tried below as suggested here
User.where("profile.name" => "/.*Senthil.*/")
But above solution not working. I tried lot of stock overflow answers , but no luck. Any help will be highly appreciated.
Screenshot : I am very sure , there is matching record.
This finds all people with the name senthil (first OR last).
User.where("profile.name" => /.*senthil.*/i )
Here i used to make the query case insensitive.
I think you mean to remove the quotes, otherwise the engine will try to match the string exactly, instead of as regex
edit: The correct regexp would be
User.where("profile.name" => /.*Senthil.*/)
I have a method in my rails app that saves the image from an og:image tag url.
def photo_from_url(url)
if !Nokogiri::HTML(open(url)).css("meta[property='og:image']").blank?
photo_url = Nokogiri::HTML(open(url)).css("meta[property='og:image']").first.attributes["content"]
self.photo = URI.parse(photo_url)
self.save
end
end
This works in most cases, except when the image url happens to include a number that start with 0, such as http://ad009cdnb.website.com/rest-of-url
In these cases, I get an illegal octal digit error.
How can I prevent the method from thinking that any numbers starting with zero are base-8?
This is not a answer. Sorry for use the system wrongly. Can't find out how to put a string with HTTP without change it in a link as comment.
URI.parse works fine:
URI.parse("http://ad009cdnb.website.com/rest-of-url").to_s
=> "http://ad009cdnb.website.com/rest-of-url"
as everyone said, we really need a stacktrace or something to help you. And most likely you will want to save the URL as a string, not as an object on the database.
Hi guys I posted this question, but I can no longer reproduce the question.
As this is no longer an issue, I think the best course of action is to close this question. However, I see that some people have been voted up for their comments (They make good points like saving as a string instead of an object). Will they be discredited if the questions is closed?
I'm grateful for everyone's input, and don't want to upset anyone. What is SO protocol for a situation like this?
So, you're in Github filing an issue and you refer to issue #31. Then, while writing this issue, you note that #johnmetta has suggested some possible solutions that he's working on. Then you hit "Submit New Issue" and when you do, "#31" and "#johnmetta" are links, and #johnmetta has been notified, and issue #31 has a notification that it has been referenced.
I realize that there are more than one technologies at work here (Javascript goodies, etc), but what I'm looking for are some examples of how to do this type of thing in the Rails world. It's an interestingly difficult subject to search for.
What I've come up with conceptually is:
Have some identifier, such as # or # that is reserved
Upon submission, search for that identifier in the appropriate attribute
Upon finding it, search for the appropriate model with a field matching what follows
Once finding that, replace that text string with a link
Optionally, do whatever necessary to notify the referenced object
That said, it seems like it's super simple (explicitly coded, assumes friendly_id).
def prettify_user_links(str, source):
result = str
str.scan(/(#\S+)+/).each do |mtch|
# Strip off whatever identifier we're using
search_string = mtch[0].gsub('#','')
# Search for the matching model in the appropriate table
user = User.find(search_string)
if user
# If we find a matching model, create some link text and link it
link_txt = "<a href=>'#{user.url}'>#{mtch}</a>"
result.gsub!(search_string, link_txt)
# Notification. Not sure how/where, maybe with a message bus, or something more brute-force like
Comment.create :user_id => user.id, :body => "You have been mentioned in #{link_to comment.excerpt, comment} by #{link_to comment.owner, owner}"
return result
That would be my first cut, but I feel there have to be much more elegant solutions.
An additional aspect to this question: How would you grab a snippit of surrounding text. The brute force way would be to search n words before and m words after that string and grab all of that, then grab that sub-string from the results and do the search. Still, seems like there'd be a more elegant solution.
What you've described is the basic way; anything else is not terribly more elegant. It's helpful to see it as two parts: one is on receipt of the comment (when you should do notifications) and the other is on display of the comment, when you should do linkification.
This allows you to keep the original comment in its original form, which is helpful.
Perhaps put an after_create (so notifications aren't sent on every edit) on the comment model (assuming a comment model that includes a 'body' field):
[edit: added contextual info]
after_create :notify_mentions
def notify_mentions
body.scan %r{(.{0,40})#(\w+)(.{0,20})} do |match|
username = match[1]
context = [match.first, match.last]
Notification.send(match, context, self) if User.exists?(:login => username)
end
end
I use \w+ in place of \S+ because people often say things like:
Hey #JohnMetta, how are you doing?
and \S+ will capture the , which might be wrong. Pulling the # out of the capture group lets me ignore it during notification.
The context in the above match groups consists of the 40 characters before and 20 characters after the matched username for your snippet. Adjust to taste.
Then when displaying the message, you essentially create a helper something like what you had:
def linkify(body)
body.gsub %r{#\w+} do |match|
link_to match, :controller => :users, :action => :show, :id => match
end
end
#gsub is awesome like that, in that it takes a block and replaces with the contents.
It's not a lot more elegant than what you had, but it should give a pretty decent result.
I want to give my pages human-readable slugs, but Rails' built-in parameterize method isn't SEO-optimized. For example, if I have a post called "Notorious B.I.G. is the best", parameterize will give me this path:
/posts/notorious-b-i-g-is-the-best
which is suboptimal since Google construes the query "Notorious B.I.G." as "Notorious BIG" instead of "Notorious B I G" (i.e., the dots are removed rather than treated as spaces)
Likewise, "Tom's fave pizza" is converted to "tom-s-fave-pizza", when it should be "toms-fave-pizza" (since Google ignores apostrophe's as well)
To create a better parameterize, I need to know which characters Google removes from queries (so I can remove them from my URLs) and which characters Google treats as spaces (so I can convert them to dashes in my URLs).
Better still, does such a parameterize method exist?
(Besides stringex, which I think tries to be too clever. 2 representative problem cases:
[Dev]> "Notorious B.I.G. is the best".to_url
=> "notorious-b-dot-i-g-is-the-best"
[Dev]> "No, Curren$y is the best".to_url
=> "no-curren$y-is-the-best"
I would try using a gem that has been designed for generating slugs. They often make good design decisions and they have a way of updating the code for changing best practices. This document represents Google's best practices on URL design.
Here is a list of the best gems for solving this problem. They are sorted by rank which is computed based on development activity and how many people "watch" changes to the gems source code.
The top one right now is frendly_id and it looks like it will generate good slugs for your use in SEO. Here is a link to the features of the gem. You can also configure it and it looks like it is perfect for your needs.
Google appears to have good results for both the "b-i-g" and "big" in the url slugs.
For the rails side of things, yes a parameterize method exists.
"Notorious B.I.G. is the best".parameterize
=> "notorious-b-i-g-is-the-best"
I think you can create the URLs yourself... something like
class Album
before_create :set_permalink
def set_permalink
self.permalink = name.parameterize
end
def to_params
"#{id}-#{permalink}"
end
end
This will create a url structure of:
/albums/3453-notorious-b-i-g-is-the-best
You can remove the id section in to_params if you want to.
Use the title tag and description meta tag to tell google what the page is called: these carry more weight than the url. So, leave your url as /posts/notorious-b-i-g-is-the-best but put "Notorious B.I.G. is the best" in your title tag.
How can i realize seo friendly urls?
Instead
http://mysite.com/articles/show/2
i would like to use the articlename instead the id
i.e.
mysite.com/articles/show/articlename
or somehow combine id and articlename like this
mysite.com/articles/show/articlename-2
i'm a rails newbie so perhaps you could give me short advice where to change
something with what code?
Look in your article controller, probably in app/controllers/articles.rb. You probably have a method named show which looks up an article by id with something like this:
#article = Article.find(params[:id])
If you know the id is going to be the title of the post instead of its id, you can instead look up your article using
#article = Article.find_by_title(params[:id])
This will allow you to use somewhat ugly URLs like /articles/show/This+is+the+title. If you want to make a slightly nicer URL, you could add a column to your article table (called, say, seo_title) to store the title translated to lowercase with underscores, yielding something like this_is_the_title.
#and
Your question is at once both simple and yet difficult. Best you check out this more mature Stack Overflow post to find your answer:
https://stackoverflow.com/questions/723765/how-do-i-make-the-urls-in-ruby-on-rails-seo-friendly-knowing-a-vendor-name/
It has more examples and more options. While find_by_title is an option, it is far from your best option.