Created
September 29, 2016 18:32
-
-
Save abhinaykumar/e029542b6af78b1b92e9242256dcf99f to your computer and use it in GitHub Desktop.
This program is the part of a coding challenge where we need to calculate the total price of a Model of a car based on their policy and base price.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Calculate the Total Price of a Model of Car based on the policy(which will | |
# decide margin) and based price provided by User. Every policy has a condition | |
# to calculate the margin which will then gets added to base price to get the | |
# total price of the car. | |
class Models | |
require 'open-uri' | |
require 'nokogiri' | |
def self.calculate_total_price(pricing_policy, base_price) | |
case pricing_policy | |
when 'Flexible' | |
total_price = flexible_policy(base_price) | |
when 'Fixed' | |
total_price = fixed_policy(base_price) | |
when 'Prestige' | |
total_price = prestige_policy(base_price) | |
else | |
total_price = 'Not a Valid input' | |
end | |
total_price | |
end | |
# Margin value would be total number of 'a' characters present in `reuters` website. | |
def self.flexible_policy(base_price) | |
reuters_doc = Nokogiri::HTML(open('http://reuters.com')).to_s | |
i = reuters_doc.count "\a" | |
j = reuters_doc.count 'a' | |
margin = (i + j).to_f / 100 | |
puts "Margin is: #{margin}" | |
base_price * margin | |
end | |
# Margin value would be number of times 'status' word present on `developer.github.com/v3` | |
def self.fixed_policy(base_price) | |
github_doc = Nokogiri::HTML(open('https://developer.github.com/v3/#http-redirects')).to_s | |
margin = github_doc.scan(/[^a-zA-Z]status[^a-zA-Z]/i).count | |
puts "Margin is: #{margin}" | |
base_price + margin | |
end | |
# Margin value would be total number of times 'pubDate' used on 'yourlocalguardian' website. | |
def self.prestige_policy(base_price) | |
local_doc = Nokogiri::HTML(open('http://www.yourlocalguardian.co.uk/sport/rugby/rss/')) | |
pub_date_elements = local_doc.search('pubDate') | |
margin = pub_date_elements.length | |
puts "Margin is: #{margin}" | |
base_price + margin | |
end | |
end | |
puts 'Press 1 for Flexible policy \n Press 2 for Fixed Policy \n Press 3 for Prestige Policy' | |
policy = gets.chomp.to_i | |
policy = 'Flexible' if policy == 1 | |
policy = 'Fixed' if policy == 2 | |
policy = 'Prestige' if policy == 3 | |
puts 'Base price for the Car Model:' | |
base_price = gets.chomp.to_i | |
total_price = Models.calculate_total_price(policy, base_price) | |
puts "Total Price for the Model is: #{total_price}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment