Class: Hibp::Request
- Inherits:
-
Object
- Object
- Hibp::Request
- Defined in:
- lib/hibp/request.rb
Overview
Hibp::Request
Used to make requests to the hibp API
@see https://haveibeenpwned.com/API/v3
Instance Attribute Summary collapse
-
#headers ⇒ Object
readonly
Returns the value of attribute headers.
Instance Method Summary collapse
-
#get(params: nil, headers: nil) ⇒ Object
Perform a GET request.
-
#initialize(endpoint:, parser: Parsers::Json.new) ⇒ Request
constructor
A new instance of Request.
Constructor Details
#initialize(endpoint:, parser: Parsers::Json.new) ⇒ Request
Returns a new instance of Request.
19 20 21 22 23 24 25 26 27 |
# File 'lib/hibp/request.rb', line 19 def initialize(endpoint:, parser: Parsers::Json.new) @endpoint = endpoint @parser = parser @headers = { 'Content-Type' => 'application/json', 'User-Agent' => "Ruby HIBP-Client #{Hibp::VERSION}" } end |
Instance Attribute Details
#headers ⇒ Object (readonly)
Returns the value of attribute headers.
11 12 13 |
# File 'lib/hibp/request.rb', line 11 def headers @headers end |
Instance Method Details
#get(params: nil, headers: nil) ⇒ Object
Perform a GET request
39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/hibp/request.rb', line 39 def get(params: nil, headers: nil) response = rest_client.get(@endpoint) do |request| configure_request(request: request, params: params, headers: headers) end @parser ? @parser.parse_response(response) : response.body rescue Faraday::ResourceNotFound nil rescue StandardError => e handle_error(e) end |