Class: Hibp::Request

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(endpoint:, parser: Parsers::Json.new) ⇒ Request

Returns a new instance of Request.

Parameters:

  • parser (Hibp::Parser) (defaults to: Parsers::Json.new)

    - A tool to parse and convert data into appropriate models

  • endpoint (String)

    - A specific API endpoint to call appropriate method



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

#headersObject (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

Parameters:

  • params (Hash) (defaults to: nil)

    - Additional query params

  • headers (Hash) (defaults to: nil)

    - Additional HTTP headers

Raises:



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