Skip to content

Instantly share code, notes, and snippets.

@dav
Created August 12, 2010 14:23
Show Gist options
  • Save dav/521041 to your computer and use it in GitHub Desktop.
Save dav/521041 to your computer and use it in GitHub Desktop.
require 'rubygems'
require 'curb'
require 'fileutils'
SERVER = 'http://localhost:3000'
USERS_URL = SERVER+'/users'
PROFILES_URL = SERVER+'/profiles'
LOCAL_PICTURE_PATH = '/Users/dav/Pictures/111minna-2.jpg'
class MyProfile
attr_accessor :email, :password, :name, :birthdate, :height, :photo_file_path
def create
@email = "#{@name}@example.com" if @email.nil?
@password = 'pass' if @password.nil?
# create the user record
c = Curl::Easy.new(USERS_URL)
c.headers = {'content-type' => 'text/xml', 'accept' => 'text/xml'}
post_data = "<user><email>#{@name}@example.com</email><password>#{@password}</password><password_confirmation>#{@password}</password_confirmation></user>"
c.http_post(post_data)
if c.response_code != 201 && c.response_code != 422
puts "User Create response for #{post_data}:\n#{c.response_code}\n#{c.body_str}\n---"
else
# update the profile for the new user
put_data = []
put_data << Curl::PostField.content('name', @name) if self.name
put_data << Curl::PostField.content('birthdate', @birthdate) if @birthdate
put_data << Curl::PostField.content('height', @height) if @height
if @photo_file_path
puts "FILE: local:#{@photo_file_path} remote:#{File.basename(@photo_file_path)}"
put_data << Curl::PostField.file('file', @photo_file_path)
end
# post
begin
c = Curl::Easy.new(PROFILES_URL)
c.multipart_form_post = true
c.http_put(put_data)
rescue Exception => e
p e
p c
end
puts "Profile Update response #{c.response_code}\n#{c.body_str}\n---"
end
end
end
#if (ARGV.size != 1)
# puts "Pass in the name of the keyword to do a google image search on"
# exit
#end
if __FILE__ == $0
mp = MyProfile.new
mp.name = 'test11'
mp.photo_file_path = LOCAL_PICTURE_PATH
begin
mp.create
rescue Curl::Err::ConnectionFailedError => cf
p cf
end
end
__END__
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment