Rails: How to define a function in a rake task - ruby-on-rails

In my task file post.rake, I want to reuse a function
def save_post(title, href, source)
post = Post.new(title: title, url: href, source: source)
if post.save
puts title + 'saved'
else
puts title + 'not saved'
end
end
However, when I define it in this file and re-use it, it returns
NoMethodError: undefined method `save_post' for main:Object
The post.rake looks like this:
task :fetch_post => :environment do
require 'nokogiri'
require 'open-uri'
url = 'http://example.com'
doc = Nokogiri::HTML(open(url) )
puts doc.css("title").text
doc.css(".a").each do |item_info|
title = item_info.text
href = item_info['href']
save_post(title, href)
end
def save_post(title, href)
post = Post.new(title: title, url: href)
if post.save
puts title + 'saved'
else
puts title + 'not saved'
end
end
end
The content-scraping part works. I just move the post-saving code out, wanting to abstract the method out.
Where should I put the def method?

If you define methods in a rake task, they become accessible globally, which may have undesired side-effects. A cleaner approach is to use an inline lambda (or move the method to some class in the app)
task :fetch_post => :environment do
require 'nokogiri'
require 'open-uri'
save_post = ->(title, href) {
post = Post.new(title: title, url: href)
if post.save
puts title + 'saved'
else
puts title + 'not saved'
end
}
url = 'http://example.com'
doc = Nokogiri::HTML(open(url) )
puts doc.css("title").text
doc.css(".a").each do |item_info|
title = item_info.text
href = item_info['href']
save_post.call(title, href)
end
end

OOOH~~~,function postion is wrong, like this, it works:
task :fetch_post => :environment do
require 'nokogiri'
require 'open-uri'
def save_post(title, href)
post = Post.new(title: title, url: href)
if post.save
puts title + 'saved'
else
puts title + 'not saved'
end
end
url = 'http://example.com'
doc = Nokogiri::HTML(open(url) )
puts doc.css("title").text
doc.css(".a").each do |item_info|
title = item_info.text
href = item_info['href']
save_post(title, href)
end
end

You should define method before and outside of task:
task :fetch_post => :environment do
require 'nokogiri'
require 'open-uri'
url = 'http://example.com'
doc = Nokogiri::HTML(open(url) )
puts doc.css("title").text
doc.css(".a").each do |item_info|
title = item_info.text
href = item_info['href']
save_post(title, href)
end
end
def save_post(title, href)
post = Post.new(title: title, url: href)
if post.save
puts title + 'saved'
else
puts title + 'not saved'
end
end
But i think this logic should be in model.
#app/models/post.rb
class Post < ActiveRecord::Base
def self.save_post(title, href)
post = Post.new(title: title, url: href)
if post.save
puts title + 'saved'
else
puts title + 'not saved'
end
end
end

Related

web scraping with rails, getting a big object instead of one for each product

so im trying to do web scraping with rails and kimurai, the problem i ran in to was that for some reason i get a single big object instead of one for each of the products im scraping, here is my code:
class ProductsSpider < Kimurai::Base
#name = "products_spider"
#engine = :mechanize
def self.process(url)
#start_urls = [url]
self.crawl!
end
def parse(response, url:, data: {})
response.xpath("//div[#class='andes-card andes-card--flat andes-card--default ui-search-result ui-search-result--core andes-card--padding-default andes-card--animated']").each do |product|
item = {}
item[:product_name] = product.xpath("//h2[#class='ui-search-item__title ui-search-item__group__element']")&.text&.squish
item[:price] = product.xpath("//span[#class='price-tag-fraction']")&.text&.squish&.delete('^0-9')to_i
item[:shipping] = product.xpath("//p[#class='ui-search-item__shipping ui-search-item__shipping--free']")&.text&.squish
Product.where(item).first_or_create
end
end
end
and here is the function on the controller:
def scrape
url = "https://computacion.mercadolibre.com.ar/componentes-pc-placas-video/msi/cordoba/placa-de-video_NoIndex_True#applied_filter_id%3Dstate%26applied_filter_name%3DUbicaci%C3%B3n%26applied_filter_order%3D13%26applied_value_id%3DTUxBUENPUmFkZGIw%26applied_value_name%3DC%C3%B3rdoba%26applied_value_order%3D11%26applied_value_results%3D120%26is_custom%3Dfalse%26view_more_flag%3Dtrue"
response = ProductsSpider.process(url)
if response[:status] == :completed && response[:error].nil?
flash.now[:notice] = "Successfully scraped url"
else
flash.now[:alert] = response[:error]
end
rescue StandardError => e
flash.now[:alert] = "Error: #{e}"
end

How to automatically download app builds from TestFlight

I have a bunch of apps uploaded to the Testflight. How to automatically download all the builds to my computer?
You can use Ruby + Mechanize (https://github.com/sparklemotion/mechanize):
require 'mechanize'
#agent = Mechanize.new
def process_login_page page
puts 'Login...'
login_form = page.forms.first # form has no action or name so just take the first one
login_field = login_form.field_with(:name => 'username')
password_field = login_form.field_with(:name => 'password')
login_field.value = 'username'
password_field.value = 'password'
login_form.submit
app_link_pattern = /\/dashboard\/applications\/(.*?)\/token\//
puts 'Dashboard...'
#agent.get("https://testflightapp.com/dashboard/applications/") do |dashboard_page|
dashboard_page.links.each do |link|
link.href =~ app_link_pattern
if $1 != nil
puts "Builds page for #{$1}..."
#agent.get "https://testflightapp.com/dashboard/applications/#{$1}/builds/" do |builds_page|
process_builds_page builds_page
end
end
end
end
end
def process_builds_page page
body = page.body
build_pages = body.scan /<tr class="goversion pointer" id="\/dashboard\/builds\/report\/(.*?)\/">/
build_pages.each do |build_id|
#agent.get "https://testflightapp.com/dashboard/builds/complete/#{build_id.first}/" do |build_page|
process_build_page build_page
end
end
end
def process_build_page page
build_link = page.links_with(:dom_class => 'bitly').first
#agent.get("https://www.testflightapp.com#{build_link.href}") { |install_page| process_install_page install_page}
end
def process_install_page page
# we need to figure out what kind of build is that
ipa_link = page.link_with(:text => "download the IPA.")
if (ipa_link != nil)
download_build ipa_link, "ipa"
else
apk_link = page.link_with(:text => "download the APK.")
if (apk_link != nil)
download_build apk_link, "apk"
end
end
end
def download_build link, file_ext
link.href =~ /\/dashboard\/ipa\/(.*?)\//
filename = "#{$1}.#{file_ext}"
file_url = "https://www.testflightapp.com#{link.href}"
puts "Downloading #{file_url}..."
#agent.get(file_url).save("out/#{filename}")
end
FileUtils.rm_rf "out"
Dir.mkdir "out"
login_page_url = "https://testflightapp.com/login/"
#agent.get(login_page_url) { |page| process_login_page page }
Disclaimer: I'm not a Ruby developer and this code is way far from being well designed or safe. Just a quick and dirty solution.

call a class from a collection_action in ActiveAdmin (RoR)

I'm new in ruby on rails. I'm trying to call a class from a collection_action in ActiveAdmin. Here is the code(app/admin/models):
collection_action :status_race, :method => :post do
#Do some import work..
redirect_to :class => :import_route
end
And this is the code of the class I want to call(app/lib/route):
class ImportRoute
def initialize
#seperator = " "
#time_format = "%d-%m-%y"
end
def run(filename)
puts "Running route import file"
raise "File" + filename + "doesn't not exist" unless File.exist(filename)
ri = RouteImporter.find(:name => self.class.name)
if(ri.nil?)
puts "Error, file doesn't exists"
end
CSV.foreach(filename, {:col_sep => #seperator}) do |row|
if row.lenght >5
ri.country_name = row[0] + " " + row[1]
ri.type = row[2]
ri.company = row [3]
else
ri.country_name = row[0]
ri.type = row[1]
ri.company = row[2]
ri.date = row[4].gsub(";", " ")
end
end
end
end
I was using redirect_to to call the class but is not working, and I don't have any clue about how to do it. Any idea? Thanks!
This code is taken from http://activeadmin.info/docs/8-custom-actions.html#collection_actions
ActiveAdmin.register Post do
collection_action :import_csv, :method => :post do
# Do some CSV importing work here...
redirect_to {:action => :index}, :notice => "CSV imported successfully!"
end
end
This collection action will generate a route at
“/admin/posts/import_csv” pointing to the
Admin::PostsController#import_csv controller action.
So it means you have to add a method import_csv in app/controllers/admin/posts_controller.rb. Inside this method, you can instantiate your model:
def import_csv
import_route = ImportRoute.new
# do stuff on this object
end
You can easily adapt this to your code

Prevent ActionMailer to strip repeating spaces in plain message

I'm trying to have an table in text mail, so I write some helpers:
module MailerHelper
def field_width(text, width)
' ' * (width - text.length) + text
end
def cell(text, width)
output = '| ' + field_width(text, width-2) + " |\n"
output << '+-' + '-'*(width-2) + '-+'
end
end
Then in view I write it like this:
<%= cell 'Test', 10 %>
But that what I get (according to letter_opener) is:
| Test |
+----------+
As can you see, the spaces that are repeating before Test. My question is how to prevent ActionMailer (or anything else what is destroying my beautiful table) from doing that.
Mailer code:
def remind(client, invoices)
#client = client
#company = #client.company
#invoices = invoices.to_a
days_left = #invoices.first.pay_date - Date.today
message = #client.group.messages.find_by_period days_left.to_i
raise 'No messages for this invoices.' if message.nil?
#template = message.template || if days_left < 0
t 'message.before'
elsif days_left > 0
t 'message.after'
else
t 'message.today'
end
#text = liquid_parse #template
#html = markdown_parse #text
mail(:to => #client.email, :subject => t('message.title'))
end
private
def markdown_parse(text)
markdown = Redcarpet::Markdown.new Redcarpet::Render::HTML,
:autolink => true, :space_after_headers => true
markdown.render text
end
def liquid_parse(text)
renderer = Liquid::Template.parse text
renderer.render 'company' => #company, 'invoice' => #invoice, 'client' => #client
end
I've found bug. It was caused by Premailer what I use to inline CSS in HTML part.
class InlineCSSInterceptor
def self.delivering_email(message)
#message.text_part.body = Premailer.new(message.text_part.body.to_s, with_html_string: true).to_plain_text # this is line causing the problem.
message.html_part.body = Premailer.new(message.html_part.body.to_s, with_html_string: true).to_inline_css
end
end
Mailer.register_interceptor InlineCSSInterceptor

Problem sending mail with message and attachements after upgrading to Rails 3

I used to have this code for sending mails:
class MailTimerMailer < ActionMailer::Base
def mail_schedule(from, to, cc, bcc, subject, message, files=[], sent_at = Time.now)
#subject = subject
#recipients = to
#from = from
#cc = cc
#bcc = bcc
#sent_on = sent_at
#body["message"] = message
#headers = {}
# attache files
files.each do |file|
attachment file.mimetype do |a|
a.body = file.binarydata
a.filename = file.filename
end
end
end
end
It no longer works. I do not have a view for my mails, as the complete message comes from outside my method. I have tried to modify my code to Rails 3 like this:
class ScheduleMailer < ActionMailer::Base
def mail_schedule(from, to, cc, bcc, subject, message, files=[], sent_at = Time.now)
#subject = subject
#recipients = to
#from = from
#cc = cc
#bcc = bcc
#sent_on = sent_at
#body["message"] = message
#headers = {}
# attache files
files.each do |file|
attachments[file.filename] = File.read("public/data/" << file.id.to_s() << "." << file.extension)
end
end
end
This code sends a mail with the attachements, but there are no actual message in the mail. It also gives me a deprecation warning "Giving a hash to body is deprecated, please use instance variables instead". I have tried with "body :message => message" but no luck.
How can I get this working again?
Thank you
This is how:
class MyMailer < ActionMailer::Base
def mail_schedule(from, to, cc, bcc, subject, message, files=[], sent_at = Time.now)
# attache files
files.each do |file|
attachments[file.filename] = File.read("public/data/" << file.id.to_s() << "." << file.extension)
end
mail(:from => from, :to => to, :cc => cc, :bcc => bcc, :subject => subject) do |format|
format.text { render :text => message }
end
end
end

Resources