class Bluedart::Base

Private Instance Methods

body_xml(xml, message, params, extra) click to toggle source

input params xml - Nokogiri::XML::Builder message - String params - Hash extra - Hash

Appends Body XML Block

Returns Nokogiri::XML::Builder

# File lib/bluedart/base.rb, line 147
def body_xml(xml, message, params, extra)
  content_ns_key = "#{namespace_key(:content)}"
  xml.Body {
    xml[content_ns_key].send(message) do |xml|
      hash_xml(xml, params)
      extra.each do |key, value|
        xml[content_ns_key].send(key) { profile_xml(xml, value)} if key.downcase == 'profile'
      end
    end
  }
  xml
end
hash_xml(xml, params) click to toggle source

input params xml - Nokogiri::XML::Builder params - Hash

Transform Hash to XML

Returns Nokogiri::XML::Builder

# File lib/bluedart/base.rb, line 102
def hash_xml(xml, params)
  params.each do |key, value|
    xml = xml_key_value(key, value, xml)
  end
  xml
end
header_xml(xml, wsa) click to toggle source

input params xml - Nokogiri::XML::Builder wsa - string

Appends Header XML Block

Returns Nokogiri::XML::Builder

# File lib/bluedart/base.rb, line 88
def header_xml(xml, wsa)
  xml.Header {
    xml["#{namespace_key(:wsa)}"].Action(wsa, "#{namespace_key(:envelope)}:mustUnderstand" => true)
  }
  xml
end
make_request(opts) click to toggle source

input params opts - Hash

Fire XML request and return parsed response

Returns Hash

# File lib/bluedart/base.rb, line 182
def make_request(opts)
  body = request_xml(opts)
  response = request(opts[:url], body.to_xml)
  response_return(response, opts[:message])
end
multi_line_address(address, line_length) click to toggle source

input params address - String line_length - Integer

Splits address into array by count of characters

Returns Array

# File lib/bluedart/base.rb, line 259
def multi_line_address(address, line_length)
  multi_line_address_block = []
  i = 0
  address.split(/[ ,]/).each do |s|
    if multi_line_address_block[i].blank?
      multi_line_address_block[i] = s
    elsif (multi_line_address_block[i].length + s.length < line_length)
      multi_line_address_block[i] += ' ' + s
    else
      i += 1
      multi_line_address_block[i] = s
    end
  end
  multi_line_address_block
end
namespace_hash() click to toggle source

input params - none

Creates hash with xml styled namespace key and value

Returns Hash

# File lib/bluedart/base.rb, line 46
def namespace_hash
  opt = {}
  namespaces.each do |type, attrs|
    key = "xmlns:#{attrs[:key]}"
    opt[key] = attrs[:value]
  end
  opt
end
namespace_key(name) click to toggle source

input params name - symbol

Provides key for a given namespace block

Returns String

# File lib/bluedart/base.rb, line 61
def namespace_key(name)
  namespaces[name][:key]
end
namespaces() click to toggle source

input params - none

Creates hash containing required namespaces

Returns Hash

# File lib/bluedart/base.rb, line 30
def namespaces
  ns = {}
  ns[:envelope] = {key:'env', value: 'http://www.w3.org/2003/05/soap-envelope'}
  ns[:content]  = {key:'ns1', value: 'http://tempuri.org/'}
  ns[:profile]  = {key:'ns2', value: 'http://schemas.datacontract.org/2004/07/SAPI.Entities.Admin'}
  ns[:wsa]      = {key:'ns3', value: 'http://www.w3.org/2005/08/addressing'}
  ns[:shipment] = {key:'ns4', value: 'http://schemas.datacontract.org/2004/07/SAPI.Entities.WayBillGeneration'}
  ns[:pickup]   = {key:'ns5', value: 'http://schemas.datacontract.org/2004/07/SAPI.Entities.Pickup'}
  ns
end
profile_hash(details, creds) click to toggle source

input params details - Hash creds - Hash

Creates profile hash

Return Hash

