class Megam::Deccanplato

Constants

API_VERSION1
HEADERS
OPTIONS
VERSION

Attributes

text[RW]

text is used to print stuff in the terminal (message, log, info, warn etc.)

Public Class Methods

new(options={}) click to toggle source

It is assumed that every API call will NOT use an API_KEY/email.

# File lib/megam/deccanplato.rb, line 52
def initialize(options={})
  puts "initiliaze entry"
  @options = OPTIONS.merge(options)  
end

Public Instance Methods

get_crm(json_string) click to toggle source
GET /crm

Yet to be tested

# File lib/megam/deccanplato/crms.rb, line 5
def get_crm(json_string)

  @options = {:path => "/provider/crm",
    :body => json_string}.merge(@options)

  request(
    :expects  => 200,
    :method   => :get,
    :body     => @options[:body]
  )
end
last_response() click to toggle source
# File lib/megam/deccanplato.rb, line 47
def last_response
  @last_response
end
post_crm(json_hash) click to toggle source

The body content needs to be a json.

# File lib/megam/deccanplato/crms.rb, line 18
def post_crm(json_hash)
  @options = {:path => '/provider/crm',
  :body => json_hash[:json]}.merge(@options)
   
  request(
    :expects  => 200,
    :method   => :post,
    :body     => @options[:body]
    )
end
request(params,&block) click to toggle source
# File lib/megam/deccanplato.rb, line 57
def request(params,&block)
  start = Time.now
  text.msg "#{text.color("START", :cyan, :bold)}"
  params.each do |pkey, pvalue|
    text.msg("> #{pkey}: #{pvalue}")
  end

  
  begin
    response = connection.request(params, &block)
    puts "=======response===="
    puts response
  rescue Excon::Errors::HTTPStatusError => error
    klass = case error.response.status

    when 401 then Megam::API::Errors::Unauthorized
    when 403 then Megam::API::Errors::Forbidden
    when 404 then Megam::API::Errors::NotFound
    when 408 then Megam::API::Errors::Timeout
    when 422 then Megam::API::Errors::RequestFailed
    when 423 then Megam::API::Errors::Locked
    when /50./ then Megam::API::Errors::RequestFailed
    else Megam::API::Errors::ErrorWithResponse
    end
    reerror = klass.new(error.message, error.response)
    reerror.set_backtrace(error.backtrace)
    text.msg "#{text.color("#{reerror.response.body}", :white)}"
    reerror.response.body = Megam::JSONCompat.from_json(reerror.response.body.chomp)
    text.msg("#{text.color("RESPONSE ERR: Ruby Object", :magenta, :bold)}")
    text.msg "#{text.color("#{reerror.response.body}", :white, :bold)}"
    raise(reerror)
  end

  @last_response = response
  text.msg("#{text.color("RESPONSE: HTTP Status and Header Data", :magenta, :bold)}")
  text.msg("> HTTP #{response.remote_ip} #{response.status}")

  response.headers.each do |header, value|
    text.msg("> #{header}: #{value}")
  end
  text.info("End HTTP Status/Header Data.")

  if response.body && !response.body.empty?
    if response.headers['Content-Encoding'] == 'gzip'
      response.body = Zlib::GzipReader.new(StringIO.new(response.body)).read
    end
    text.msg("#{text.color("RESPONSE: HTTP Body(JSON)", :magenta, :bold)}")

    text.msg "#{text.color("#{response.body}", :white)}"

    begin
      text.msg("#{text.color("RESPONSE: 2 -> Ruby ", :magenta, :bold)}")

      text.msg "#{text.color("#{response.body}", :white, :bold)}"
    rescue Exception => jsonerr
      text.error(jsonerr)
      raise(jsonerr)
     end
  end
  text.msg "#{text.color("END(#{(Time.now - start).to_s}s)", :blue, :bold)}"
  # reset (non-persistent) connection
  @connection.reset
  response.body
end

Private Instance Methods

connection() click to toggle source

Make a lazy connection.

# File lib/megam/deccanplato.rb, line 124
def connection
  @options[:path] =API_VERSION1+ @options[:path]
  @options[:headers] = HEADERS.merge({
         'X-Megam-Date' =>  Time.now.strftime("%Y-%m-%d %H:%M")
  }).merge(@options[:headers])

  text.info("HTTP Request Data:")
  text.msg("> HTTP #{@options[:scheme]}://#{@options[:host]}")
  @options.each do |key, value|
    text.msg("> #{key}: #{value}")
  end
  text.info("End HTTP Request Data.")
  @connection = Excon.new("#{@options[:scheme]}://#{@options[:host]}",@options)
end