AWS S3 using AWS/S3 Ruby gem

working on storing files to Amazon’s S3 web service. Here’s what I’ve come up with so far:

1
2
3
4
5
6
7
8
9
10
11
12
require 'aws/s3'
def aws_connect
  @aws_connect ||= AWS::S3::Base.establish_connection!(
    :access_key_id=>AWS_KEY,
    :secret_access_key=>AWS_SECRET
  )
end
 
def aws_upload(file,bucket=AWS_BUCKET)
  aws_connect
  AWS::S3::S3Object.store(file,open(file),bucket)
end

then a:

1
aws_upload('test.txt')

Will upload the the file to the Amazon bucket. Pretty simple.

Posted: March 22nd, 2010 | Author: jay | Filed under: Code | Tags: , , , , , , , , | No Comments »

OAuth summarized

Application Scope

1
2
# create the consumer...
consumer ||= OAuth::Consumer.new(KEY, SECRET, {:site => SITE, :authorize_path => PATH })

Session Scope

1
2
3
4
5
# create the request token...
rt=consumer.get_request_token({ :oauth_callback => OAUTH_CALLBACK_URL })
# save the request token and secret in the session...
session[:r_token]=rt.token
session[:r_secret]=rt.secret

User Scope (Model)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# use session values to create the request token...
rt=OAuth::RequestToken.new(consumer, session[:r_token], session[:r_secret])
# grab the user data from the OAuth provider...
access_token=rt.get_access_token({:oauth_verifier=>params[:oauth_verifier]})
oauth_user_json=access_token.get(VERIFY_PATH).body
oauth_user=JSON.parse(oauth_user_json)
# create or find the the user (using twitter.com for the email address - could use some work)... 
u=TwitterUser.first_or_create(:email=>"#{oauth_user['screen_name']}@twitter.com")
u.username=oauth_user['screen_name']
u.save!
u.oauth_tokens.all.destroy
u.oauth_tokens.new(:user_access_token=>access_token.to_yaml)
u.save!
# set the session user for future use...
session[:user]=u.id
...
# and when you need access to the OAuth provider again, use the access_token stored in the User model
u=User.first(:id=>session[:user])
access_token=YAML::load(u.oauth_tokens.first.user_access_token)
verify=access_token.get(OAUTH_PROVIDERS["https://twitter.com"][:verify_path]).body
Posted: March 17th, 2010 | Author: jay | Filed under: Code | Tags: , , , , , , , , , | No Comments »

Trying out Pony for email

Email is going to be used to activate an account, and I’m leaning towards using the Pony gem to simplify the creations and sending.

Here’s how simple Pony is:

1
Pony.mail(:to => 'test@example.com', :from=>'test@example.com', :subject=>'spam', :body=>(erb :registration_email))

and here’s how you can test it with the pony-test gem:

1
2
3
4
5
6
7
8
9
10
  def test_valid_new_user_signup_should_create_email
    count=all_email.count
    create_user
    assert_equal count+1, all_email.count
  end
 
  def test_signup_email_should_contain_activation_link
    create_user
    assert_match /http\:\/\//, current_email.body
  end

Don’t forget to nclude the Pony test helpers in your test class…

1
include Pony::TestHelpers
Posted: March 12th, 2010 | Author: jay | Filed under: Code | Tags: , , , , | No Comments »