Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- require 'nokogiri'
- class BigFiveResultsTextSerializer
- # big5 = BigFiveResultsTextSerializer.new('personality_test.html').to_h
- # post = BigFiveResultsPoster.new(big5, 'test+1114@gmail.com').post
- # post.token = UPnemRUJEaXxhdKuwMzxMSJe
- # post.results_hash = UPnemRUJEaXxhdKuwMzxMSJe
- def initialize(text)
- @file = Nokogiri::HTML(File.read(text))
- @hash = {}
- end
- def to_h
- add_name_to_hash
- # contains all domain, facets and their score in a rough format
- extracted_domains_and_facet = @file.css('.graph-txt')
- extracted_domains_and_facet.each do |domain_and_facets|
- # clean up the data by removing the information not required
- clean_domain_and_facts = clean_up_domain_and_facts(domain_and_facets)
- # prepare the hash in a format that is required
- prepare_domain_and_facts(clean_domain_and_facts)
- end
- @hash
- end
- private
- def add_name_to_hash
- @hash.merge!({'NAME' => 'Shairyar Baig'})
- end
- def clean_up_domain_and_facts(result)
- # get the personality node that we are interested in
- domain_and_facets = result.css('code').children.children
- # drop the first index as we dont need it
- domain_and_facets.shift
- domain_and_facets
- end
- def prepare_domain_and_facts(results)
- domain = prepare_domain(results)
- prepare_facets(domain, results)
- end
- def prepare_domain(results)
- domain = extract_domain_and_score(results[0])
- @hash.merge!(domain.first => {'Overall Score' => domain.last.to_i, 'Facets' => {}})
- domain
- end
- def prepare_facets(domain, results)
- results.each_with_index do |result, index|
- # first index is the domain and we only want to prepare the facets
- if index != 0
- extract_domain_and_score = extract_domain_and_score(result)
- @hash[domain.first]['Facets'].merge!({"#{extract_domain_and_score.first}" => extract_domain_and_score.last.to_i})
- end
- end
- end
- def extract_domain_and_score(result)
- # removes the special characters
- # and then converts the result into an array of domain/facet and score ["EXTRAVERSION", "43"]
- result.text.gsub('.', '|').split('|')
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement