Skip to content

Instantly share code, notes, and snippets.

View oliverbarnes's full-sized avatar

Oliver Azevedo Barnes oliverbarnes

View GitHub Profile
class Person < ActiveRecord::Base
attr_accessor :cell_area_code, :cell_number
before_save :merge_cellphone
private
def merge_cellphone
self.cellphone = "(#{cell_area_code}) #{cell_number}"
<% form.input :cellphone do %>
<%= form.input :area_code %>
<%= form.input :number %>
#perhaps even for extensions
<%= form.input :extension %>
<% end %>
require 'rubygems'
require 'mechanize'
agent = Mechanize::new
agent.user_agent = "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_0; en-US) AppleWebKit/533.4 (KHTML, like Gecko) Chrome/5.0.375.127 Safari/533.4"
begin
page = agent.get 'http://asdapriceguarantee.co.uk/Compare-Prices/ASDA-receipt.aspx'
rescue Mechanize::ResponseCodeError
puts "ResponseCodeError - Code: #{$!}"
## Continents and Countries
Continent.create!({:name => 'North America' }) do |continent|
continent.countries << Country.create!({:name => 'Canada'})
continent.countries << Country.create!({:name => 'USA'})
end
Continent.create!({:name => 'South America' }) do |continent|
continent.countries << Country.create!({:name => 'Argentina'})
continent.countries << Country.create!({:name => 'Bolivia'})
continent.countries << Country.create!({:name => 'Chile'})
Continent.all.each {|c| puts c.name; c.countries.each {|ct| puts " "+ct.name}}
{
'North America' => ['Canada', 'USA'],
'South America' => ['Argentina', 'Bolivia', 'Chile'],
'Europe' => ['France', 'Netherlands', 'Norway', 'Scotland', 'Spain', 'Sweden'],
'United Kingdom' => ['England', 'Ireland'],
'Africa' => ['South Africa'],
'Asia' => ['China', 'India', 'Japan', 'Malaysia', 'Singapore', 'South Korea', 'Taiwan', 'United Arab Emirates'],
'Australasia' => ['Australia', 'Indonesia']
}.map do |continent, countries|
#mongomapper doesn't seem to support passing blocks to Model.create!
@oliverbarnes
oliverbarnes / env.rb
Created March 4, 2011 19:58
Cucumber setup with spork and database_cleaner for mongo_mapper
require 'rubygems'
require 'spork'
Spork.prefork do
ENV["RAILS_ENV"] ||= "test"
require File.expand_path(File.dirname(__FILE__) + '/../../config/environment')
...cucumber and capybara requires...
require 'database_cleaner'
@oliverbarnes
oliverbarnes / gist:864318
Created March 10, 2011 15:55
gemlock.diff
--- Gemfile.lock
+++ (clipboard)
@@ -168,7 +168,7 @@
rubyzip
simple_form (1.3.1)
spork (0.9.0.rc4)
- sugar-high (0.3.9)
+ sugar-high (0.3.7)
mocha (>= 0.9.8)
require_all (~> 1.2.0)
@oliverbarnes
oliverbarnes / dragonfly_with_image_name_in_url.rb
Created March 22, 2011 00:20
Finding dragonfly image with cucumber/capybara
#config
app.url_path_prefix = '/images'
#route
match '/images/:dragonfly/:file_name', :to => Dragonfly[:images]
#model
class Store
include MongoMapper::Document
key :photo_uid, String
@oliverbarnes
oliverbarnes / gist:904733
Created April 5, 2011 22:42
Chargify API doc example
Scenario: Retrieve a customer via my reference id (as an integer or simple string)
Given I have a customer with these attributes
| reference | first_name | last_name | email |
| 7890 | Joe | Blow | [email protected] |
When I send a GET request to https://[@subdomain].chargify.com/customers/lookup.xml?reference=7890
Then the response status should be "200 OK"
And the response should be the xml:
"""
<?xml version="1.0" encoding="UTF-8"?>
<customer>