Class: YS1::Http
- Inherits:
-
Object
- Object
- YS1::Http
- Defined in:
- lib/ys1/http.rb
Overview
The Http class handles HTTP GET requests and processes responses.
Instance Attribute Summary collapse
-
#http ⇒ Object
readonly
Returns the value of attribute http.
-
#response ⇒ Object
readonly
Returns the value of attribute response.
-
#url ⇒ Object
readonly
Returns the value of attribute url.
Instance Method Summary collapse
-
#initialize(uri) ⇒ Http
constructor
Initializes the Http client with a given URI.
-
#json_to_data ⇒ Object
Parses the JSON response body into a Ruby data structure.
-
#rc2xx? ⇒ Boolean
Checks if the HTTP response code is in the 2xx range.
-
#request ⇒ Http
Sends an HTTP GET request to the specified URI.
Constructor Details
#initialize(uri) ⇒ Http
Initializes the Http client with a given URI.
14 15 16 17 18 |
# File 'lib/ys1/http.rb', line 14 def initialize(uri) @url = URI.parse(uri) @http = Net::HTTP.new(url.host, url.port) @http.use_ssl = (url.scheme == "https") end |
Instance Attribute Details
#http ⇒ Object (readonly)
Returns the value of attribute http.
10 11 12 |
# File 'lib/ys1/http.rb', line 10 def http @http end |
#response ⇒ Object (readonly)
Returns the value of attribute response.
10 11 12 |
# File 'lib/ys1/http.rb', line 10 def response @response end |
#url ⇒ Object (readonly)
Returns the value of attribute url.
10 11 12 |
# File 'lib/ys1/http.rb', line 10 def url @url end |
Instance Method Details
#json_to_data ⇒ Object
Parses the JSON response body into a Ruby data structure.
37 38 39 40 |
# File 'lib/ys1/http.rb', line 37 def json_to_data request JSON.parse(@response.body) end |
#rc2xx? ⇒ Boolean
Checks if the HTTP response code is in the 2xx range.
30 31 32 33 |
# File 'lib/ys1/http.rb', line 30 def rc2xx? response_code = @response.code.to_i response_code.between?(200, 299) end |
#request ⇒ Http
Sends an HTTP GET request to the specified URI.
22 23 24 25 26 |
# File 'lib/ys1/http.rb', line 22 def request request = Net::HTTP::Get.new(@url) @response = @http.request(request) self end |