class Cwc::Client

Attributes

options[RW]

Public Class Methods

configure(options) click to toggle source
# File lib/cwc/client.rb, line 25
def configure(options)
  self.default_client_configuration = options
end
default_client_configuration() click to toggle source
# File lib/cwc/client.rb, line 21
def default_client_configuration
  @default_client_configuration ||= {}
end
default_client_configuration=(x) click to toggle source
# File lib/cwc/client.rb, line 17
def default_client_configuration=(x)
  @default_client_configuration = x
end
new(options={}) click to toggle source

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
# 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

Public Instance Methods

create_message(params) click to toggle source

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

# 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) click to toggle source
# 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) click to toggle source
# File lib/cwc/client.rb, line 130
def office_supported?(office_code)
  !offices.find{ |office| office.code == office_code }.nil?
end
required_json(o={}) click to toggle source
# File lib/cwc/client.rb, line 134
def required_json(o={})
  Cwc::RequiredJson.merge(o)
end
validate(message) click to toggle source
# 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

Protected Instance Methods

action(action) click to toggle source
# File lib/cwc/client.rb, line 140
def action(action)
  host = options[:host].sub(/\/+$/, '')
  action = action.sub(/^\/+/, '')
  "#{host}/#{action}?apikey=#{options[:api_key]}"
end

Private Instance Methods

get(url) click to toggle source
# File lib/cwc/client.rb, line 148
def get(url)
  verify = !["false", "0"].include?(ENV["CWC_VERIFY_SSL"])
  headers = { host: ENV["CWC_HOST_HEADER"] }.reject{ |_, v| v.nil? }
  RestClient::Resource.new(url, verify_ssl: verify).get(headers)
end
offices() click to toggle source
# File lib/cwc/client.rb, line 162
def offices
  if options[:host] =~ %r{^https://cwc.house.gov}
    Cwc::OfficeCodes.map{ |code| Office.new(code) }
  else
    response = get action("/v2/offices")
    JSON.parse(response.body).map{ |code| Office.new(code) }
  end
end
post(url, message) click to toggle source
# File lib/cwc/client.rb, line 154
def post(url, message)
  verify = !["false", "0"].include?(ENV["CWC_VERIFY_SSL"])
  headers = { content_type: :xml, host: ENV["CWC_HOST_HEADER"] }.
            reject{ |_, v| v.nil? }
  RestClient::Resource.new(url, verify_ssl: verify).
    post(message, headers)
end