Cory O'Daniel – These are just words Software development, thoughts, and randomness

27Jul/101

My Genius Invention to revolutionize using the iPhone 4.

So, I'm an Apple Fanboi, so I don't mind finding ways to make their products MORE AWESOME. It's needless to say that the iPhone 4 is revolutionary, but I have made a draft of an invention I am working on that will make the iPhone 4 a truly genius piece of technology.

You'd better sit down.

Your iPhone 4 can do all sorts of cool things like fling angry birds at pigs, help you cook an awesome hamburger, take pictures of girls buts in line at starbucks and send them to your friends, but imagine if you could use the iPhone 4, wait for it, as a phone.

Imagine if you could call people on it and have a conversation without being interrupted by ear muting, ear hangups, holding it wrong and hanging up, or accidentally conference calling your ex girlfriend into a conversation with your current girlfriend with your ear.

Yeah, it'd be a utopian life. So I decided I was going to revolutionize this revolutionary device and low and behold here is what I came up with.

Now, this is just a rough draft. I'm still working on the technicalities. I'm hoping since the internet is a trustworthy place none of you will find this and beat me to the punch on creating this device.


View Full Size Here

Apple, if you would like to buy my patent please feel free to contact me. Steve, I know you are completely intrigued by the idea of being able to use the iPhone as a phone. It blew me away too.

Update - Initial reports are in from my prototype. It turns out if you have hair and use the headset it actually shorts out the antenna. So for now its just for bald people. I'm working on a bumper for the head band.

Post to Twitter Post to Digg Post to Facebook Post to Reddit Post to StumbleUpon

Filed under: Random 1 Comment
7Jul/100

Testing if something is ‘true’ to all objects in an array/collection in ruby

Maybe there is something that does this already. I didnt know, so I whipped it together really quick. Its an extension to the array class that lets you know if something is 'true' to all objects in the array.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Array
 
  def all_true?(&block)
    t_ref = true  
 
    if block_given?
      self.each do |_val_|
        t_ref &= !!(yield(_val_))
      end
    else
      self.each do |_val_|
        t_ref &= !!(_val_)
      end
    end
 
    t_ref
  end
 
end

You can use / test it like so:

1
2
3
4
5
6
[ true, true, true ].all_true?  #=> true
[ true, true, false ].all_true?  #=> false
 
# Want to run some other non boolean logic on it?
[ 1, 2, 3, 4, 5].all_true?{ |val| val.integer? } #=> true
[ 1, 'a', 3, 4, 6].all_true?{ |val| val.integer? } #=> false

Yeah, I just need to know is shits the same across the whole collection of active record objects sometimes. Now I can do it, go data.

Post to Twitter Post to Digg Post to Facebook Post to Reddit Post to StumbleUpon

14Jun/100

Testing 404/500, Error Pages with Webrat visit method

This is a super simple snippet, and maybe there is something built into webrat, but I didn't find it. I needed to test for 404 pages in some of my tests (I was writing tests that made sure a page didnt show for disabled content). Purposely getting 404's can be annoying though since webrat opens up the 404 page in your default browser. There is a variable available in Webrat's configuration that you can use to temporarily disable this functionality. I wrapped it in the following method:

1
2
3
4
5
  def visit_but_dont_open(url)
    Webrat.configuration.instance_variable_set("@open_error_files", false)
    visit url
    Webrat.configuration.instance_variable_set("@open_error_files", true)
  end

Throw that in your test_helper and you can start doing:

1
2
visit_but_dont_open "Http://example.com/page/that/doesnt/exist"
assert !response.ok?

Yay, 404.

Post to Twitter Post to Digg Post to Facebook Post to Reddit Post to StumbleUpon

Tagged as: , , No Comments
24May/102

Lost finale – did i get it?

I think I understood the lost finale? What I understood was that everything that happened on the island was REAL. It all happened. The monster, the time travel, the dharma initiative. Everything. As the losties died off they went to the side world where they could meet up and "move on" together. So when we saw the plane leave, those people left the island, went on to have lives and eventually died. Hurley and Ben stayed on the island for however long and eventually died.

This is supported by the following statements I think?

Hurley to Ben - you were a good #2

Christian to Jack - everybody dies some die before you some die way after you

Christian to Jack - the time on the island was the most important part of your life

Am I on the right track?

Post to Twitter Post to Digg Post to Facebook Post to Reddit Post to StumbleUpon

Tagged as: 2 Comments
7May/100

Ruby 1.9 Date#strftime adds a space on %b

So, I had a spec failing in my integration suite and I couldn't figure out what the hell it was - EVERYTHING LOOKED LEGIT. I even logged into the site, and visually verified it. Whats the dilly?

