Skip to content

Instantly share code, notes, and snippets.

@softwaregravy
softwaregravy / myads_sample.rb
Created January 26, 2011 20:40
Getting an image into Base64
We couldn’t find that file to show.
@softwaregravy
softwaregravy / api.feature
Created February 26, 2011 20:27 — forked from guenter/api.feature
I prefer symbols to strings when accessing hash. So make the looks with_indifferent_access
Feature: API
In order to use the service from third party apps
As a user
I want to be able to use an API
Background:
Given a user exists # Pickle
And I login as the user using basic auth
Scenario Outline: Get a ticket
@softwaregravy
softwaregravy / validate_phone_number.rb
Created March 1, 2011 13:56
Accepting any 10 digit number in a string as a phone number
class XXX < ActiveRecord::Base
before_validation :format_phone
validates_presence_of :phone
validates_length_of :phone, :is => 10
attr_accessible :phone
@softwaregravy
softwaregravy / global_request_logging
Created March 7, 2011 15:45
the code I use to log all requests and responses (watch out not to log html)
# inside Application controller
around_filter :global_request_logging
def global_request_logging
http_request_header_keys = request.headers.keys.select{|header_name| header_name.match("^HTTP.*")}
http_request_headers = request.headers.select{|header_name, header_value| http_request_header_keys.index(header_name)}
logger.info "Received #{request.method.inspect} to #{request.url.inspect} from #{request.remote_ip.inspect}. Processing with headers #{http_request_headers.inspect} and params #{params.inspect}"
begin
yield
@softwaregravy
softwaregravy / print_all_attributes
Created March 11, 2011 06:23
Printing out all attributes
<% @merchant_campaign.instance_variable_get(:@attributes).each do |attribute, value| %>
<tr>
<td><%= attribute %></td>
<td><%= value %></td>
</tr>
<% end %>
@softwaregravy
softwaregravy / render_render
Created March 12, 2011 16:54
calling another template from anther template
<% f.fields_for :member_collection do |builder| %>
<%= render "membrer_collection_fields", :form => builder %>
<% end %>
def serializable_hash(options = {})
my_options = {:except => [:id, :category, :draws, :owner_identifier, :owner_reference_data,
:primary_image, :related_data, :created_at, :updated_at, :creating_user_id ],
:include => []}.merge(options ||= {})
s = serializable_hash(my_options)
s["address"] = address.serializable_hash_for_public(options)
s
end
def serializable_hash(options = {})
# TODO exclude the id
options = {:include => [:address, :location, :hours_of_operations, :slow_periods, :discounts],
:except => [:created_at, :updated_at, :creating_user_id]}.merge(options ||= {})
super options
end
@softwaregravy
softwaregravy / gist:1054751
Created June 29, 2011 19:51
Always run the job
class JobClass
def perform
begin
#I do stuff here
rescue Exception => e
# catch all exceptions
HoptoadNotifier.notify(e)
ensure
# we want this job to just run continuously, but we don't have an 'auto' way to do that
# so we schedule a new one every time we run
@softwaregravy
softwaregravy / Questions
Created July 11, 2011 21:42
Magma Rails Give-away
Day Job: Sr. Software Engineer at ThinkNear
Open Source contribution (if any): rails docs and guides
Tell me about your experience with Ruby/Rails: started using it in fall 2010, and have been on it full time every since. Big change from my background in Java (used to work at Amazon.com)
How do you use GitHub: for everything. personal and business. We keep our companies repositories on github. We also use it to collaborate with contractors.
Favorite luchador(es):Rikochet