Ruby on Rails 4 Parsing first 3 RSS items - ruby-on-rails

I'm currently making a website in RoR 4 and I want to include RSS from my blog on the front page. However, currently I get all of the articles from the blog while I need only first 3.
I'm fairly new to rails and I couldn't find any pointers to how I could do such thing.
My current code in the controller is:
def index
require 'rss'
#rss = RSS::Parser.parse(open('FEED_URL').read, false)
end
I imagine I could simply cut the #rss after the third element after it has been parsed, but that seems to me a bit dirty in Ruby. Is there a better way to do it?
Thank you very much!

Looking here, I'd take a gander and try...
#rss = RSS::Parser.parse(open('FEED_URL').read, false).items[0..2]

Feedjira is a fantastic RSS parser. Just do:
Feedjira::Feed.fetch_and_parse('FEED_URL').entries[0,3]

Related

Display places 2 weeks or older with Ruby on Rails + PostgresQL

This topic has been covered before, but I am new and trying to understand. My app is designed to store the locations of an item and since time matters, I wanted to create a link for a view that would ONLY show places that are two weeks or older. I understand I should create a method in the model similar to what's below, but I am just confused on this code. I have been trying to find material to read to understand better. The homepage shows all the places, but I want a link to also show 2 weeks or older.
Along with help, I would love any good reads to help me understand better. Thanks so much SO!
def self.recent_places
Place.select("p.*, COUNT(v.id) AS count").where("post.created_at >= 2.week.ago.utc")
end
This is a link to the GitHub.
https://github.com/Mbartlett413/DumpStack
I would recommend using scope over class method though two are mostly the same:
scope :older_than, ->(time) { where("created_at < ?", time) }
Using:
Post.older_than(2.weeks.ago)
Post.older_than(1.month.ago)
Here you go: http://guides.rubyonrails.org/active_record_querying.html#passing-in-arguments
I actually just solved this for my own app, this is the method that I used inside my model:
scope :recent_places, -> {
where(Place.arel_table[:created_at].lt(2.weeks.ago))
}

Difficult search in ruby on rails

How can I search first in model (i know how to do this). And then search in this array for more concretence? As you see:
#articles = Article.find(:all, :conditions => { :ART_ID => #search.map(&:ARL_ART_ID)})
#a = #articles.find_all{|item| item.ART_ARTICLE_NR == search.upcase }
First i search in model, but thanks to my db) it have many wrong results, so i must to clarify my array. But there how to search like sql:
like % %
Now it search very strong: if i search AC451, it's good, but if AC45 or C451 it's nothing fetches. How to say him so that before and after
search
could be everything?
Like this, maybe?
item.ART_ARTICLE_NR.include?(search.upcase)
You are asking for trouble by not following rails naming conventions an using upper case column names. That said, the rails3 way to do it is probably:
#articles = Article.where(:ART_ID => #search.map(&:ARL_ART_ID)).where('ART_ARTICLE_NR LIKE', "%#{search.upcase}%")
Without knowing what #search is, it's hard to be sure. But you should read up on the active record guide on the rails 3 query format.
It's maybe not straigtforward answer, but have you considered using ransack gem?

How should I write scraper that searched for a constant button targeted at one domain

I would like to scrape an entire domain ex(Tumblr.com) and search each blog for an embedded tag. Can this be done with rails?
This is not a simple question to answer.
I would point you to:
http://stdlib.rubyonrails.org/libdoc/open-uri/rdoc/index.html
sure, if this tag has an css selector (id, class) or a specific XPATH that you can search for, you should use Mechanize a powerful ruby library.
Something like
agent.page.search(".mytag").each do |item|
day = item.at("a").text
item.search("p").each do |e|
image = e.at("a")
agent.get(image).save_as("images/img#{rand(1000)}_#{File.basename image}")
end
end

Rails 3 - Need help understanding how to QUERY with a WHERE IN (XXXXX)

I want to build a rails query like the following. And would like to learn how so I can do it again on my own.
Pseudo Code:
#usersprojects = ?? Do I get record or just objects here? not sure?
SELECT *
FROM audit_log
WHERE project_id IN (#usersprojects)
IN the where IN () do I some how tell Rails to use the record.id?
thank you so much for helping me learn this. I want to Rails!
#kchau's answer was close, but you need to map out the project_id's from those records, like so:
AuditLogs.find(#userprojects.map(&:project_id))
So, if #usersprojects contained the User's Projects that you're trying to find audit logs for, you would do a query like:
logs = AuditLogs.find(#userprojects)
See the Rails guide for reference.

Liquid Templates Not Parsing!

Im trying to use Liquid Template Language in my Rails Application, i've watched Ryan Bates' video over at rails cast, i pretty much follow the instructions but it just doesnt seem to work!
When I try something like
#template = Liquid::Template.parse("Hi {{name}}")
#template.render('name' => 'toby')
I get
hi toby
but when i try something like
category = Category.first
#template = Liquid::Template.parse("Hi {{category.name}}")
#template.render('category' => category)
I don't get the desired result, I get only
hi ""
Can someone please help me out with this?
When the value is not a hash, you need to tell liquid which methods it can read from the passed object.
This documentation page show you how to instruct ActiveRecord.
The quickest way is to use the liquid_methods macro.

Resources