Class: Cwc::Message

Inherits:
Object
  • Object
show all
Defined in:
lib/cwc/message.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeMessage

Returns a new instance of Message.



16
17
18
19
20
21
22
23
# File 'lib/cwc/message.rb', line 16

def initialize
  self.delivery = {}
  self.recipient = {}
  self.constituent = {}
  self.message = {}

  self.delivery_id = SecureRandom.random_number(36**33).to_s(36).rjust(12, "0")[0, 32]
end

Instance Attribute Details

#constituentObject

Returns the value of attribute constituent.



11
12
13
# File 'lib/cwc/message.rb', line 11

def constituent
  @constituent
end

#deliveryObject

Returns the value of attribute delivery.



9
10
11
# File 'lib/cwc/message.rb', line 9

def delivery
  @delivery
end

#delivery_idObject

Returns the value of attribute delivery_id.



14
15
16
# File 'lib/cwc/message.rb', line 14

def delivery_id
  @delivery_id
end

#messageObject

Returns the value of attribute message.



12
13
14
# File 'lib/cwc/message.rb', line 12

def message
  @message
end

#recipientObject

Returns the value of attribute recipient.



10
11
12
# File 'lib/cwc/message.rb', line 10

def recipient
  @recipient
end

Instance Method Details

#constituent_section(xml) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/cwc/message.rb', line 97

def constituent_section(xml)
  xml.Constituent {
    xml.Prefix constituent.fetch(:prefix)
    xml.FirstName constituent.fetch(:first_name)

    if constituent[:middle_name]
      xml.MiddleName constituent[:middle_name]
    end

    xml.LastName constituent.fetch(:last_name)

    if constituent[:suffix]
      xml.Suffix constituent[:suffix]
    end        

    if constituent[:title]
      xml.Title constituent[:title]
    end

    if constituent[:organization]
      xml.Organization constituent[:organization]
    end

    xml.Address1 Array(constituent.fetch(:address))[0]
    if Array(constituent[:address])[1]
      xml.Address2 Array(constituent[:address])[1]

      if Array(constituent[:address])[2]
        xml.Address3 Array(constituent[:address])[2]
      end
    end
    xml.City constituent.fetch(:city)
    xml.StateAbbreviation constituent.fetch(:state_abbreviation)
    xml.Zip constituent.fetch(:zip)

    if constituent[:phone]
      xml.Phone constituent[:phone]
    end

    if constituent[:address_validation]
      xml.AddressValidation "Y"
    end

    xml.Email constituent.fetch(:email)

    if constituent[:email_validation]
      xml.EmailValidation "Y"
    end
  }
end

#delivery_dateObject



25
26
27
# File 'lib/cwc/message.rb', line 25

def delivery_date
  Time.now.strftime("%Y%m%d")
end

#delivery_section(xml) ⇒ Object



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
# File 'lib/cwc/message.rb', line 42

def delivery_section(xml)
  xml.Delivery {
    xml.DeliveryId delivery_id
    xml.DeliveryDate delivery_date

    xml.DeliveryAgent delivery.dig!(:agent, :name)
    xml.DeliveryAgentAckEmailAddress delivery.dig!(:agent, :ack_email)
    xml.DeliveryAgentContact {
      xml.DeliveryAgentContactName delivery.dig!(:agent, :contact_name)
      xml.DeliveryAgentContactEmail delivery.dig!(:agent, :contact_email)
      xml.DeliveryAgentContactPhone delivery.dig!(:agent, :contact_phone)
    }

    if delivery.dig(:organization, :name)
      xml.Organization delivery.dig(:organization, :name)
    end

    if (delivery.dig(:organization, :contact) || {}).keys.grep(/name|email|phone/).any?
      xml.OrganizationContact {
        if delivery.dig(:organization, :contact, :name)
          xml.OrganizationContactName delivery.dig(:organization, :contact, :name)
        end

        if delivery.dig(:organization, :contact, :email)
          xml.OrganizationContactEmail delivery.dig(:organization, :contact, :email)
        end

        if delivery.dig(:organization, :contact, :phone)
          xml.OrganizationContactPhone delivery.dig(:organization, :contact, :phone)
        end
      }
    end

    if delivery.dig(:organization, :about)
      xml.OrganizationAbout delivery.dig(:organization, :about)
    end

    xml.CampaignId delivery.fetch(:campaign_id)
  }
end

#message_section(xml) ⇒ Object



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/cwc/message.rb', line 148

def message_section(xml)
  xml.Message {
    xml.Subject message.fetch(:subject)

    xml.LibraryOfCongressTopics {
      message.fetch(:library_of_congress_topics).each do |topic|
        xml.LibraryOfCongressTopic topic
      end
    }

    Array(message[:bills]).each do |bill|
      xml.Bill {
        xml.BillCongress bill[:congress]
        xml.BillTypeAbbreviation bill[:type_abbreviation]
        xml.BillNumber bill[:number]
      }
    end

    if message[:pro_or_con]
      xml.ProOrCon message[:pro_or_con]
    end

    if message[:organization_statement]
      xml.OrganizationStatement message[:organization_statement]
    end

    if message[:constituent_message]
      xml.ConstituentMessage message[:constituent_message]
    end

    if message[:more_info]
      xml.MoreInfo message[:more_info]
    end
  }
end

#recipient_section(xml) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/cwc/message.rb', line 83

def recipient_section(xml)
  xml.Recipient {
    xml.MemberOffice recipient.fetch(:member_office)

    if recipient[:is_response_requested]
      xml.IsResponseRequested "Y"
    end

    if recipient[:newsletter_opt_in]
      xml.NewsletterOptIn "Y"
    end
  }
end

#to_xmlObject



29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/cwc/message.rb', line 29

def to_xml
  Nokogiri::XML::Builder.new do |xml|
    xml.CWC {
      xml.CWCVersion "2.0"

      delivery_section(xml)
      recipient_section(xml)
      constituent_section(xml)
      message_section(xml)
    }         
  end.to_xml
end