Class: AlephApi::RestfulApiClient

Inherits:
Object
  • Object
show all
Defined in:
lib/aleph_api/restful_api_client.rb

Defined Under Namespace

Classes: Patron, Record

Constant Summary collapse

RESTFUL_API_DEFAULT_PORT =
1891
RESTFUL_API_DEFAULT_ROOT_PATH =
"/rest-dlf"
RESTFUL_API_DEFAULT_SCHEME =
"http"
@@faraday_adapter =

don’s use a constant here, because faraday treats them diffrently

defined?(::Patron) ? :patron : ::Faraday.default_adapter

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ RestfulApiClient

Returns a new instance of RestfulApiClient.



26
27
28
29
30
31
# File 'lib/aleph_api/restful_api_client.rb', line 26

def initialize(options = {})
  options.symbolize_keys.try do |_sanitized_options|
    @host = (_sanitized_options[:host] || ENV["ALEPH_RESTFUL_API_HOST"] || ENV["ALEPH_API_HOST"]).try(:strip)
    @url = (_sanitized_options[:url] || ENV["ALEPH_RESTFUL_API_URL"]).try(:strip)
  end
end

Instance Attribute Details

#hostObject

Returns the value of attribute host.



19
20
21
# File 'lib/aleph_api/restful_api_client.rb', line 19

def host
  @host
end

#urlObject

Returns the value of attribute url.



20
21
22
# File 'lib/aleph_api/restful_api_client.rb', line 20

def url
  @url
end

Instance Method Details

#http(method, path, options = {}) ⇒ Object

inter-client api



42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/aleph_api/restful_api_client.rb', line 42

def http(method, path, options = {})
  # http://www.intridea.com/blog/2012/3/12/faraday-one-http-client-to-rule-them-all
  connection = ::Faraday.new do |_builder|
    _builder.request :url_encoded
    _builder.use AlephApi::Faraday::Response::EnforceUtf8EncodedBody
    _builder.adapter @@faraday_adapter
  end

  connection.send(method, "#{self.url}#{path}", options) do |_request|
    # some http libraries (e.g. patron) have tight connection timeouts which
    # may lead to (unnecessary) errors when used with aleph, so we raise 'em
    _request.options.open_timeout = 20 # seconds
  end
end

#patron(patron_id) ⇒ Object



33
34
35
# File 'lib/aleph_api/restful_api_client.rb', line 33

def patron(patron_id)
  Patron.new(self, patron_id)
end

#record(record_id) ⇒ Object



37
38
39
# File 'lib/aleph_api/restful_api_client.rb', line 37

def record(record_id)
  Record.new(self, record_id)
end