Class: ExternalApi

Inherits:
Object
  • Object
show all
Defined in:
lib/calculate-tip/external-api.rb

Instance Method Summary collapse

Constructor Details

#initialize(amount = nil, tip = "15") ⇒ ExternalApi

Returns a new instance of ExternalApi.



7
8
9
10
11
# File 'lib/calculate-tip/external-api.rb', line 7

def initialize(amount = nil, tip = "15")
  @amount = amount.to_f
  @tip = tip.to_f
  @base_url = "http://office.code-runners.com:8888"
end

Instance Method Details

#calculateObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/calculate-tip/external-api.rb', line 21

def calculate
  uri = URI.parse(@base_url)
  request = Net::HTTP::Post.new(uri)
  
  request.set_form_data(
    "amount" => @amount.to_s,
    "tip" => @tip.to_s,
  )

  req_options = {
    use_ssl: uri.scheme == "https",
  }

  response = Net::HTTP.start(uri.hostname, uri.port, req_options) do |http|
    http.request(request)
  end

  return response.body
end

#get_amountObject



13
14
15
# File 'lib/calculate-tip/external-api.rb', line 13

def get_amount
  @amount
end

#get_tipObject



17
18
19
# File 'lib/calculate-tip/external-api.rb', line 17

def get_tip
  @tip
end