I upgraded to ruby 1.9 from ruby 1.8 and apparently, when doing strftime on date, you get a free whitespace character with "%b"

1
2
3
# Ruby 1.8
irb(main):006:0> Date.today.strftime("%b %e, %Y")
=> "May 7, 2010"
1
2
irb(main):007:0> Date.today.strftime("%b %e, %Y")
=> "May  7, 2010" # TWO SPACES

Super dumb - wasted a good 10 minutes of my day because I could see the diff between:

1
2
"May  7, 2010"
"May 7, 2010"

In the middle of a web page full of other content.

Why the freebee space character on the %b? Dunno. I just smashed my stftime statement together and my specs are rolling.

1
Date.today.strftime("%b%e, %Y")

Uh, yeah - this is my bug report.

Post to Twitter Post to Digg Post to Facebook Post to Reddit Post to StumbleUpon

17Apr/100

Padrino, Compass, and Sass – Working happily via Ian Serlin

My cohort, Ian Serlin, discovered this. In a project we are working on we could get Compass to play well with PadrinoRb. It seems like the #sass method doesn't care about the options being passed to it, and we kept getting stack traces rendered into our CSS files. The stack trace was ruby looking for - and failing to find compass/reset.css.

This is the code that DOESN'T work (padrino 0.9.10, compass 0.8.17, sinatra 1.0).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
  configure do
    register SassInitializer #The Rack Sass reloader...
 
    Compass.configuration do |config|
      config.project_path = File.dirname(__FILE__)
      config.sass_dir = "stylesheets"
      config.project_type = :stand_alone
      config.http_path = "/"
      config.css_dir = "stylesheets"
      config.images_dir = "images"
      config.output_style = :compressed
    end
  end   
  get '/stylesheets/:file.css' do
    content_type 'text/css', :charset => 'utf-8'
    # This is the doc on how to give Sass some more load paths to find the compass  files. 
    #   it doesnt work :P and for that matter, neither does:
    # sass :file, Compass.sass_engine_options
    # or the set :sass, whatever_hash_here
    sass :file, :sass => Compass.sass_engine_options
  end

Our solution was to force Sass options and Compass options to merge...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
  configure do
    register SassInitializer
 
    Compass.configuration do |config|
      config.project_path = File.dirname(__FILE__)
      config.sass_dir = "stylesheets"
      config.project_type = :stand_alone
      config.http_path = "/"
      config.css_dir = "stylesheets"
      config.images_dir = "images"
      config.output_style = :compressed
    end
 
    Sass::Plugin.options.merge!(Compass.sass_engine_options)
  end
 
  get '/stylesheets/:file.css' do
    content_type 'text/css', :charset => 'utf-8'
    sass :file
  end

Yay, magic spells - it works. You can check out more ian magic spells at ianserlin.com.

Post to Twitter Post to Digg Post to Facebook Post to Reddit Post to StumbleUpon

5Mar/102

Attaching local or remote files to Paperclip and Milton Models in Rails (Mocking content_type and original_filename in a Tempfile)

I was working on a project today where I needed to import some data from MySpace accounts (yeah, MySpace), which included importing the users profile image. In the controller that did the importing I was using OpenURI to retrieve the image and then turn it into a Tempfile to be attached the the model, like so:

1
2
3
4
5
6
7
def import
#...snip
  tempfile = Tempfile.new( my_filename)
  tempfile.write open( image_url ).read
  @imported_user.images.create(:file => tempfile)
#...snip
end

This doesn't work. It blows up missing one of two methods:

  1. #original_filename
  2. #content_type

If you inspect a normal file upload in Rails which has these methods, you'll find that is just a regular old Tempfile. But it has the methods! If you create a Tempfile manually, it won't have the methods. That's because Rails magics them on. I am not sure why they don't create a subclass like Rails::Tempfile that contains these methods and just use that. I guess its because OOP is retarded (sarcasm).

So, I wrote a little subclass that will take a file path, and quack like the magic'd rails Tempfiles you get from an upload so you can attach local files to models or even remote files.

This works pretty straightforward and I'm currently using it in production. It won't work on windows systems because the dependency on the 'file' binary. Also, some linux systems are missing this library by default, so make sure you yum|dpkg|apt|port or whatever to get it installed.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
require 'open-uri'
require 'digest/sha1'
 
class RemoteFile < ::Tempfile
 
  def initialize(path, tmpdir = Dir::tmpdir)
    @original_filename  = File.basename(path)
    @remote_path        = path
 
    super Digest::SHA1.hexdigest(path), tmpdir
    fetch
  end
 
  def fetch
    string_io = OpenURI.send(:open, @remote_path)
    self.write string_io.read
    self.rewind
    self
  end
 
  def original_filename
    @original_filename
  end
 
  def content_type
    mime = `file --mime -br #{self.path}`.strip
    mime = mime.gsub(/^.*: */,"")
    mime = mime.gsub(/;.*$/,"")
    mime = mime.gsub(/,.*$/,"")
    mime
  end
