Class: Hyperclient::EntryPoint

Inherits:
Link
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/hyperclient/entry_point.rb

Overview

The EntryPoint is the main public API for Hyperclient. It is used to initialize an API client and setup the configuration.

Examples:

client = Hyperclient::EntryPoint.new('http://my.api.org')

Instance Method Summary collapse

Methods inherited from Link

#delete, #deprecation, #expand, #get, #head, #hreflang, #inspect, #method_missing, #name, #options, #patch, #post, #profile, #put, #resource, #respond_to_missing?, #templated?, #title, #to_ary, #type, #uri_template, #url, #variables

Constructor Details

#initialize(url) ⇒ EntryPoint

Initializes an EntryPoint.

Parameters:

  • url

    A String with the entry point of your API.



21
22
23
24
# File 'lib/hyperclient/entry_point.rb', line 21

def initialize(url)
  @link = {'href' => url}
  @entry_point = self
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Hyperclient::Link

Instance Method Details

#connectionObject

A Faraday connection to use as a HTTP client.

Returns:

  • a Faraday::Connection.



29
30
31
# File 'lib/hyperclient/entry_point.rb', line 29

def connection
  @connection ||= Faraday.new(url, {headers: default_headers}, &default_faraday_block)
end

#default_faraday_blockObject (private)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a block to initialize the Faraday connection. The default block includes a middleware to encode requests as JSON, a response middleware to parse JSON responses and sets the adapter as NetHttp.

These middleware can always be changed by accessing the Faraday connection.

Returns:

  • a block.



43
44
45
46
47
48
49
# File 'lib/hyperclient/entry_point.rb', line 43

def default_faraday_block
  lambda do |faraday|
    faraday.request  :json
    faraday.response :json, content_type: /\bjson$/
    faraday.adapter :net_http
  end
end

#default_headersObject (private)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns the default headers to initialize the Faraday connection. The default headers et the Content-Type and Accept to application/json.

Returns:

  • a Hash.



55
56
57
# File 'lib/hyperclient/entry_point.rb', line 55

def default_headers
  {'Content-Type' => 'application/json', 'Accept' => 'application/json'}
end