# File lib/bluedart/base.rb, line 16
def profile_hash(details, creds)
  params = {}
  params[:api_type] = details[:api_type]
  params[:license_key] = creds[:license_key]
  params[:login_id] = creds[:login_id]
  params[:version] = details[:version]
  params
end
profile_xml(xml, values) click to toggle source

input params xml - Nokogiri::XML::Builder values - Hash

Appends Profile XML Block

Returns Nokogiri::XML::Builder

# File lib/bluedart/base.rb, line 72
def profile_xml(xml, values)
  ns_key = "#{namespace_key(:profile)}"
  xml[ns_key].Api_type values[:api_type]
  xml[ns_key].LicenceKey values[:license_key]
  xml[ns_key].LoginID values[:login_id]
  xml[ns_key].Version values[:version]
  xml
end
request(url, body) click to toggle source

input params url - String body - String

Fires request and returns response

Returns Hash

# File lib/bluedart/base.rb, line 236
def request(url, body)
  res = HTTParty.post(url, body: body, headers: {'Content-Type' => 'application/soap+xml; charset="utf-8"'}, :verify => false)
  content = xml_hash(res.body)[:envelope][:body]
end
request_xml(opts) click to toggle source

input params opts - Hash

Create XML Request

Returns Nokogiri::XML::Builder

# File lib/bluedart/base.rb, line 166
def request_xml(opts)
  envelope_ns_key = "#{namespace_key(:envelope)}"
  builder = Nokogiri::XML::Builder.new(encoding: 'UTF-8') do |xml|
    xml[envelope_ns_key].Envelope(namespace_hash) {
      xml = header_xml(xml, opts[:wsa])
      xml = body_xml(xml, opts[:message], opts[:params], opts[:extra])
    }
  end
end
required_content(prefix, content) click to toggle source

input params prefix - String content - Hash

Removes Junk content from response

Returns Hash

# File lib/bluedart/base.rb, line 219
def required_content(prefix, content)
  if content[:fault].nil?
    prefix_s = prefix.snakecase
    keys = (prefix_s + '_response').to_sym, (prefix_s + '_result').to_sym
    return content[keys[0]][keys[1]]
  else
    return {error: true, error_text: content[:fault]}
  end
end
response_return(response, message) click to toggle source

input params response - Hash message - String

Provides Parsed Response

Returns Hash

# File lib/bluedart/base.rb, line 195
def response_return(response, message)
  response_hash = {error: false, error_text: ''}
  if response[:error]
    response_hash[:error] = true
    response_hash[:error_text] = response[:error_message]
  else
    content = required_content(message, response)
    if content[:is_error] || content[:error]
      response_hash[:error] = true
      response_hash[:error_text] = content[:error_message] || content[:status] || content[:error_text]
    else
      response_hash[:content] = content
    end
  end
  response_hash
end
singular(key) click to toggle source

TODO: ITS A HACK NEEDS TO BE REMOVED input params key - string

Removes last letter from string

Returns String

# File lib/bluedart/base.rb, line 116
def singular(key)
  key = key[0..-2]
end
xml_hash(xml) click to toggle source

input params xml - String

Converts XML to Hash

Returns Hash

# File lib/bluedart/base.rb, line 247
def xml_hash(xml)
  nori = Nori.new(strip_namespaces: true, :convert_tags_to => lambda { |tag| tag.snakecase.to_sym })
  nori.parse(xml)
end
xml_key_value(key, value, xml) click to toggle source
# File lib/bluedart/base.rb, line 120
def xml_key_value(key, value, xml)
  if value.is_a?(Hash)
    xml.send(key) do |xml|
      value.each {|inner_key, inner_values| xml = xml_key_value(inner_key, inner_values, xml)}
    end
  elsif value.is_a?(Array)
    new_key = singular(key)
    value.each do |single_value|
      xml.send(key) do |xml|
        xml = hash_xml(xml, single_value)
      end
    end
  else
    xml.send(key, value)
  end
  xml
end