end

Usage is pretty simple:

1
2
3
remote_file = RemoteFile.new("http://www.google.com/intl/en_ALL/images/logo.gif")
remote_file.original_filename #=> logo.gif
remote_file.content_type #= image/gif

Using it in your controller:

1
2
3
4
5
def import
  #...snip
  @imported_user.images.create(:file => RemoteFile.new( url_to_image ))
  #...snip
end

Post to Twitter Post to Digg Post to Facebook Post to Reddit Post to StumbleUpon

25Feb/101

A ruby wrapper for Google AjaxLibs (jquery, jquery ui, mootools, prototype, swfobject, etc) [Lazy GoogleJsApi Wrapper]

I like using the Google AjaxLibs API. Its cool to not host files when I don't have to, especially ones I know a user has already probably cached from Google anyway (even though Google Page speed busts my balls about too many DNS lookups :P ).

I do on the other hand hate watching in the bottom of my browser "Waiting for google.com" when using the standard Javascript API for loading the Libraries like so:

1
2
3
4
<script type="text/javascript" src="http://www.google.com/jsapi?key=INSERT-YOUR-KEY"></script>
<script type="text/javascript">
 google.load("jquery", "1.4.2");
</script>

And you don't have to wait, you can totally do something like:

1
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>

Which is why I wrote this little ruby wrapper. Its nothing special. It just encapsulates all the URLs for the libraries and their versions to allow you to not remember the URL and not have to go looking for the documentation when you start a new app or just need an additional library.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# Docs: http://code.google.com/apis/ajaxlibs/documentation/index.html
#
class GoogleJsApi
  BaseURL = "http://ajax.googleapis.com/ajax/libs".freeze
 
  Libraries = { 
    "jquery" => {
      :versions => %w(1.2.3 1.2.6 1.3.0 1.3.1 1.3.2 1.4.0 1.4.1 1.4.2),
      :compressed_url => "/jquery/%s/jquery.min.js",
      :uncompressed_url => "/jquery/%s/jquery.js"
    },
    "jqueryui" => {
      :versions => %w(1.5.2 1.5.3 1.6 1.7.0 1.7.1 1.7.2),
      :compressed_url   => "/jqueryui/%s/jquery-ui.min.js",
      :uncompressed_url => "/jqueryui/%s/jquery-ui.js"
    },
    "prototype" => {
      :versions => %w(1.6.0.2 1.6.0.3 1.6.1.0),
      :compressed_url   => "",
      :uncompressed_url => "/prototype/%s/prototype.js"
    },
    "scriptaculous" => {
      :versions => %w(1.8.1 1.8.2 1.8.3),
      :compressed_url   => "",
      :uncompressed_url => "/scriptaculous/%s/scriptaculous.js"
    },
    "mootools" => {
      :versions => %w(1.1.1 1.1.2 1.2.1 1.2.2 1.2.3 1.2.4),
      :compressed_url => "/mootools/%s/mootools-yui-compressed.js",
      :uncompressed_url => "/mootools/%s/mootools.js"
    },
    "dojo" => {
      :versions => %w(1.1.1 1.2.0 1.2.3 1.3.0 1.3.1 1.3.2 1.4.0 1.4.1),
      :compressed_url => "/dojo/%s/dojo/dojo.xd.js",
      :uncompressed_url => "/dojo/%s/dojo/dojo.xd.js.uncompressed.js"
    },
    "swfobject" => {
      :versions => %w(2.1 2.2),
      :compressed_url   => "/swfobject/%s/swfobject.js",
      :uncompressed_url => "/swfobject/%s/swfobject_src.js"
    },
    "yui" => {
      :versions => %w(2.6.0 2.7.0 2.8.0r4),
      :compressed_url   => "/yui/%s/build/yuiloader/yuiloader-min.js",
      :uncompressed_url => "/yui/%s/build/yuiloader/yuiloader.js"
    },
    "ext-core" => {
      :versions => %w(3.0.0 3.1.0),
      :compressed_url   => "/ext-core/%s/ext-core.js",
      :uncompressed_url => "/ext-core/%s/ext-core-debug.js"
    },
    "chrome-frame" => {
      :versions => %w(1.0.0 1.0.1 1.0.2),
      :compressed_url   => "/chrome-frame/%s/CFInstall.min.js",
      :uncompressed_url => "/chrome-frame/%s/CFInstall.js"
    } 
  }.freeze
 
  class << self
    def include(name, version=nil, compressed=true, validate_version=true)
      name = name.to_s
      if lib = GoogleJsApi::Libraries[name]
        version ||= lib[:versions].last
 
        if validate_version && lib[:versions].include?(version)
          GoogleJsApi.url_for(name, version, compressed)
        elsif !validate_version
          GoogleJsApi.url_for(name, version, compressed)
        else
          raise Exception, "Invalid version (#{version}) for #{name}"
        end
      else
        raise Exception, "Unknown Google Javascript Library"
      end
    end
 
    def version_info
      tmp_version = {}
      GoogleJsApi::Libraries.each do |k,v|
        tmp_version[ k ] = v[:versions]
      end
      tmp_version
    end
 
    protected
    def url_for(name, version, compressed)
      GoogleJsApi::BaseURL +
      GoogleJsApi::Libraries[ name ][ compressed ? :compressed_url : :uncompressed_url ] % version
    end
  end
 
