lib/adapters/parse_adapter.rb in dm-parse-0.2.1 vs lib/adapters/parse_adapter.rb in dm-parse-0.2.2

- old
+ new

@@ -9,44 +9,36 @@ VERSION = "1" APP_ID_HEADER = "X-Parse-Application-Id" API_KEY_HEADER = "X-Parse-REST-API-Key" MASTER_KEY_HEADER = "X-Parse-Master-Key" - attr_reader :classes, :users, :login, :password_reset, :file_storage + attr_reader :engine def initialize(name, options) super - @classes = build_parse_resource_for "classes" - @users = build_parse_resource_for "users" - @login = build_parse_resource_for "login" - @password_reset = build_parse_resource_for "requestPasswordReset" - @file_storage = build_parse_resource_for "files" + master = @options[:master].nil? ? false : @options[:master] + @engine = Parse::Engine.new @options[:app_id], @options[:api_key], master end - def parse_resources_for(model) - storage_name = model.storage_name - storage_name == "_User" ? users : classes[storage_name] - end - - def parse_resource_for(resource) - parse_resources_for(resource.model)[resource.id] - end - def create(resources) resources.each do |resource| - params = attributes_as_fields(resource.attributes(:property)).except("objectId", "createdAt", "updatedAt") - model = resource.model - result = parse_resources_for(model).post params: params + params = attributes_as_fields(resource.attributes(:property)).except("objectId", "createdAt", "updatedAt") + model = resource.model + storage_name = model.storage_name + result = engine.create storage_name, params + initialize_serial resource, result["objectId"] - resource.created_at = resource.updated_at = result["createdAt"] + resource.created_at = resource.updated_at = result["createdAt"].to_datetime end.size end def read(query) - model = query.model - params = parse_params_for(query) - response = parse_resources_for(model).get params: params + model = query.model + params = parse_params_for(query) + storage_name = model.storage_name + response = engine.read storage_name, params + response["results"] end # Read the "count" from Parse # This is Parse-only @@ -57,15 +49,17 @@ # @return [Integer] # the number of records that match the query # # @api semipublic def read_count(query) - model = query.model - params = parse_params_for(query) - params[:count] = 1 - params[:limit] = 0 - response = parse_resources_for(model).get params: params + model = query.model + params = parse_params_for(query) + params[:count] = 1 + params[:limit] = 0 + storage_name = model.storage_name + response = engine.read storage_name, params + response["count"] end # Login, which is Parse-only # @@ -77,11 +71,11 @@ # @return [Hash] # the user information # # @api semipublic def sign_in(username, password) - login.get params: {username: username, password: password} + engine.sign_in username, password end # Request a password reset email # Parse-only # @@ -89,11 +83,11 @@ # the email address # # @return [Hash] # a empty Hash def request_password_reset(email) - password_reset.post params: {email: email} + engine.request_password_reset email end # Upload a file # Parse-only # @@ -106,42 +100,32 @@ # @param [String] content_type # the content type # # @return [Hash] # the uploaded file information - def upload_file(filename, content, content_type = MIME::Types.type_for(filename).first) - headers = file_storage.options[:headers] - headers = headers.merge("Content-Type" => content_type) if content_type - file_storage[URI.escape(filename)].post body: content, headers: headers + def upload_file(filename, content, content_type) + engine.upload_file filename, content, content_type end def delete(resources) resources.each do |resource| - parse_resource_for(resource).delete + storage_name = resource.model.storage_name + + engine.delete storage_name, resource.id end.size end def update(attributes, resources) resources.each do |resource| - params = attributes_as_fields(attributes).except("createdAt", "updatedAt") - parse_resource_for(resource).put(params: params) + params = attributes_as_fields(attributes).except("createdAt", "updatedAt") + storage_name = resource.model.storage_name + + engine.update storage_name, resource.id, params end.size end private - def build_parse_resource_for(name) - Parse::Resource.new(HOST, format: :json, headers: key_headers)[VERSION][name] - end - - def key_headers - key_type = @options[:master] ? MASTER_KEY_HEADER : API_KEY_HEADER - { - APP_ID_HEADER => @options[:app_id], - key_type => @options[:api_key] - } - end - def parse_params_for(query) result = { :limit => parse_limit_for(query) } if conditions = parse_conditions_for(query) result[:where] = conditions.to_json end @@ -195,13 +179,24 @@ parse_query.build end def feed_for(parse_query, condition, comparison_class) - field = condition.subject.field - comparison = comparison_class.new condition.value - parse_query.add field, comparison + subject = condition.subject + case subject + when DataMapper::Property + comparison = comparison_class.new condition.value + parse_query.add subject.field, comparison + when DataMapper::Associations::OneToMany::Relationship + child_key = condition.subject.child_key.first + parse_query.add "objectId", comparison_class.new(condition.value.map { |resource| resource.send child_key.name }) + when DataMapper::Associations::ManyToOne::Relationship + child_key = subject.child_key.first + parse_query.add child_key.field, comparison_class.new(condition.foreign_key_mapping.value) + else + raise NotImplementedError, "Condition: #{condition}" + end end def feed_reversely(parse_query, conditions) conditions.each do |condition| case condition @@ -210,11 +205,11 @@ when GreaterThanOrEqualToComparison then feed_for(parse_query, condition, Lt) when LessThanComparison then feed_for(parse_query, condition, Gte) when LessThanOrEqualToComparison then feed_for(parse_query, condition, Gt) when NotOperation then feed_directly(parse_query, condition) when AndOperation then feed_reversely(parse_query, condition) - when InclusionComparison then feed_inclusion(parse_query, condition, Nin) + when InclusionComparison then feed_for(parse_query, condition, Nin) else raise NotImplementedError end end end @@ -239,23 +234,14 @@ when EqualToComparison then feed_for(parse_query, condition, Eql) when GreaterThanComparison then feed_for(parse_query, condition, Gt) when GreaterThanOrEqualToComparison then feed_for(parse_query, condition, Gte) when LessThanComparison then feed_for(parse_query, condition, Lt) when LessThanOrEqualToComparison then feed_for(parse_query, condition, Lte) + when InclusionComparison then feed_for(parse_query, condition, In) when NotOperation then feed_reversely(parse_query, condition) when AndOperation then feed_directly(parse_query, condition) - when InclusionComparison then feed_inclusion(parse_query, condition, In) else raise NotImplementedError - end - end - - def feed_inclusion(parse_query, condition, comparison_class) - if condition.subject.is_a?(DataMapper::Associations::OneToMany::Relationship) - child_key = condition.subject.child_key.first.name - parse_query.add "objectId", comparison_class.new(condition.value.map { |resource| resource.send child_key }) - else - feed_for(parse_query, condition, comparison_class) end end end