Skip to content

Commit

Permalink
Added upload class method
Browse files Browse the repository at this point in the history
  • Loading branch information
jamonholmgren committed Sep 17, 2012
1 parent c8dc9e5 commit a04c234
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions lib/parse_resource/base.rb
Expand Up @@ -250,6 +250,33 @@ def self.resource
RestClient::Resource.new(base_uri, app_id, master_key)
end

# Creates a RESTful resource for file uploads
# sends requests to [base_uri]/files
#
def self.upload(file_instance, filename, options={})
if @@settings.nil?
path = "config/parse_resource.yml"
environment = defined?(Rails) && Rails.respond_to?(:env) ? Rails.env : ENV["RACK_ENV"]
@@settings = YAML.load(ERB.new(File.new(path).read).result)[environment]
end

base_uri = "https://api.parse.com/1/files"

#refactor to settings['app_id'] etc
app_id = @@settings['app_id']
master_key = @@settings['master_key']

options[:content_type] ||= 'image/jpg' # TODO: Guess mime type here.
file_instance = File.new(file_instance, 'rb') if file_instance.is_a? String

private_resource = RestClient::Resource.new "#{base_uri}/#{filename}", app_id, master_key
private_resource.post(file_instance, options) do |resp, req, res, &block|
return false if resp.code == 400
return resp
end
false
end

# Find a ParseResource::Base object by ID
#
# @param [String] id the ID of the Parse object you want to find.
Expand Down

0 comments on commit a04c234

Please sign in to comment.