end

Here are some tests for it if you are interested in how/if it works:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
require 'pp'
 
pp GoogleJsApi.version_info
 
# Include the newest version of a library
puts GoogleJsApi.include('jquery')
 
# Include a specific version of a library
puts GoogleJsApi.include "chrome-frame", "1.0.0"
 
# Include a specific version of a library uncompressed
puts GoogleJsApi.include "chrome-frame", "1.0.0", false
 
# Include a specific version of a library w/o validating the version; useful if this goes out of date :P
puts GoogleJsApi.include "jquery", "NOT_A_VERSION", false, false
 
begin
  puts GoogleJsApi.include :jqueryui, "1.0"
rescue Exception => e
  puts "This should blow up..."
  puts e.message
end

If you want to use it in your Rails or Merb app you can do:

1
2
3
4
5
# Rails
require 'google_js_api' #or whatever you named the file when you dropped it in lib
 
# In your application layout or whatever
javascript_include_tag GoogleJsApi.include(:jquery, "1.4.0")
1
2
3
4
5
# Merb
require 'google_js_api' #or whatever you named the file when you dropped it in lib
 
#in your application layout or where ever
require_js GoogleJsApi.include(:swfobject)

Its simple, its lazy. I like it.

Post to Twitter Post to Digg Post to Facebook Post to Reddit Post to StumbleUpon

23Feb/100

Why the hell can’t Apple fix this bug? – Snow Leopard Keyboard hangs when changing spaces

Everyone knows about this bug, except apparently Apple.

In Snow Leopard the keyboard hangs occasionally when changing spaces. Sometimes for only a few seconds, sometimes forever. All sorts of people are talking or reporting about this issue.

Why isn't it being fixed? Hallo there, Apple? Are you there?

There are currently about 100 posts in the Apple support thread dating back from Sep 15, 2009. You know whats annoying? Killing your Dock constantly while you are working.

Apple: SrSLy 4real. Fix this. Its equivalent to working really hard and someone smacking you in the forehead randomly. It kinda sucks, and its really irritating.

(Note: My keyboard only hung once during this post).


Update: March 3, 2010

After some further research, at least for me and a few others, this bug is tied to an application called Cinch. If you have Cinch installed, try uninstalling it and seeing if this problem goes away. A pretty useful app, but I'd rather resize my windows manually than 'get interrupted' when I'm in my workflow.

I uninstalled Cinch 5 days ago, and the problem has gone away for me. I was having my keyboard hang 4 or 5 times a day with it installed.

Post to Twitter Post to Digg Post to Facebook Post to Reddit Post to StumbleUpon

11Feb/100

Lower ACL Permissions on Amazon S3 items with ruby

I recently had to change a bunch of permissions on some items on S3. Unfortunately the items were mixed in with items that I didn't want to change the permissions on and the file names are all kinda jumbled, so I couldn't pinpoint what I needed to change by eyeballing the file names.

So I wrote a little ruby method to go in and change something given a key and bucket name.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
require 'aws/s3'
 
AWS::S3::Base.establish_connection!(
  :access_key_id     => "YOUR_ACCESS_KEY",
  :secret_access_key => "YOUR_SECRET_KEY"
)
 
include AWS::S3
def s3_set_public_read(key, bucket)
  puts "Processing: #{key}"  
 
  policy = S3Object.acl(key, bucket)
  policy.grants << ACL::Grant.grant(:public_read)
  policy.grants << ACL::Grant.grant(:public_read_acp)
  S3Object.acl(key, bucket, policy)      
end

Now I can just run a loop of ActiveRecord objects around that method and convert them all to public read.

If you want a GUI tool for managing S3 stuff, I totally recommend S3Hub. Its a really sexy tool for managing S3.

Post to Twitter Post to Digg Post to Facebook Post to Reddit Post to StumbleUpon

Tagged as: , , No Comments