Class: Psc::Connection

Inherits:
Faraday::Connection
  • Object
show all
Defined in:
lib/psc/connection.rb

Overview

A Faraday::Connection set up for use with Patient Study Calendar. See the README for a couple of examples.

Instance Method Summary collapse

Constructor Details

#initialize(url, options) {|builder| ... } ⇒ Connection

Create a new PSC connection. This is a Faraday::Connection with the following middleware:

  • Either Faraday::HttpBasic or Faraday::PscToken depending on the contents of the :authenticator option
  • Faraday::StringIsXml
  • ::Faraday::Request::JSON
  • ::Faraday::Request::UrlEncoded
  • ::FaradayStack::ResponseXML for content-type text/xml
  • ::FaradayStack::ResponseJSON for content-type application/json
  • ::Faraday::Adapter::NetHttp

If a block is provided, it will receive the Faraday builder so that you can append more middleware. If you provide your own adapter, the net/http adapter will not be appended.

Parameters:

  • url (String)

    the base URL for your PSC instance

  • options (Hash)

    the options for the connection. These are same as accepted by Faraday::Connection, plus:

Options Hash (options):

  • :authenticator (Hash)

    parameters for the authentication middleware. These are detailed in the README.

Yields:

  • (builder)

    (optional)



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/psc/connection.rb', line 36

def initialize(url, options)
  super do |builder|
    builder.use *authentication_middleware(options[:authenticator])
    builder.use Psc::Faraday::StringIsXml
    builder.use Psc::Faraday::AcceptPscTypes
    builder.request :json
    builder.request :url_encoded
    builder.use FaradayStack::ResponseXML, :content_type => 'text/xml'
    builder.use FaradayStack::ResponseJSON, :content_type => 'application/json'

    if block_given?
      yield builder
    end

    builder.adapter :net_http unless has_adapter?(builder)
  end

  unless self.path_prefix =~ %r{/api/v1$}
    self.path_prefix = if self.path_prefix == '/'
                         '/api/v1'
                       else
                         self.path_prefix + '/api/v1'
                       end
  end
end