<commit_before>require File.expand_path('../utils', __FILE__)
require File.expand_path('../exception', __FILE__)
require 'pry'
module Elmas
  module Resource
    STANDARD_FILTERS = [:id].freeze

    attr_accessor :attributes
    attr_accessor :id
    attr_accessor :url
    attr_accessor :response

    def initialize(attributes = {})
      @attributes = Utils.normalize_hash(attributes)
      @filters = STANDARD_FILTERS
    end

    def id
      (attributes[:id] ||= nil)
    end

    def id=(value)
      attributes[:id] = value
    end

    def base_path
      Utils.collection_path self.class.name
    end

    def url
      apply_filters(base_path)
    end

    def find_all
      find(base_path)
    end

    # Normally use the url method (which applies the filters) but sometimes you only want to use the base path or other paths
    def find(url = self.url)
      begin
        @response = Elmas.get(url)
      rescue
        puts "I dunno what went wrong"
      end
    end

    def valid?
      valid = true
      mandatory_attributes.each do |attribute|
        valid = @attributes.has_key? attribute
      end
      valid
    end

    # Pass filters in an array, for example [:id, :name]
    def find_by(filters)
      @filters = filters
      find
    end

    def save
      attributes_to_submit = self.sanitize
      begin
        @response = Elmas.post(base_path, attributes_to_submit)
      rescue
        puts "Horrible outcome, nothing works"
      end
    end

    # Parse the attributes for to post to the API
    def sanitize
      to_submit = {}
      @attributes.each do |key, value|
        if value.is_a? Elmas::Resource #Turn relation into ID
          to_submit["#{key}_#{id}"] = value.id
        else
          to_submit[key] = value
        end
      end
      to_submit
    end

    def response
      @response
    end

    #?$filter=ID eq guid'#{id}'
    def id_filter(sign)
      "#{sign}$filter=ID eq guid'#{id}'"
    end
<commit_msg>Do not save if not valid<commit_after>require File.expand_path('../utils', __FILE__)
require File.expand_path('../exception', __FILE__)
require 'pry'
module Elmas
  module Resource
    STANDARD_FILTERS = [:id].freeze

    attr_accessor :attributes
    attr_accessor :id
    attr_accessor :url
    attr_accessor :response

    def initialize(attributes = {})
      @attributes = Utils.normalize_hash(attributes)
      @filters = STANDARD_FILTERS
    end

    def id
      (attributes[:id] ||= nil)
    end

    def id=(value)
      attributes[:id] = value
    end

    def base_path
      Utils.collection_path self.class.name
    end

    def url
      apply_filters(base_path)
    end

    def find_all
      find(base_path)
    end

    # Normally use the url method (which applies the filters) but sometimes you only want to use the base path or other paths
    def find(url = self.url)
      begin
        @response = Elmas.get(url)
      rescue
        puts "I dunno what went wrong"
      end
    end

    def valid?
      valid = true
      mandatory_attributes.each do |attribute|
        valid = @attributes.has_key? attribute
      end
      valid
    end

    # Pass filters in an array, for example [:id, :name]
    def find_by(filters)
      @filters = filters
      find
    end

    def save
      attributes_to_submit = self.sanitize
      if self.valid?
        begin
          @response = Elmas.post(base_path, attributes_to_submit)
        rescue
          puts "Horrible outcome, nothing works"
        end
      else
        puts "Resource is not valid, you should add some more attributes"
      end
    end

    # Parse the attributes for to post to the API
    def sanitize
      to_submit = {}
      @attributes.each do |key, value|
        if value.is_a? Elmas::Resource #Turn relation into ID
          to_submit["#{key}_#{id}"] = value.id
        else
          to_submit[key] = value
        end
      end
      to_submit
    end

    def response
      @response
    end

    #?$filter=ID eq guid'#{id}'
    def id_filter(sign)
      "#{sign}$filter=ID eq guid'#{id}'"
    end

{'fraction_non_alphanumeric': 0.08167770419426049, 'fraction_numerical': 0.0, 'mean_word_length': 2.156346749226006, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, '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': '196814', 'n_tokens_mistral': 1245, 'n_tokens_neox': 1235, 'n_words': 376}