class InterFAX::Client

Constants

HOST
USER_AGENT

Attributes

account[W]
documents[W]
files[W]
host[RW]
http[RW]
inbound[W]
outbound[W]
password[RW]
username[RW]

Public Class Methods

new(options = {}) click to toggle source
# File lib/interfax/client.rb, line 18
def initialize(options = {})
  self.username = options.fetch(:username) do
    ENV['INTERFAX_USERNAME'] || raise(KeyError, "Missing required argument: username")
  end

  self.password = options.fetch(:password) do
    ENV['INTERFAX_PASSWORD'] || raise(KeyError, "Missing required argument: password")
  end

  self.host = options.fetch(:host) do
    ENV['INTERFAX_HOST'] || HOST
  end

  self.http = Net::HTTP.new(host, Net::HTTP.https_default_port)
  http.set_debug_output $stdout if options[:debug]
  http.use_ssl = true
end

Public Instance Methods

account() click to toggle source
# File lib/interfax/client.rb, line 36
def account
  @account ||= InterFAX::Account.new(self)
end
delete(path) click to toggle source
# File lib/interfax/client.rb, line 76
def delete path
  uri = uri_for(path)
  request = Net::HTTP::Delete.new(uri.request_uri)
  transmit(request)
end
deliver(params = {}) click to toggle source
# File lib/interfax/client.rb, line 40
def deliver params = {}
  outbound.deliver(params)
end
documents() click to toggle source
# File lib/interfax/client.rb, line 52
def documents
  @documents ||= InterFAX::Documents.new(self)
end
files() click to toggle source
# File lib/interfax/client.rb, line 56
def files
  @files ||= InterFAX::Files.new(self)
end
get(path, params = {}) click to toggle source
# File lib/interfax/client.rb, line 60
def get path, params = {}, valid_keys = {}
  uri = uri_for(path, params, valid_keys)
  request = Net::HTTP::Get.new(uri.request_uri)
  transmit(request)
end
inbound() click to toggle source
# File lib/interfax/client.rb, line 48
def inbound
  @inbound ||= InterFAX::Inbound.new(self)
end
outbound() click to toggle source
# File lib/interfax/client.rb, line 44
def outbound
  @outbound ||= InterFAX::Outbound.new(self)
end
post(path, params = {}) click to toggle source
# File lib/interfax/client.rb, line 66
def post path, params = {}, valid_keys = {}, headers = {}, body = nil
  uri = uri_for(path, params, valid_keys)
  request = Net::HTTP::Post.new(uri.request_uri)
  headers.each do |key, value|
    request[key] = value
  end
  request.body = body if body
  transmit(request)
end

Private Instance Methods

json?(response) click to toggle source
# File lib/interfax/client.rb, line 145
def json?(response)
  type?(response, 'text/json')
end
parse(response) click to toggle source
# File lib/interfax/client.rb, line 106
def parse response
  case response
  when Net::HTTPSuccess
    if response['location']
      response['location']
    elsif tiff?(response) || pdf?(response)
      return [response.body, response['Content-Type']]
    elsif json?(response)
      begin
        JSON.parse(response.body)
      rescue JSON::ParserError
        response.body
      end
    else
      response.body
    end
  when Net::HTTPNotFound
    raise NotFoundError, "Record not found: #{response.body}"
  when Net::HTTPBadRequest
    raise BadRequestError, "Bad request (400): #{response.body}"
  when Net::HTTPUnauthorized
    raise UnauthorizedError, "Access Denied (401): #{response.body}"
  else
    if json?(response)
      raise ServerError, "HTTP #{response.code}: #{JSON.parse(response.body)}"
    else
      raise ServerError, "HTTP #{response.code}: #{response.body}"
    end
  end
end
pdf?(response) click to toggle source
# File lib/interfax/client.rb, line 137
def pdf?(response)
  type?(response, 'application/pdf')
end
tiff?(response) click to toggle source
# File lib/interfax/client.rb, line 141
def tiff?(response)
  type?(response, 'image/tiff')
end
transmit(request) click to toggle source
# File lib/interfax/client.rb, line 100
def transmit(request)
  request["User-Agent"] = USER_AGENT
  request.basic_auth username, password
  parse(http.request(request))
end
type?(response, mime_type) click to toggle source
# File lib/interfax/client.rb, line 149
def type?(response, mime_type)
  content_type = response['Content-Type']
  correct_header = content_type && content_type.split(';').first == mime_type
  has_body = response.body && response.body.length > 0
  correct_header && has_body
end
uri_for(path, params = {}, keys = {}) click to toggle source
# File lib/interfax/client.rb, line 84
def uri_for(path, params = {}, keys = {})
  params = validate(params, keys)
  uri = URI("https://#{host}#{path}")
  uri.query = URI.encode_www_form(params)
  uri
end
validate(params = {}) click to toggle source
# File lib/interfax/client.rb, line 91
def validate params = {}, keys = {}
  params.each do |key, value|
    if !keys.include? key.to_sym
      raise ArgumentError.new("Unexpected argument: #{key} - please make to use camelCase arguments")
    end
  end
  params
end