Module: SocialHubHttpUtility

Defined in:
lib/socialHub_httpUtility.rb,
lib/socialHub_httpUtility/version.rb

Constant Summary collapse

VERSION =
"1.0.14"

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.configurationObject

Returns the value of attribute configuration.



7
8
9
# File 'lib/socialHub_httpUtility.rb', line 7

def configuration
  @configuration
end

Class Method Details

.configure {|configuration| ... } ⇒ Object

Yields:



18
19
20
# File 'lib/socialHub_httpUtility.rb', line 18

def self.configure
  yield(configuration)
end

.get_http_request(domain, path, params, headers, distination_service) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
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
# File 'lib/socialHub_httpUtility.rb', line 22

def self.get_http_request(domain, path, params, headers, distination_service)
    
    if(configuration.service_name)
      puts "service name from the ruby gem is"
      puts configuration.service_name
    end

    url = domain+path
    http_end_index = url.index(':')
    http_type = url[0 , http_end_index]
    uri = URI.parse(url)
    uri.query = URI.encode_www_form( params)
    http = Net::HTTP.new(uri.host, uri.port)
    http.use_ssl = (http_type == 'https')
    request = Net::HTTP::Get.new(uri.request_uri)
    
    headers.each do |key,value|
      request.add_field(key, value)        
    end

    if(!headers.key?("request-id"))
      request.add_field("request-id",SecureRandom.uuid)
    end
    log_params = { 
      "service_name" => params["service_name"],
      "http_url"     => url,
      "http_request_params" => params,
      "http_request_type"   => "GET"
    }

    response = {}
     = {}
    begin
      res = http.request(request)
      case res
        when Net::HTTPSuccess then
          ["statusCode"] = 1  
          ["reason"] = "SUCCESS"
          response["metadata"] = 
          response["http_response"] = res

        when Net::HTTPMethodNotAllowed then 
           ["statusCode"] = 0
           ["reason"] = "Method NOT ALLOWED FOR THIS URL >> Check The Method Type "
           response["metadata"] = 
           response["http_response"] = {}

        when Net::HTTPNotFound then 
           ["statusCode"] = 0
           ["reason"] = "THERE IS NO SUCH A METHOD  >> Check That The Server Has This Method"
           response["metadata"] = 
           response["http_response"] = {}
        end
      
      begin
        log(log_params, distination_service, )
      rescue 
      end

    rescue Timeout::Error, Errno::EINVAL, Errno::ECONNRESET, EOFError,
     Net::HTTPBadResponse, Net::HTTPHeaderSyntaxError, Net::ProtocolError,Errno::ECONNREFUSED => e
       ["statusCode"] = -1
       ["reason"] = e.message
       response["metadata"] = 
       response["http_response"] = {}
       puts "Error during processing: #{$!}"
       puts "Backtrace:\n\t#{e.backtrace.join("\n\t")}"
       begin
         log(log_params, distination_service, )
       rescue 
       end
      
    end

    
    return response 
end

.post_http_request(domain, path, params, headers, distination_service) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/socialHub_httpUtility.rb', line 100

def self.post_http_request(domain, path, params, headers, distination_service)
    
    if(configuration.service_name)
      puts "service name from the ruby gem is"
      puts configuration.service_name
    end

    url = domain+path
    http_end_index = url.index(':')
    http_type = url[0 , http_end_index]
    uri = URI.parse(url)
    http = Net::HTTP.new(uri.host, uri.port)
    http.use_ssl = (http_type == 'https')
    post_data = URI.encode_www_form(params)
    if(!headers.key?("request-id"))
      headers["request-id"] = SecureRandom.uuid
    end
     log_params = { 
      "service_name" => params["service_name"],
      "http_url"     => url,
      "http_request_params" => params,
      "http_request_type"   => "POST"
    }
    response = {}
     = {}
    begin
      res = http.post(uri.path, post_data, headers)
      case res
        when Net::HTTPSuccess then
          ["statusCode"] = 1
          ["reason"] = "SUCCESS"
          response["metadata"] = 
          response["http_response"] = res

        when Net::HTTPMethodNotAllowed then 
           ["statusCode"] = 0
           ["reason"] = "Method NOT ALLOWED FOR THIS URL >> Check The Method Type "
           response["metadata"] = 
           response["http_response"] = {}

        when Net::HTTPNotFound then 
           ["statusCode"] = 0
           ["reason"] = "THERE IS NO SUCH A METHOD  >> Check That The Server Has This Method"
           response["metadata"] = 
           response["http_response"] = {}
        end
              
      begin
        log(log_params, distination_service, )
      rescue 
      end      
    
    rescue Timeout::Error, Errno::EINVAL, Errno::ECONNRESET, EOFError,
      Net::HTTPBadResponse , Net::HTTPHeaderSyntaxError ,Net::ProtocolError,Errno::ECONNREFUSED => e
      
      ["statusCode"] = -1
      ["reason"] = e.message
      response["metadata"] = 
      response["http_response"] = {}
      puts "Error during processing: #{$!}"
      puts "Backtrace:\n\t#{e.backtrace.join("\n\t")}"
      begin
         log(log_params, distination_service, )
      rescue 
     end  
    end    
   
  return response
end

.resetObject



14
15
16
# File 'lib/socialHub_httpUtility.rb', line 14

def self.reset
  @configuration = Configuration.new
end

Instance Method Details

#log(log_params, distination_service, response_meta_data) ⇒ Object



170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/socialHub_httpUtility.rb', line 170

def log (log_params, distination_service, )
    uri = URI.parse("http://localhost:8186/MonitoringService/storeLog")
    time = Time.now
    log_params["log_time"] =  time.to_s
    log_params["log_time_unix"] = time.to_i   

    if ["statusCode"] == 1
      log_params["log_message"] = "Successed to get Http Response from " + distination_service
      log_params["http_response"] = "SUCCESS"
    elsif (["statusCode"] == -1 || ["statusCode"] == 0 )
      log_params["log_message"] = "Failed to get Http Response from " + distination_service  
      log_params["http_response"] = "FAILED"                       
      log_params["http_response_reason"] = ["reason"]   
    end
    
    http = Net::HTTP.new(uri.host, uri.port)
    post_data = URI.encode_www_form(log_params)
    res = http.post(uri.path, post_data)

end