Class: ExternalApi
- Inherits:
-
Object
- Object
- ExternalApi
- Defined in:
- lib/calculate-tip/external-api.rb
Instance Method Summary collapse
- #calculate ⇒ Object
- #get_amount ⇒ Object
- #get_tip ⇒ Object
-
#initialize(amount = nil, tip = "15") ⇒ ExternalApi
constructor
A new instance of ExternalApi.
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
#calculate ⇒ Object
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, ) = { use_ssl: uri.scheme == "https", } response = Net::HTTP.start(uri.hostname, uri.port, ) do |http| http.request(request) end return response.body end |
#get_amount ⇒ Object
13 14 15 |
# File 'lib/calculate-tip/external-api.rb', line 13 def get_amount @amount end |
#get_tip ⇒ Object
17 18 19 |
# File 'lib/calculate-tip/external-api.rb', line 17 def get_tip @tip end |