class ExotelApi::Call

Public Class Methods

connect_to_agent(params={}) click to toggle source
# File lib/exotel_api/call.rb, line 19
def self.connect_to_agent(params={})
  self.new.connect_to_agent(params)
end
connect_to_flow(params={}) click to toggle source
# File lib/exotel_api/call.rb, line 15
def self.connect_to_flow(params={})
  self.new.connect_to_flow(params)
end
details(params={}) click to toggle source
# File lib/exotel_api/call.rb, line 23
def self.details(params={})
  self.new.details(params)
end
new() click to toggle source
# File lib/exotel_api/call.rb, line 8
def initialize; end
shoot(params={}) click to toggle source

TODO check if this is a good decision to provide a wrapper.

# File lib/exotel_api/call.rb, line 11
def self.shoot(params={})
  self.new.shoot(params)
end

Public Instance Methods

connect_to_agent(params={}) click to toggle source
# File lib/exotel_api/call.rb, line 41
def connect_to_agent(params={})
  if valid?(params, {:type => 'agent'})
    params = transfrom_params(params, {:type => 'agent'})
    make_call(params)
  end
end
connect_to_flow(params={}) click to toggle source
# File lib/exotel_api/call.rb, line 34
def connect_to_flow(params={})
  if valid?(params, {:type => 'flow'})
    params = transfrom_params(params, {:type => 'flow'})
    make_call(params)
  end
end
details(sid) click to toggle source
# File lib/exotel_api/call.rb, line 48
def details(sid)
  response = self.class.get("/#{ExotelApi.exotel_sid}/Calls/#{sid}",  :basic_auth => auth)
  handle_response(response)
end
shoot(params={}) click to toggle source
# File lib/exotel_api/call.rb, line 27
def shoot(params={})
  if valid?(params, {:type => 'flow'})
    params = transfrom_params(params, {:type => 'flow'})
    make_call(params)
  end
end

Protected Instance Methods

auth() click to toggle source
# File lib/exotel_api/call.rb, line 75
def auth
  {:username => ExotelApi.exotel_sid, :password => ExotelApi.exotel_token}
end
camelcase_key(key) click to toggle source
# File lib/exotel_api/call.rb, line 94
def camelcase_key(key)
  key.to_s.split('_').map(&:capitalize).join.to_sym #Input: call_type, Output: :CallType
end
flow_url(flow_id) click to toggle source
# File lib/exotel_api/call.rb, line 79
def flow_url(flow_id)
  "http://my.exotel.in/exoml/start/#{flow_id}"
end
handle_response(response) click to toggle source
# File lib/exotel_api/call.rb, line 98
def handle_response(response)
  case response.code.to_i
  when 200...300 then ExotelApi::Response.new(response)
  when 401 then raise ExotelApi::AuthenticationError, "#{response.body} Verify your sid and token." 
  when 403 then ExotelApi::Response.new(response)
  else
    raise ExotelApi::UnexpectedError, response.body
  end
end
make_call(params) click to toggle source
# File lib/exotel_api/call.rb, line 55
def make_call(params)
  response = self.class.post(URI.escape("/#{ExotelApi.exotel_sid}/Calls/connect"), {:body => params, :basic_auth => auth })
  handle_response(response)
end
transfrom_params(params, options) click to toggle source
# File lib/exotel_api/call.rb, line 83
def transfrom_params(params, options)
  if options[:type] == 'flow'
    #Construct flow url and delete flow_id.
    params = params.merge(:URL => flow_url(params[:flow_id]))
    params.delete(:flow_id)
  end
  
  #Keys are converted to camelcase
  params.inject({}){ |h, (key, value)| h[camelcase_key(key)] = value; h }
end
valid?(params, options) click to toggle source
# File lib/exotel_api/call.rb, line 60
def valid?(params, options)
  mandatory_keys = [:from, :to, :caller_id, :call_type]
  mandatory_keys << :flow_id if options[:type] == 'flow'

  unless mandatory_keys.all?{|key| params.keys.include?(key)}
    raise ExotelApi::ParamsError, "Missing one or many required parameters." 
  end 
  valid_call_type?(params)
  return true  
end
valid_call_type?(params) click to toggle source
# File lib/exotel_api/call.rb, line 71
def valid_call_type?(params)
  raise ExotelApi::ParamsError, "Call Type is not valid" unless ['trans', 'promo'].include?(params[:call_type])
end