Class: IngvQuake::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/ingv_quake/client.rb

Overview

The Client class is responsible for making HTTP requests to the INGV Earthquake web services API. It provides methods to initialize a connection, retrieve event resources, and configure the underlying Faraday adapter.

Constant Summary collapse

BASE_URL =

The base URL for the INGV Earthquake web services API.

'https://webservices.ingv.it/'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(adapter: Faraday.default_adapter, stubs: nil) ⇒ Client

Initializes a new Client instance with an optional adapter and stubs.

Examples:

Faraday adapter and stubs can be customized

client = IngvQuake::Client.new(adapter: MyAdapter, stubs: MyStubs)

Parameters:

  • adapter (Symbol, Class) (defaults to: Faraday.default_adapter)

    Faraday adapter to use for making HTTP requests.

  • stubs (Faraday::Adapter::Test::Stubs) (defaults to: nil)

    Optional stubs to use for testing.



19
20
21
22
# File 'lib/ingv_quake/client.rb', line 19

def initialize(adapter: Faraday.default_adapter, stubs: nil)
  @adapter = adapter
  @stubs = stubs
end

Instance Attribute Details

#adapterObject (readonly)

Returns the value of attribute adapter.



11
12
13
# File 'lib/ingv_quake/client.rb', line 11

def adapter
  @adapter
end

Instance Method Details

#connectionFaraday::Connection

Initializes a Faraday connection with the INGV Earthquake API base URL and XML headers. Configures the response to be parsed as XML and sets the adapter with optional stubs.

Returns:

  • (Faraday::Connection)

    The Faraday connection instance.



40
41
42
43
44
45
# File 'lib/ingv_quake/client.rb', line 40

def connection
  @connection ||= Faraday.new(url: BASE_URL) do |conn|
    conn.response :xml
    conn.adapter adapter, @stubs
  end
end

#get_eventsEventResource

Initializes an EventResource instance for fetching events from the API.

Examples:

Initialize new client and call ‘get_events`

client = IngvQuake::Client.new
events = client.get_events

Returns:

  • (EventResource)

    An instance of EventResource configured with the current client.



31
32
33
# File 'lib/ingv_quake/client.rb', line 31

def get_events
  EventResource.new(self)
end