Last active
August 29, 2015 14:02
-
-
Save evolve2k/63f446447510a275e1f1 to your computer and use it in GitHub Desktop.
Import without errors - when you ask for nested things.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Actually Id also move all the rubbish out of the middle and make variables so it hurts your brain less. | |
# call the variables what you like, but see how it's like, sigh of releif. | |
# Make your brain pass the least amount of crap as possible. | |
# Encapsulation lets you hide ugly things inside nice little packages :) | |
require 'rubygems' | |
require 'nokogiri' | |
require 'open-uri' | |
domain_url = "http://prod3.agileticketing.net/websales/pages/" | |
listings = ".Item , .ViewLink" | |
title_field = ".Name" | |
date_field = "Date" | |
time_field = ".Time , span" | |
url_field = ".ViewLink" | |
url = "http://prod3.agileticketing.net/websales/pages/list.aspx?epguid=5e3ea987-8f29-408f-ad74-5c32388b1f83&" | |
doc = Nokogiri::HTML(open(url)) | |
doc.css(listings).each do |event| | |
if event | |
if event.at_css(title_field) | |
title = (event.at_css(title_field).text || 'title unknown') | |
end | |
if event.at_css(time_field) | |
start_time = (event.at_css(time_field).text || nil) | |
end | |
if event.at_css(url_field) | |
url = domain_url + event.at_css(url_field)[:href] if event.at_css(url_field) | |
end | |
end | |
puts "#{title} - #{start_time} - #{url}" | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rubygems' | |
require 'nokogiri' | |
require 'open-uri' | |
url = "http://prod3.agileticketing.net/websales/pages/list.aspx?epguid=5e3ea987-8f29-408f-ad74-5c32388b1f83&" | |
doc = Nokogiri::HTML(open(url)) | |
doc.css(".Item , .ViewLink").each do |event| | |
if event | |
if event.at_css(".Name") | |
title = (event.at_css(".Name").text || 'title unknown') | |
end | |
if event.at_css(".Time , span") | |
start_time = (event.at_css(".Time , span").text || nil) | |
end | |
if event.at_css(".ViewLink") | |
url = "http://prod3.agileticketing.net/websales/pages/" + event.at_css(".ViewLink")[:href] if event.at_css(".ViewLink") | |
end | |
end | |
puts "#{title} - #{start_time} - #{url}" | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Event.new creating errors | |
The issue has to do with the ruby file loading up your fulls rails environment. | |
Now is the time to introduce rake (very briefly). | |
I suggest you read this short into to rake here: http://jasonseifer.com/2010/04/06/rake-tutorial | |
Watch this vid by the creator if you are interested: https://www.youtube.com/watch?v=AFPWDzHWjEY | |
Do this. | |
1. rename your file from .rb to .rake | |
2. Add this around your code | |
# keep require statements up the top of the file. | |
task :import_docfest => :environment do | |
# All your current code goes in the middle, except the require statements which go above. | |
end | |
From the command line you now type. | |
$ rake import_docfest | |
The rake tool finds all the rake files and then finds a task matching your task name and runs it. | |
you can run this rake task from the root/base directory of your project, no need to mention the path anymore. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# When you want to query nested things. | |
# eg animal.fur.color # you can only ask for color if animal and animal.fur exist. | |
# So you need to use this sort of structure. | |
if animal | |
if animal.fur | |
# code here | |
puts animal.fur.color | |
end | |
end | |
# This can be tidied up to be: | |
if animal && animal.fur | |
puts animal.fur.color | |
end | |
# or even nicer using rubys if at the end trick: | |
puts animal.fur.color if animal && animal.fur | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment