Skip to content

Instantly share code, notes, and snippets.

@agmcleod
agmcleod / sparkup.rb
Created September 13, 2011 01:16
Sparkup changes
module Redcar
class Sparkup
def self.plugin_dir
File.join(File.dirname(__FILE__), "..")
end
attr_accessor :indent_spaces
@agmcleod
agmcleod / xmlparse.rb
Created September 16, 2011 14:44
Parses an xml file with the given Object node name
# If you have an xml representation of a data entity, and need to convert to CSV, this script works pretty well.
# Specify the file name of an xml file, and the node name of the object. So if you have an XML file of <Person> objects that has all the fields in child elements.
# Specify the Person node, and it will iterate through to create a csv. Each person will be a separate row.
require 'rubygems'
require 'nokogiri'
puts 'type xml file name'
xml_name = gets.chomp
f = File.open(xml_name)
require 'mathn'
require 'csv'
def to_rad(angle)
angle/180 * Math::PI
end
def get_distance(from, to)
earth_radius = 6371
dLat = to_rad(to.first - from.first)
@agmcleod
agmcleod / Error.txt
Created October 14, 2011 12:16
Fix for comment selection command - ruby 1.9.2
/tmp/temp_textmate.J8yW2j:139:in /bin/bash: -c: line 0: unexpected EOF while looking for matching `''
/bin/bash: -c: line 1: syntax error: unexpected end of fileto_a' for "sdfd":String (NoMethodError)
@agmcleod
agmcleod / calculate_cites_to_latlng.rb
Created October 28, 2011 02:54
Get 5 nearest latlng values.
require 'mathn'
require 'csv'
def to_rad(angle)
angle/180 * Math::PI
end
def get_distance(from, to)
earth_radius = 6371
dLat = to_rad(to.first - from.first)
@agmcleod
agmcleod / coffe_to_js.coffee
Created December 15, 2011 20:19
Example coffee to js
Note = Backbone.Model.extend({})
NoteView = Backbone.View.extend({
el: null,
initialize: ->
this.model.view = this
$('.view').append("<div class=\"note\" id=\"note_#{this.model.get('id')}\"><a>X</a><p>#{this.model.get('body')}</p><cite>#{this.model.get('author')}</cite></div>")
this.el = $("#note_#{this.model.get('id')}")
self = this
this.el.children('a').click ->
@agmcleod
agmcleod / wrapper.rb
Created January 3, 2012 22:09
ruby kata, word wrap
class Wrapper
def self.wrap(string, column_to_wrap)
recursive_wrap(string.reverse.scan(/\S+\s?/), column_to_wrap).reverse
end
def self.recursive_wrap(array, column)
head = array[0]
if head.scan(/.{1,#{column}}/).length > 1
head = head.strip.reverse.scan(/.{1,#{column}}/).collect{ |c| c.reverse }.reverse.join("\n")
end
class CharityProductDeleteJob < Struct.new(:charity_id, :shop_id)
def perform
charity = Charity.find(charity_id)
shop = Shop.find(shop_id)
ShopifyAPI::Base.site = shop.api_url
products = ShopifyAPI::Product.find(:all, params: { handle: "#{charity.handle}-charity" })
unless products.blank?
Rails.logger.debug "Destroy product"
products.each { |p| p.destroy }
end
@agmcleod
agmcleod / 1.9.2_tests
Created February 23, 2012 15:19
ruby 1.9.3-p125 and 1.9.2-p290 tests
aaronmcleod@MacBook-Pro $ rvm ruby-1.9.2-p290
aaronmcleod@MacBook-Pro $ bundle exec rspec spec
................................................*......
Pending:
CopyProcess::Processor#compile_files_to_element_types
# No reason given
# ./spec/copyprocess/processor_spec.rb:222
Finished in 1.55 seconds
@agmcleod
agmcleod / home_spec.rb
Created February 28, 2012 05:07
shopify api integration testing
require 'spec_helper'
describe "home" do
before do
@domain = "myshop.myshopify.com"
@token = SecureRandom.hex(16)
@shopify_session = ShopifyAPI::Session.new(@domain, @token)
end