Class: Faxage::SendFax

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/faxage/send_fax.rb

Constant Summary collapse

JOB_ID_REGEX =
/(?<=JOBID:)\s+\d+/

Instance Method Summary collapse

Constructor Details

#initialize(username:, company:, password:, recipname:, faxno:, faxfilenames:, faxfiledata:, debug:) ⇒ SendFax

the httpsfax.php URL, except that it also returns the contents of your POST: Note that the debugging URL is still live and identical to the regular/production URL. For example, if you send a fax using the sendfax operation with the debugging URL, the fax will still get sent as normal.



27
28
29
30
31
32
33
34
35
36
# File 'lib/faxage/send_fax.rb', line 27

def initialize(username:, company:, password:, recipname:, faxno:, faxfilenames:, faxfiledata:, debug:)
  @username = username
  @company = company
  @password = password
  @recipname = recipname
  @faxno = faxno
  @faxfilenames = faxfilenames
  @faxfiledata = faxfiledata
  @debug = debug
end

Instance Method Details

#sendfax(**options) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/faxage/send_fax.rb', line 38

def sendfax(**options)
  if debug
    subdirectory = "/httpsfax-debug.php"
  else
    subdirectory = "/httpsfax.php"
  end

  body = {
    operation: "sendfax",
    username: username,
    company: company,
    password: password,
    recipname: recipname,
    faxno: faxno,
    faxfilenames: faxfilenames,
    faxfiledata: faxfiledata
  }.merge!(options)

  response = self.class.post(subdirectory,
    body: body
  )

  if response.parsed_response.nil?
    raise NoResponseError.new("An empty response was returned from Faxage.")
  elsif response.parsed_response.include?("ERR01: Database connection failed")
    raise FaxageInternalError.new("Internal FAXAGE error.")
  elsif response.parsed_response.include?("ERR02: Login incorrect")
    raise LoginError.new("One or more of username, company, password is incorrect or your account is disabled for some reason.")
  elsif response.parsed_response.include?("ERR03: No files to fax")
    raise NoFilesError.new("No valid files were found in faxfilenames[] and/or faxfiledata[].")
  elsif response.parsed_response.include?("ERR04: Fax number")
    raise InvalidFaxNoError.new("The faxno variable does not contain a 10-digit numeric only string.")
  elsif response.parsed_response.include?("ERR05")
    raise BlockedNumberError.new("The number you tried to fax to was blocked (outside of continental US, Canada and Hawaii or a 555, 911, or other invalid/blocked type of number).")
  elsif response.parsed_response.include?("ERR08: Unknown operation")
    raise UnknownOperationError.new("Either operation is not correctly hard coded or the POST was bad, the POST contents are returned for debugging purposes. #{response.parsed_response}")
  elsif response.parsed_response.include?("ERR15: Invalid Job ID")
    raise InvalidJobIdError.new("Internal FAXAGE error – the job was not properly inserted into our database.")
  else
    debug_output = response.parsed_response
    job_id = response.parsed_response.scan(JOB_ID_REGEX)[0].strip.to_i
    if debug
      data = {
        job_id: job_id,
        debug: debug_output
      }
    else
      data = {
        job_id: job_id
      }
    end
    return data
  end
end