<commit_before>module Infrared
  class API

    include Infrared::Defaults
    include Infrared::Development #add into module elsewhere based ENV

    # class BadRequest < Faraday::Error; end

    attr_accessor :connection, :cookies, :headers, :session

    def initialize(url=nil,email=nil, password=<PASSWORD>)
      @connection = conn(url)
      @headers = {}
      login(email,password)
    end

    private 
    # Exceptions
    # URI::BadURIError
    def conn(url)
      @connection = Faraday.new(:url => url) do |faraday|
        faraday.request  :url_encoded             # form-encode POST params
        faraday.response :logger                  # log requests to STDOUT
        faraday.adapter  Faraday.default_adapter  # make requests with Net::HTTP
      end
    end

    # Exceptions
    # URI::Generic:0x007fe3b09dd578> (NoMethodError) 
    def login(email,password)
      first_load = connection.get(endpoint(:signin))
      set_headers(first_load) 
      body = {email: email, password: password}
      response = connection.post do |req|
        req.url endpoint(:signin)
        req.headers  = headers
        # req.params = {email: email, password: password}
        req.body = body
      end
      binding.pry
    end

    def csrf_token_from_body(body)
      # raises some exceptions here for parsing
      t = ::Nokogiri::HTML(body).xpath('//meta[@name="csrf-param"]/@content')
      {"X-CSRF-Token" => ((t && t.first)? t.first.value : '')}
    end

    def set_headers(response)
      @headers.merge!(default_headers)
      @headers.merge!(csrf_token_from_body(response.body))
      @cookies = CGI::Cookie.parse(response.headers['set-cookie'])
      headers.merge!({"Cookie" => "connect.sid=#{@cookies['connect.sid'].first}"})
      # headers.merge!({"Cookie" => response.headers['set-cookie']})
    end

  end
end<commit_msg>refactored a little<commit_after>module Infrared
  class API

    include Infrared::Defaults
    include Infrared::Development #add into module elsewhere based ENV

    # class BadRequest < Faraday::Error; end

    attr_accessor :connection, :cookies, :headers, :session

    def initialize(url=nil,email=nil, password=<PASSWORD>)
      @connection = conn(url)
      @headers = {}
      login(email,password)
    end

    private 
    # Exceptions
    # URI::BadURIError
    def conn(url)
      @connection = Faraday.new(:url => url) do |faraday|
        faraday.request  :url_encoded             # form-encode POST params
        faraday.response :logger                  # log requests to STDOUT
        faraday.adapter  Faraday.default_adapter  # make requests with Net::HTTP
      end
    end

    # Exceptions
    # URI::Generic:0x007fe3b09dd578> (NoMethodError) 
    def login(email,password)
      first_load = connection.get(endpoint(:signin))
      set_headers(first_load) 
      set_session(first_load)  
      body = {email: email, password: password}
      response = connection.post do |req|
        req.url endpoint(:signin)
        req.headers  = headers
        # req.params = {email: email, password: password}
        req.body = body
      end
      binding.pry
    end

    def csrf_token_from_body(body)
      # raises some exceptions here for parsing
      t = ::Nokogiri::HTML(body).xpath('//meta[@name="csrf-param"]/@content')
      {"X-CSRF-Token" => ((t && t.first)? t.first.value : '')}
    end

    def set_headers(response)
      headers.merge!(default_headers)
      headers.merge!(set_session(response))
      headers.merge!(csrf_token_from_body(response.body))
    end

    def set_session(response)
      cookies = CGI::Cookie.parse(response.headers['set-cookie'])
      cookies = ({"Cookie" => "connect.sid=#{cookies['connect.sid'].first}"})
    end

  end
end
{'fraction_non_alphanumeric': 0.12025654730090861, 'fraction_numerical': 0.005344735435595938, 'mean_word_length': 2.8193877551020408, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 14, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7015538', 'n_tokens_mistral': 1191, 'n_tokens_neox': 1129, 'n_words': 243}