<commit_before>module DataMapperRest
  # TODO: Abstract XML support out from the protocol
  # TODO: Build JSON support

  # All http_"verb" (http_post) method calls use method missing in connection class which uses run_verb
  class Adapter < DataMapper::Adapters::AbstractAdapter
    include Extlib
    
    def connection
      @connection ||= Connection.new(@uri)
    end

    # Creates a new resource in the specified repository.
    # TODO: rescue from errors
    def create(resources)
      created = 0
      resources.each do |resource|
        response = connection.http_post(resource_name(resource), resource.to_xml)
        data = response.body
        
        id_field = resource.class.key(resource.repository.name).find {|p| p.serial?}
        
        id_field.set!(resource, normalized_id(data, resource.class)) if id_field
        created += 1  
      end
      
      created
    end

    # read_set
    #
    # Examples of query string:
    # A. []
    #    GET /books/
    #
    # B. [[:eql, #<Property:Book:id>, 4200]]
    #    GET /books/4200
    #
    # IN PROGRESS
    # TODO: Need to account for query.conditions (i.e., [[:eql, #<Property:Book:id>, 1]] for books/1)
    def read_many(query)
      resource_name = Inflection.underscore(query.model.name)
      ::DataMapper::Collection.new(query) do |collection|
        case query.conditions
        when []
          resources_meta = read_set_all(repository, query, resource_name)
        else
          resources_meta = read_set_for_condition(repository, query, resource_name)
        end
        resources_meta.each do |resource_meta|
          if resource_meta.has_key?(:associations)
            load_nested_resources_from resource_meta[:associations], query
          end
          collection.load(resource_meta[:values])
        end
      end
    end

    def read_one(query)
      resource = nil
      resource_name = resource_name_from_query(query)
      resources_meta = nil
      if query.conditions.empty? && query.limit == 1
        results = read_set_all(repository, query, resource_name)
        resource_meta = results.first unless results.empty?
      else
        id = query.conditions.first[2]
        # KLUGE: Again, we're assuming below that we're dealing with a pluralized resource mapping

        response = connection.http_get("/#{resource_name.pluralize}/#{id}.xml")

        # KLUGE: Rails returns HTML if it can't find a resource.  A properly RESTful app would return a 404, right?
        return nil if response.is_a? Net::HTTPNotFound || response.content_type == "text/html"

        data = response.body
        resource_meta = parse_resource(data, query.model, query)
      end
      if resource_meta
        if resource_meta.has_key?(:associations)
          load_nested_resources_from resource_meta[:associations], query
        end
        resource = query.model.load(resource_meta[:values], query)
      end
      resource
    end

    def update(attributes, query)
      # TODO What if we have a compound key?
      raise NotImplementedError.new unless is_single_resource_query? query
      id = query.conditions.first[2]
      resource = nil
      query.repository.scope do
        resource = query.model.get(id)
      end
      attributes.each do |attr, val|
        resource.send("#{attr.name}=", val)
      end
      # KLUGE: Again, we're assuming below that we're dealing with a pluralized resource mapping
      res = connection.http_put("/#{resource_name_from_query(query).pluralize}/#{id}.xml", resource.to_xml)
      # TODO: Raise error if cannot reach server
      res.kind_of?(Net::HTTPSuccess) ? 1 : 0<commit_msg>[dm-rest-adapter] The new exception code properly throws a 404 error<commit_after>module DataMapperRest
  # TODO: Abstract XML support out from the protocol
  # TODO: Build JSON support

  # All http_"verb" (http_post) method calls use method missing in connection class which uses run_verb
  class Adapter < DataMapper::Adapters::AbstractAdapter
    include Extlib
    
    def connection
      @connection ||= Connection.new(@uri)
    end

    # Creates a new resource in the specified repository.
    # TODO: rescue from errors
    def create(resources)
      created = 0
      resources.each do |resource|
        response = connection.http_post(resource_name(resource), resource.to_xml)
        data = response.body
        
        id_field = resource.class.key(resource.repository.name).find {|p| p.serial?}
        
        id_field.set!(resource, normalized_id(data, resource.class)) if id_field
        created += 1  
      end
      
      created
    end

    # read_set
    #
    # Examples of query string:
    # A. []
    #    GET /books/
    #
    # B. [[:eql, #<Property:Book:id>, 4200]]
    #    GET /books/4200
    #
    # IN PROGRESS
    # TODO: Need to account for query.conditions (i.e., [[:eql, #<Property:Book:id>, 1]] for books/1)
    def read_many(query)
      resource_name = Inflection.underscore(query.model.name)
      ::DataMapper::Collection.new(query) do |collection|
        case query.conditions
        when []
          resources_meta = read_set_all(repository, query, resource_name)
        else
          resources_meta = read_set_for_condition(repository, query, resource_name)
        end
        resources_meta.each do |resource_meta|
          if resource_meta.has_key?(:associations)
            load_nested_resources_from resource_meta[:associations], query
          end
          collection.load(resource_meta[:values])
        end
      end
    end

    def read_one(query)
      resource = nil
      resource_name = resource_name_from_query(query)
      resources_meta = nil
      if query.conditions.empty? && query.limit == 1
        results = read_set_all(repository, query, resource_name)
        resource_meta = results.first unless results.empty?
      else
        id = query.conditions.first[2]
        # KLUGE: Again, we're assuming below that we're dealing with a pluralized resource mapping

        response = connection.http_get("/#{resource_name.pluralize}/#{id}.xml")

        data = response.body
        resource_meta = parse_resource(data, query.model, query)
      end
      if resource_meta
        if resource_meta.has_key?(:associations)
          load_nested_resources_from resource_meta[:associations], query
        end
        resource = query.model.load(resource_meta[:values], query)
      end
      resource
    end

    def update(attributes, query)
      # TODO What if we have a compound key?
      raise NotImplementedError.new unless is_single_resource_query? query
      id = query.conditions.first[2]
      resource = nil
      query.repository.scope do
        resource = query.model.get(id)
      end
      attributes.each do |attr, val|
        resource.send("#{attr.name}=", val)
      end
      # KLUGE: Again, we're assuming below that we're dealing with a pluralized resource mapping
      res = connection.http_put("/#{resource_name_from_query(query).pluralize}/#{id}.xml", resource.to_xml)
      # TODO: Raise error if cannot reach server
      res.kind_of?(Net::HTTPSuccess) ? 1 : 0
{'fraction_non_alphanumeric': 0.09197018703417241, 'fraction_numerical': 0.005625087891998313, 'mean_word_length': 2.645310097385956, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 8}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6807706', 'n_tokens_mistral': 2061, 'n_tokens_neox': 2010, 'n_words': 544}