Class: YS1::Http

Inherits:
Object
  • Object
show all
Defined in:
lib/ys1/http.rb

Overview

The Http class handles HTTP GET requests and processes responses.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uri) ⇒ Http

Initializes the Http client with a given URI.

Parameters:

  • uri (String)

    The URI for the HTTP request.



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

#httpObject (readonly)

Returns the value of attribute http.



10
11
12
# File 'lib/ys1/http.rb', line 10

def http
  @http
end

#responseObject (readonly)

Returns the value of attribute response.



10
11
12
# File 'lib/ys1/http.rb', line 10

def response
  @response
end

#urlObject (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_dataObject

Parses the JSON response body into a Ruby data structure.

Returns:

  • (Object)

    The parsed JSON data.



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.

Returns:

  • (Boolean)

    True if response code is 2xx, false otherwise.



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

#requestHttp

Sends an HTTP GET request to the specified URI.

Returns:

  • (Http)

    Returns the instance with response and body populated.



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