Last active
September 17, 2020 17:49
-
-
Save Tasha25/7568350 to your computer and use it in GitHub Desktop.
`rspec-rails`, `nokogiri`, and `httparty` in a rails project.
This file contains 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
Create a new rails app called `gray_lady_app` outside of your kick_hash folder. | |
__It should not be in a git repo.__ | |
Add `rspec-rails`, `nokogiri`, and `httparty` to your gemfile and create a model called `Scraper` | |
===================================================== | |
$ rails _3.2.14_ new gray_lady_app -d postgresql | |
In Gemfile | |
1. gem "rspec-rails", :group => [:test, :development] | |
2. gems in your gist (https://gist.github.com/Tasha25/7024875) | |
#Gemfile | |
group :development, :test do | |
gem 'pry-rails' # Causes rails console to open pry | |
# https://github.com/rweng/pry-rails | |
gem 'pry-debugger' # Adds step, next, finish, and continue commands and breakpoints | |
# https://github.com/nixme/pry-debugger | |
gem 'pry-stack_explorer' # Navigate the call-stack | |
# https://github.com/pry/pry-stack_explorer | |
gem 'annotate' # Annotate all your models, tests, fixtures, and factories | |
# https://github.com/ctran/annotate_models | |
gem 'quiet_assets' # Turns off the Rails asset pipeline log | |
# https://github.com/evrone/quiet_assets | |
gem 'better_errors' # Replaces the standard Rails error page with a much better and more useful error page | |
# https://github.com/charliesome/better_errors | |
gem 'binding_of_caller' # Advanced features for better_errors advanced features (REPL, local/instance variable inspection, pretty stack frame names) | |
# https://github.com/banister/binding_of_caller | |
gem 'meta_request' # Supporting gem for Rails Panel (Google Chrome extension for Rails development). | |
# https://github.com/dejan/rails_panel/tree/master/meta_request | |
gem 'rails-erd' # Diagrams your models. NOTE! $ brew install graphviz | |
# https://github.com/voormedia/rails-erd | |
gem 'awesome_print' # Pretty print your Ruby objects in full color and with proper indentation | |
# https://github.com/michaeldv/awesome_print | |
end | |
3. Place Nokogiri and httparty in the file | |
gem "nokogiri", "~> 1.6.0" | |
gem "httparty" | |
$ bundle install | |
$ rails g rspec:install | |
This is optionally, I went into the spec_helper and pasted the following code: | |
== code started | |
# Use color in STDOUT | |
config.color_enabled = true | |
# Use color not only in STDOUT but also in pagers and files | |
config.tty = true | |
# Use the specified formatter | |
config.formatter = :documentation # :progress, :html, :textmate | |
==code end | |
change database.yml (https://gist.github.com/Tasha25/7025173) | |
note: when copying from the gist you should copy the raw version | |
development: | |
adapter: postgresql | |
encoding: unicode | |
database: <%= File.basename(Rails.root) %>_development | |
pool: 5 | |
host: localhost | |
username: <%= ENV['PG_USERNAME'] %> | |
password: | |
test: | |
adapter: postgresql | |
encoding: unicode | |
database: <%= File.basename(Rails.root) %>_test | |
pool: 5 | |
host: localhost | |
username: <%= ENV['PG_USERNAME'] %> | |
password: | |
production: | |
adapter: postgresql | |
encoding: unicode | |
database: <%= File.basename(Rails.root) %>_production | |
pool: 5 | |
username: <%= ENV['PG_USERNAME'] %> | |
password: | |
#Creating a model that is called "Scraper" that doesn't inherit from ActiveRecord'" | |
Go to models and the create a file called scaler.rb | |
#created a models folder in spec | |
#added scraper_spec.rb to the folder | |
# I didn't run rake db:create | |
$rake db:create | |
In order to get information by understanding your test you need to go into the database to find information | |
You might put the following in | |
scraper_spec.rb | |
==== | |
require 'spec_helper' | |
describe Scraper do | |
describe ".new" do | |
it "initializes with a url" do | |
expect( Scraper.new(r) ).to be | |
expect{ Scraper.new("http://nytimes.com") }.to_not raise_error | |
end | |
it "raises an error without a url" do | |
expect{ Scraper.new }.to raise_error(ArgumentError) | |
end # this is an inner describe block in the Scraper | |
end | |
end | |
the code the makes this pass in scraper.rb is: | |
class Scraper | |
def initialize(url) | |
@url = url | |
end | |
def info | |
response = HTTParty.get("http://nytimes.com") | |
doc = Nokogiri::HTML(response) | |
end | |
end | |
You then go into the rails console of that folder and you can see if you get a response from the new york times. | |
pry> response = HTTParty.get("http://nytimes.com") | |
pry> doc = Nokogiri::HTML(response) | |
TASK === to use nokogiri to return an array of all the links on a page. The below code will have to do it in the console of the webpage. | |
==begin code | |
document.links | |
var arr = [], l = document.links; | |
for(var i=0; i<l.length; i++) { | |
arr.push(l[i].href); | |
} | |
==end code | |
If you type 'arr' in the console you will see a list of hrefs in the document. | |
Question? How do you change that to be used in nokogiri? | |
In order to see the data outside of the terminal you will have to create an ajax request and you can assign the request to buttons. | |
Creating a travis.yml file in order to have your file checked online | |
Go to the directory that you pushed to github | |
you will do | |
$touch .travis.yml #this is a hidden file | |
These are the things we need to tell .travis.yml because it is a list of things we want it to do on the cloud. | |
#what language | |
language: ruby | |
#what version | |
rvm: | |
-1.9.3 | |
#what we want to do | |
script: | |
- "bundle exec rspec spec" | |
You need to go into the the .travis.yml file and put the following | |
===code starts | |
language: ruby | |
rvm: | |
- 1.9.3 | |
script: | |
- "bundle exec rake db:create" | |
- "bundle exec rake db:migrate RAILS_ENV=test" | |
- "bundle exec rspec spec" | |
 | |
===code ends | |
afterwards you | |
1. git add . | |
2. git commit -m updated .. | |
3. git push origin master | |
When you go to https://travis-ci.org/ | |
you should people running | |
 | |
====Seeing a badge === | |
Delete the README.md file in your folder | |
You click the Build Status Icon to the right of the gear in travis. | |
You will see many different options in order to put a badge on your page. | |
You will choose the one that is for rdoc | |
you will see different things on you copy the rdoc link and you will put it in your README.rdoc | |
git add . | |
git commit -m "Updated rdoc with travis badge" | |
You will then go to check out github | |
You will see a badge on your repository. | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment