SHARE
TWEET

Untitled

a guest Apr 22nd, 2019 90 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. require 'nokogiri'
  2.  
  3. class BigFiveResultsTextSerializer
  4.  
  5.   # big5 = BigFiveResultsTextSerializer.new('personality_test.html').to_h
  6.   # post = BigFiveResultsPoster.new(big5, 'test+1114@gmail.com').post
  7.   # post.token = UPnemRUJEaXxhdKuwMzxMSJe
  8.   # post.results_hash = UPnemRUJEaXxhdKuwMzxMSJe
  9.  
  10.   def initialize(text)
  11.  
  12.     @file = Nokogiri::HTML(File.read(text))
  13.     @hash = {}
  14.  
  15.   end
  16.  
  17.   def to_h
  18.    
  19.     add_name_to_hash
  20.  
  21.     # contains all domain, facets and their score in a rough format
  22.     extracted_domains_and_facet = @file.css('.graph-txt')
  23.  
  24.     extracted_domains_and_facet.each do |domain_and_facets|
  25.  
  26.       # clean up the data by removing the information not required
  27.       clean_domain_and_facts = clean_up_domain_and_facts(domain_and_facets)
  28.  
  29.       # prepare the hash in a format that is required
  30.       prepare_domain_and_facts(clean_domain_and_facts)
  31.     end
  32.  
  33.     @hash
  34.   end
  35.  
  36.  
  37.   private
  38.  
  39.   def add_name_to_hash
  40.     @hash.merge!({'NAME' => 'Shairyar Baig'})
  41.   end
  42.  
  43.   def clean_up_domain_and_facts(result)
  44.     # get the personality node that we are interested in
  45.     domain_and_facets = result.css('code').children.children
  46.  
  47.     # drop the first index as we dont need it
  48.     domain_and_facets.shift
  49.     domain_and_facets
  50.   end
  51.  
  52.   def prepare_domain_and_facts(results)
  53.  
  54.     domain = prepare_domain(results)
  55.     prepare_facets(domain, results)
  56.  
  57.   end
  58.  
  59.   def prepare_domain(results)
  60.     domain = extract_domain_and_score(results[0])
  61.     @hash.merge!(domain.first => {'Overall Score' => domain.last.to_i, 'Facets' => {}})
  62.  
  63.     domain
  64.   end
  65.  
  66.   def prepare_facets(domain, results)
  67.  
  68.     results.each_with_index do |result, index|
  69.  
  70.       # first index is the domain and we only want to prepare the facets
  71.       if index != 0
  72.         extract_domain_and_score = extract_domain_and_score(result)
  73.         @hash[domain.first]['Facets'].merge!({"#{extract_domain_and_score.first}" => extract_domain_and_score.last.to_i})
  74.       end
  75.     end
  76.  
  77.   end
  78.  
  79.   def extract_domain_and_score(result)
  80.     # removes the special characters
  81.     # and then converts the result into an array of domain/facet and score ["EXTRAVERSION", "43"]
  82.     result.text.gsub('.', '|').split('|')
  83.   end
  84.  
  85. end
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top