class Microbilt::Requests::CreateForm

Attributes

callback_type[R]
callback_url[RW]
contact_by[R]
do_not_request_phones[R]
final_url[RW]

Public Class Methods

new(options = nil) click to toggle source
# File lib/microbilt/requests/create_form.rb, line 9
def initialize(options = nil)
  options.keys.each do |k|
    self.send("#{k}=", options[k])
  end if options

  do_not_request_phones = false if do_not_request_phones.nil?
end

Public Instance Methods

call(customer) click to toggle source
# File lib/microbilt/requests/create_form.rb, line 27
def call(customer)
  response = http_connection.post do |req|
    req.url Microbilt::Configuration::CREATE_FORM_URI
    req.headers['Content-Type'] = Microbilt::Configuration::CONTENT_TYPE
    req.params = all_params(customer)
  end

  handle_response(response)
end
to_params() click to toggle source
# File lib/microbilt/requests/create_form.rb, line 17
def to_params
  {
    'CallbackUrl' => callback_url,
    'CallbackType' => callback_type.to_s,
    'FinalURL' => final_url,
    'ContactBy' => contact_by.to_s.upcase,
    'DoNotRequestPhones' => do_not_request_phones.to_s
  }
end

Private Instance Methods

add_customer_url(token) click to toggle source
# File lib/microbilt/requests/create_form.rb, line 78
def add_customer_url(token)
  "#{Microbilt.configuration.server_url}#{Microbilt::Configuration::ADD_CUSTOMER_URI}?reference=#{token}"
end
all_params(customer) click to toggle source
# File lib/microbilt/requests/create_form.rb, line 90
def all_params(customer)
  to_params.merge(Microbilt.configuration.to_params).merge(customer.to_params)
end
callback_type=(value) click to toggle source
# File lib/microbilt/requests/create_form.rb, line 109
def callback_type=(value)
  raise ArgumentError.new("Invalid value: #{value}") unless
    %i(json xml).include?(value.to_sym)

  @callback_type = value.to_sym
end
contact_by=(value) click to toggle source

Setters

# File lib/microbilt/requests/create_form.rb, line 95
def contact_by=(value)
  raise ArgumentError.new("Invalid value: #{value}") unless
     %i(sms email both neither).include?(value.to_sym)

  @contact_by = value.to_sym
end
do_not_request_phones=(value) click to toggle source
# File lib/microbilt/requests/create_form.rb, line 102
def do_not_request_phones=(value)
  raise ArgumentError.new("Invalid value: #{value}") unless
    value.is_a?(TrueClass) || value.is_a?(Falseclass)

  @do_not_request_phones = value
end
get_data_url(token) click to toggle source
# File lib/microbilt/requests/create_form.rb, line 82
def get_data_url(token)
  "#{Microbilt.configuration.server_url}#{Microbilt::Configuration::GET_DATA_URI}?reference=#{token}"
end
get_html_data_url(token) click to toggle source
# File lib/microbilt/requests/create_form.rb, line 86
def get_html_data_url(token)
  "#{Microbilt.configuration.server_url}#{Microbilt::Configuration::GET_HTML_DATA_URI}?guid={#{token}}"
end
handle_response(response) click to toggle source
# File lib/microbilt/requests/create_form.rb, line 39
def handle_response(response)
  output = output_template(response)

  if output[:status] == :ok
    token = parse_token(response.body)
    output[:token] = token
    output[:add_customer_form_url] = add_customer_url(token)
    output[:get_data_url] = get_data_url(token)
    output[:get_html_data_url] = get_html_data_url(token)
  end

  output
end
output_template(response) click to toggle source
# File lib/microbilt/requests/create_form.rb, line 53
def output_template(response)
  status = response.status.to_i

  if status.between?(200, 299)
    status = :ok
  elsif status.between?(300, 399)
    status = :redirect
  elsif status.between?(400, 599)
    status = :error
  end

  {
    success: status == :ok,
    status: status,
    http_response: {
      status: response.status,
      body: response.body
    }
  }
end
parse_token(response_body) click to toggle source
# File lib/microbilt/requests/create_form.rb, line 74
def parse_token(response_body)
  response_body.match(/{(.*)}/)[1]
end