lib/parse_resource/base.rb in parse_resource-1.7.2 vs lib/parse_resource/base.rb in parse_resource-1.7.3

- old
+ new

@@ -138,10 +138,12 @@ case @attributes[k]["__type"] when "Pointer" result = klass_name.constantize.find(@attributes[k]["objectId"]) when "Object" result = klass_name.constantize.new(@attributes[k], false) + when "File" + result = @attributes[k]["url"] end #todo: support Dates and other types https://www.parse.com/docs/rest#objects-types else result = @attributes[k] end @@ -154,61 +156,10 @@ @attributes.each_pair do |k,v| create_setters!(k,v) create_getters!(k,v) end end - - def self.has_many(children, options = {}) - options.stringify_keys! - - parent_klass_name = model_name - lowercase_parent_klass_name = parent_klass_name.downcase - parent_klass = model_name.constantize - child_klass_name = options['class_name'] || children.to_s.singularize.camelize - child_klass = child_klass_name.constantize - - if parent_klass_name == "User" - parent_klass_name = "_User" - end - - @@parent_klass_name = parent_klass_name - @@options ||= {} - @@options[children] ||= {} - @@options[children].merge!(options) - - send(:define_method, children) do - @@parent_id = self.id - @@parent_instance = self - - parent_klass_name = case - when @@options[children]['inverse_of'] then @@options[children]['inverse_of'].downcase - when @@parent_klass_name == "User" then "_User" - else @@parent_klass_name.downcase - end - - query = child_klass.where(parent_klass_name.to_sym => @@parent_instance.to_pointer) - singleton = query.all - - class << singleton - def <<(child) - parent_klass_name = case - when @@options[children]['inverse_of'] then @@options[children]['inverse_of'].downcase - when @@parent_klass_name == "User" then @@parent_klass_name - else @@parent_klass_name.downcase - end - if @@parent_instance.respond_to?(:to_pointer) - child.send("#{parent_klass_name}=", @@parent_instance.to_pointer) - child.save - end - super(child) - end - end - - singleton - end - - end @@settings ||= nil # Explicitly set Parse.com API keys. # @@ -248,10 +199,37 @@ app_id = @@settings['app_id'] master_key = @@settings['master_key'] 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 JSON.parse(resp) + end + false + end + # Find a ParseResource::Base object by ID # # @param [String] id the ID of the Parse object you want to find. # @return [ParseResource] an object that subclasses ParseResource. def self.find(id) @@ -294,22 +272,30 @@ # Limits the number of objects returned # def self.limit(n) Query.new(self).limit(n) end - - def self.order(attribute) - Query.new(self).order(attribute) + + # Skip the number of objects + # + def self.skip(n) + Query.new(self).skip(n) end + + #def self.order(attribute) + # Query.new(self).order(attribute) + #end # Create a ParseResource::Base object. # # @param [Hash] attributes a `Hash` of attributes # @return [ParseResource] an object that subclasses `ParseResource`. Or returns `false` if object fails to save. def self.create(attributes = {}) attributes = HashWithIndifferentAccess.new(attributes) - new(attributes).save + obj = new(attributes) + obj.save + obj end def self.destroy_all all.each do |object| object.destroy @@ -353,29 +339,30 @@ # https://www.parse.com/docs/ios/api/Classes/PFConstants.html error_response = JSON.parse(resp) pe = ParseError.new(error_response["code"]).to_array self.errors.add(pe[0], pe[1]) - + return false else @attributes.merge!(JSON.parse(resp)) @attributes.merge!(@unsaved_attributes) attributes = HashWithIndifferentAccess.new(attributes) @unsaved_attributes = {} create_setters_and_getters! + return true end - - self end - - result end def save if valid? run_callbacks :save do - new? ? create : update + if new? + return create + else + return update + end end else false end rescue false @@ -393,50 +380,60 @@ put_attrs.delete('updatedAt') put_attrs = put_attrs.to_json opts = {:content_type => "application/json"} result = self.instance_resource.put(put_attrs, opts) do |resp, req, res, &block| - case resp.code when 400 # https://www.parse.com/docs/ios/api/Classes/PFConstants.html error_response = JSON.parse(resp) pe = ParseError.new(error_response["code"], error_response["error"]).to_array self.errors.add(pe[0], pe[1]) + return false else @attributes.merge!(JSON.parse(resp)) @attributes.merge!(@unsaved_attributes) @unsaved_attributes = {} create_setters_and_getters! - self + return true end - - result end - end def update_attributes(attributes = {}) self.update(attributes) end def destroy - self.instance_resource.delete - @attributes = {} + if self.instance_resource.delete + @attributes = {} + @unsaved_attributes = {} + return true + end + false + end + + def reload + return false if new? + + fresh_object = self.class.find(id) + @attributes.update(fresh_object.instance_variable_get('@attributes')) @unsaved_attributes = {} - nil + + self end # provides access to @attributes for getting and setting def attributes @attributes ||= self.class.class_attributes @attributes end + # AKN 2012-06-18: Shouldn't this also be setting @unsaved_attributes? def attributes=(n) @attributes = n @attributes end