<commit_before>class PadletWrapper

  DefaultHost  = 'padlet.com'
  DefaultEmail = '<EMAIL>'
  WallPath     = 'walls'
  AuthPath     = 'session'
  PolicyPath   = 'privacy_policies'
  PadletPublicPolicy = 4

  attr_accessor :password
  attr_accessor :username
  attr_accessor :padlet_url
  attr_accessor :padlet_user_id
  attr_accessor :auth_data
  attr_accessor :auth_cookies
  attr_accessor :policy_id
  attr_accessor :is_public
  attr_accessor :is_moderated

  yaml_file_path = File.join(Rails.root,'config','padlet.yml')
  Opts = {
    :host            => DefaultHost,
    :email           => DefaultEmail
  }

  begin
    yaml_config = YAML.load_file(yaml_file_path).symbolize_keys
    Opts.merge!(yaml_config)
  rescue Exception => e
    puts "Error: #{e} loading yaml: #{yaml_file_path}"
    puts "Using defaults (see padlet_wrapper.rb)"
  end

  def self.hostname
    Opts[:host]
  end

  def self.basic_auth_user
    Opts[:basic_auth_user]
  end

  def self.basic_auth_pass
    Opts[:basic_auth_pass]
  end

  def self.email
    Opts[:email]
  end

  def self.pass
    self.email.reverse
  end

  def self.make_bookmark(user=nil,pass=nil)
    user ||= self.email
    pass ||= self.pass
    instance = self.new(user,pass)
    instance.get_auth_token
    instance.make_wall
    return instance
  end

  def initialize(_user,_pass)
    self.username = _user
    self.password = _pass
  end

  def headers(opts)
    headers  = {'Content-Type' => 'application/json' }
    unless (self.auth_cookies.blank?)
      headers['cookie'] = self.auth_cookies.join(";")
    end
    opts[:headers] = headers
    opts
  end

  def get_opts(data)
    opts = {}
    headers(opts)
    auth_headers(opts)
    opts[:body] = data.to_json
    opts
  end

  def get_auth_token
    results = self.json_post(AuthPath, self.auth_request)
    cookies = results.get_fields('Set-Cookie');
    cookies.map! { |c|
      name, remainder = c.split("=",2)
      value = remainder.split(";")[0]
      "#{name}=#{value}"
    }
    results = JSON.parse(results)
    self.padlet_user_id = results['id']
    self.auth_data = results.dup
    self.auth_cookies   = cookies
    self
  end

  def make_wall
    results = self.json_post(WallPath,self.auth_data)
    self.padlet_url=results['links']['doodle'] # also checkout 'embed'
    self.fix_hostname_in_response_url
    # Noah removed because IE can't deal with basic auth urls:
    # http://support.microsoft.com/kb/834489
    # self.add_auth_info_to_url
    self
  end

  protected
  def auth_headers(opts)
    user =PadletWrapper.basic_auth_user
    pass =PadletWrapper.basic_auth_pass
    if (user && pass)<commit_msg>PadletWrapper :  JSON results are in the response body.<commit_after>class PadletWrapper

  DefaultHost  = 'padlet.com'
  DefaultEmail = '<EMAIL>'
  WallPath     = 'walls'
  AuthPath     = 'session'
  PolicyPath   = 'privacy_policies'
  PadletPublicPolicy = 4

  attr_accessor :password
  attr_accessor :username
  attr_accessor :padlet_url
  attr_accessor :padlet_user_id
  attr_accessor :auth_data
  attr_accessor :auth_cookies
  attr_accessor :policy_id
  attr_accessor :is_public
  attr_accessor :is_moderated

  yaml_file_path = File.join(Rails.root,'config','padlet.yml')
  Opts = {
    :host            => DefaultHost,
    :email           => DefaultEmail
  }

  begin
    yaml_config = YAML.load_file(yaml_file_path).symbolize_keys
    Opts.merge!(yaml_config)
  rescue Exception => e
    puts "Error: #{e} loading yaml: #{yaml_file_path}"
    puts "Using defaults (see padlet_wrapper.rb)"
  end

  def self.hostname
    Opts[:host]
  end

  def self.basic_auth_user
    Opts[:basic_auth_user]
  end

  def self.basic_auth_pass
    Opts[:basic_auth_pass]
  end

  def self.email
    Opts[:email]
  end

  def self.pass
    self.email.reverse
  end

  def self.make_bookmark(user=nil,pass=nil)
    user ||= self.email
    pass ||= self.pass
    instance = self.new(user,pass)
    instance.get_auth_token
    instance.make_wall
    return instance
  end

  def initialize(_user,_pass)
    self.username = _user
    self.password = _<PASSWORD>
  end

  def headers(opts)
    headers  = {'Content-Type' => 'application/json' }
    unless (self.auth_cookies.blank?)
      headers['cookie'] = self.auth_cookies.join(";")
    end
    opts[:headers] = headers
    opts
  end

  def get_opts(data)
    opts = {}
    headers(opts)
    auth_headers(opts)
    opts[:body] = data.to_json
    opts
  end

  def get_auth_token
    results = self.json_post(AuthPath, self.auth_request)
    cookies = results.get_fields('Set-Cookie');
    cookies.map! { |c|
      name, remainder = c.split("=",2)
      value = remainder.split(";")[0]
      "#{name}=#{value}"
    }
    results = JSON.parse(results.body)
    self.padlet_user_id = results['id']
    self.auth_data      = results.dup
    self.auth_cookies   = cookies
    self
  end

  def make_wall
    results = self.json_post(WallPath,self.auth_data)
    results = JSON.parse(results.body)
    self.padlet_url=results['links']['doodle'] # also checkout 'embed'
    self.fix_hostname_in_response_url
    # Noah removed because IE can't deal with basic auth urls:
    # http://support.microsoft.com/kb/834489
    # self.add_auth_info_to_url
    self
  end

  protected
  def auth_headers(opts)
    user =PadletWrapper.basic_auth_user
    pass =PadletWrapper.basic_auth_pass
    if (user && pass)
{'fraction_non_alphanumeric': 0.10567441860465117, 'fraction_numerical': 0.0033488372093023254, 'mean_word_length': 3.1609907120743035, 'pattern_counts': {'":': 0, '<': 6, '<?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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1266830', 'n_tokens_mistral': 1904, 'n_tokens_neox': 1862, 'n_words': 366}