Class: Megam::Deccanplato

Inherits:
Object
  • Object
show all
Defined in:
lib/megam/deccanplato.rb,
lib/megam/deccanplato/crms.rb,
lib/megam/deccanplato/version.rb

Constant Summary collapse

HEADERS =
{
  'Accept' => 'application/json',
  'Accept-Encoding' => 'gzip',
  'User-Agent' => "megam-deccanplato/#{Megam::Deccanplato::VERSION}",
  'X-Ruby-Version' => RUBY_VERSION,
  'X-Ruby-Platform' => RUBY_PLATFORM
}
OPTIONS =
{
  :headers => {},
  :host => 'localhost',
  :port => '8080',
  :nonblock => false,
  :scheme => 'http'
}
API_VERSION1 =
"/deccanplato"
VERSION =
"0.1.0"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Deccanplato

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



52
53
54
55
# File 'lib/megam/deccanplato.rb', line 52

def initialize(options={})
  puts "initiliaze entry"
  @options = OPTIONS.merge(options)  
end

Instance Attribute Details

#textObject

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



23
24
25
# File 'lib/megam/deccanplato.rb', line 23

def text
  @text
end

Instance Method Details

#get_crm(json_string) ⇒ Object

GET /crm Yet to be tested



5
6
7
8
9
10
11
12
13
14
15
# 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_responseObject



47
48
49
# File 'lib/megam/deccanplato.rb', line 47

def last_response
  @last_response
end

#post_crm(json_hash) ⇒ Object

The body content needs to be a json.



18
19
20
21
22
23
24
25
26
27
# 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) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# 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