Class: Cwc::Client

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Client

Required options keys

api_key                         String
delivery_agent			String, must match the api key owner
delivery_agent_ack_email	String
delivery_agent_contact_name	String
delivery_agent_contact_email	String
delivery_agent_contact_phone	String, format xxx-xxx-xxxx


37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/cwc/client.rb', line 37

def initialize(options={})
  options = self.class.default_client_configuration.merge(options)
  self.options = {
    api_key: options.fetch(:api_key),
    host: options.fetch(:host),

    delivery_agent: {
      name: options.fetch(:delivery_agent),
      ack_email: options.fetch(:delivery_agent_ack_email),
      contact_name: options.fetch(:delivery_agent_contact_name),
      contact_email: options.fetch(:delivery_agent_contact_email),
      contact_phone: options.fetch(:delivery_agent_contact_phone)
    }
  }
end

Instance Attribute Details

#optionsObject

Returns the value of attribute options.



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

def options
  @options
end

Class Method Details

.configure(options) ⇒ Object



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

def configure(options)
  self.default_client_configuration = options
end

.default_client_configurationObject



21
22
23
# File 'lib/cwc/client.rb', line 21

def default_client_configuration
  @default_client_configuration ||= {}
end

.default_client_configuration=(x) ⇒ Object



17
18
19
# File 'lib/cwc/client.rb', line 17

def default_client_configuration=(x)
  @default_client_configuration = x
end

Instance Method Details

#create_message(params) ⇒ Object

Params format {

 campaign_id:		String
 recipient: {
   member_office:		String
   is_response_requested:	Boolean	?
   newsletter_opt_in:		Boolean	?
 },
 organization: {
   name:		String	?
   contact: {
     name:	String	?
     email:	String	?
     phone:	String	?
     about:	String	?
   }
 },
 constituent: {
   prefix:		String
   first_name:		String
   middle_name:		String	?
   last_name:		String
   suffix:		String	?
   title:		String	?
   organization:		String	?
   address:		Array[String]
   city:			String
   state_abbreviation:	String
   zip:			String
   phone:		String	?
   address_validation:	Boolean	?
   email:		String
   email_validation:	Boolean	?
},
message: {
  subject:			String
  library_of_congress_topics:	Array[String], drawn from Cwc::TopicCodes. Must give at least 1.
  bills:	{			Array[Hash]
    congress:			Integer	?
    type_abbreviation:		String
    number:			Integer
  },
  pro_or_con:			"pro" or "con"	?
  organization_statement:	String		?
  constituent_message:		String		?
  more_info:			String (URL)	?
}

Use message for personal message, or message for campaign message At least one of these must be given



104
105
106
107
108
109
110
111
112
113
114
# File 'lib/cwc/client.rb', line 104

def create_message(params)
  Cwc::Message.new.tap do |message|
    message.delivery[:agent] = options.fetch(:delivery_agent)
    message.delivery[:organization] = params.fetch(:organization, {})
    message.delivery[:campaign_id] = params.fetch(:campaign_id)

    message.recipient.merge!(params.fetch(:recipient))
    message.constituent.merge!(params.fetch(:constituent))
    message.message.merge!(params.fetch(:message))
  end
end

#deliver(message) ⇒ Object



116
117
118
119
120
121
# File 'lib/cwc/client.rb', line 116

def deliver(message)
  post action("/v2/message"), message.to_xml
  true
rescue RestClient::BadRequest => e
  raise BadRequest.new(e)
end

#office_supported?(office_code) ⇒ Boolean

Returns:

  • (Boolean)


130
131
132
# File 'lib/cwc/client.rb', line 130

def office_supported?(office_code)
  !offices.find{ |office| office.code == office_code }.nil?
end

#required_json(o = {}) ⇒ Object



134
135
136
# File 'lib/cwc/client.rb', line 134

def required_json(o={})
  Cwc::RequiredJson.merge(o)
end

#validate(message) ⇒ Object



123
124
125
126
127
128
# File 'lib/cwc/client.rb', line 123

def validate(message)
  post action("/v2/validate"), message.to_xml
  true
rescue RestClient::BadRequest => e
  raise BadRequest.new(e)
end