Class: SFax::Client

Inherits:
Object
  • Object
show all
Includes:
Constants
Defined in:
lib/sfax/client.rb

Constant Summary

Constants included from Constants

SFax::Constants::SEND_FAX_PATH, SFax::Constants::SEND_FAX_QUEUE_ID_ERROR_VALUE, SFax::Constants::SFAX_URL

Instance Method Summary collapse

Constructor Details

#initialize(username:, api_key:, encryption_key:) ⇒ Client

Returns a new instance of Client.



11
12
13
14
15
# File 'lib/sfax/client.rb', line 11

def initialize(username:, api_key:, encryption_key:)
  @username = username
  @api_key = api_key
  @encryptor = SFax::Encryptor.new(key: encryption_key)
end

Instance Method Details

#send_fax(fax_number:, file:, recipient_name:) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/sfax/client.rb', line 17

def send_fax(fax_number:, file:, recipient_name:)
  validate_fax_number fax_number

  response = connection.post SEND_FAX_PATH do |request|
    request.params = {
      'token' => generate_token,
      'ApiKey' => api_key,
      'RecipientFax' => fax_number,
      'RecipientName' => recipient_name,
    }
    request.body = {
      file: Faraday::UploadIO.new(file, 'application/pdf', "#{gen_dt}.pdf")
    }
  end

  response_object = JSON.parse(response.body)
  # TODO: Refactor into proper response objec.
  def response_object.fax_id
    fetch('SendFaxQueueId')
  end

  def response_object.success?
    fetch('isSuccess')
  end

  def response_object.message
    fetch('message')
  end

  response_object.tap do |o|
    raise SendFaxError, o.message unless o.success?
    raise SendFaxError, o.message if o.fax_id.to_s == SEND_FAX_QUEUE_ID_ERROR_VALUE
  end
end