Class: Faxage::ReceiveFax

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(username:, company:, password:) ⇒ ReceiveFax

Returns a new instance of ReceiveFax.



12
13
14
15
16
# File 'lib/faxage/receive_fax.rb', line 12

def initialize(username:, company:, password:)
  @username = username
  @company = company
  @password = password
end

Instance Attribute Details

#companyObject (readonly)

Assigned FAXAGE username



8
9
10
# File 'lib/faxage/receive_fax.rb', line 8

def company
  @company
end

#passwordObject (readonly)

Assigned FAXAGE username



8
9
10
# File 'lib/faxage/receive_fax.rb', line 8

def password
  @password
end

#usernameObject (readonly)

Assigned FAXAGE username



8
9
10
# File 'lib/faxage/receive_fax.rb', line 8

def username
  @username
end

Instance Method Details

#listfax(**options) ⇒ Object



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
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/faxage/receive_fax.rb', line 18

def listfax(**options)
  # This operation is used to gather a list of incoming faxes for your account.

  subdirectory = "/httpsfax.php"

  body = {
    operation: "listfax",
    username: username,
    company: company,
    password: password
  }.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?("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?("ERR11: No incoming faxes available")
    raise NoIncomingFaxesError.new("There are no incoming faxes to list for you.")
  else
    data = []
    response.parsed_response.split("\n").each do |received_fax|
      # <recvid><tab><recvdate>(OPTIONAL:
      # <tab><starttime>)
      # <tab><CID><tab><DNIS>(OPTIONAL:
      # <tab><filename>)(OPTIONAL:
      # <tab><pagecount>)(OPTIONAL: <tab><tsid>)
      individual_fax = Hash.new
      received_fax.split("\t").each_with_index do |item, index|
        if options[:starttime].nil?
          case index
          when 0
            individual_fax[:recvid] = item
          when 1
            individual_fax[:revdate] = item
          when 2
            individual_fax[:cid] = item
          when 3
            individual_fax[:dnis] = item
          when 4 && !options[:filename].nil?
            individual_fax[:filename] = item
          when 4 && options[:filename].nil? && !options[:pagecount].nil?
            individual_fax[:pagecount] = item
          when 4 && options[:filename].nil? && options[:pagecount].nil? && !options[:tsid].nil?
            individual_fax[:tsid] = item
          when 5 && !options[:filename].nil? && !options[:pagecount].nil?
            individual_fax[:pagecount] = item
          when 5 && !options[:filename].nil? && options[:pagecount].nil? && !options[:tsid].nil?
            individual_fax[:tsid] = item
          when 6 && !options[:filename].nil? && !options[:pagecount].nil? && !options[:tsid].nil?
            individual_fax[:tsid] = item
          end
        else
          case index
          when 0
            individual_fax[:recvid] = item
          when 1
            individual_fax[:revdate] = item
          when 2
            individual_fax[:starttime] = item
          when 3
            individual_fax[:cid] = item
          when 4
            individual_fax[:dnis] = item
          when 5 && !options[:filename].nil?
            individual_fax[:filename] = item
          when 5 && options[:filename].nil? && !options[:pagecount].nil?
            individual_fax[:pagecount] = item
          when 5 && options[:filename].nil? && options[:pagecount].nil? && !options[:tsid].nil?
            individual_fax[:tsid] = item
          when 6 && !options[:filename].nil? && !options[:pagecount].nil?
            individual_fax[:pagecount] = item
          when 6 && !options[:filename].nil? && options[:pagecount].nil? && !options[:tsid].nil?
            individual_fax[:tsid] = item
          when 7 && !options[:filename].nil? && !options[:pagecount].nil? && !options[:tsid].nil?
            individual_fax[:tsid] = item
          end
        end
        data << individual_fax
      end
    end
    return data
  end
end