Module: Glide

Defined in:
lib/glide.rb,
lib/glide/version.rb

Constant Summary collapse

GLIDE_HUMAN_NAME =
{"elec" => "Electricity", "tv_license" => "TV License"}
VERSION =
"0.0.5"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.api_keyObject

Returns the value of attribute api_key.



12
13
14
# File 'lib/glide.rb', line 12

def api_key
  @api_key
end

Class Method Details

.calculate_totals(quotes) ⇒ Object



36
37
38
39
40
41
42
# File 'lib/glide.rb', line 36

def calculate_totals(quotes)
	quotes["total"] = {}
	["tenant_week", "tenant_month", "monthly_fee"].each do |k|
		quotes["total"][k] = "%.2f" % quotes.map { |e| e[1][k].to_f }.reduce(:+)
	end
	quotes		
end

.get_quote(services = ['elec'], extra = {}, period = 6, tenants = 1) ⇒ Object



14
15
16
17
18
19
20
21
# File 'lib/glide.rb', line 14

def get_quote(services = ['elec'], extra = {}, period = 6, tenants = 1)
	quotes = {}
	services.each do |service|
		quotes[service] = get_service_quote(service, extra[service], period, tenants)
		quotes[service]["human_name"] = insert_human_name(service)
	end
	calculate_totals(quotes)
end

.get_service_quote(service, extra, period, tenants) ⇒ Object



23
24
25
26
27
28
29
30
# File 'lib/glide.rb', line 23

def get_service_quote(service, extra, period, tenants)
	res = Timeout::timeout(5) { HTTParty.get("https://api.glide.uk.com/signup/servicePrice.json", :query => {:service => service, :period => period, :extra => extra, :tenants => tenants, :key => api_key}) }
	JSON.parse(res.body)["results"]
rescue Timeout::Error => e
	{"message" => "A timeout error has occured", "error" => 2}
rescue
	{"message" => "An error has occured", "error" => 3}
end

.insert_human_name(service) ⇒ Object



32
33
34
# File 'lib/glide.rb', line 32

def insert_human_name(service)
	GLIDE_HUMAN_NAME[service] || service.capitalize
end