easyhttp 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,18 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'easyhttp/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "easyhttp"
8
+ gem.version = Easyhttp::VERSION
9
+ gem.authors = "Srinivas Prabhu Ramanna"
10
+ gem.email = "Srinivas.prabhur@gmail.com"
11
+ gem.description = "This is a wrapper around ruby http lib"
12
+ gem.summary = "Hides the complexity and provides a simple interface to interact with ruby http class"
13
+ gem.homepage = ""
14
+
15
+ gem.files = %w(easyhttp.gemspec)
16
+ gem.files += Dir.glob("lib/**/*.rb")
17
+ gem.files += Dir.glob("lib/**/*.yaml")
18
+ end
@@ -0,0 +1,71 @@
1
+ require 'rubygems'
2
+ require File.dirname(__FILE__)+'/easyhttp/version'
3
+ require File.dirname(__FILE__)+'/easyhttp/headers'
4
+ require File.dirname(__FILE__)+'/easyhttp/log'
5
+ require File.dirname(__FILE__)+'/easyhttp/web'
6
+
7
+
8
+
9
+ module Easyhttp
10
+ class << self
11
+ include Log
12
+ include Headers
13
+ include Web
14
+
15
+ def Easyhttp.get url,headers=$default_headers
16
+ $log.info "GET - #{url}"
17
+ $log.debug "Headers - #{headers.inspect}"
18
+ begin
19
+ res = http_methods("GET",url,headers)
20
+ catch_redirections(res)
21
+ return res
22
+ rescue => e
23
+ raise "#{e.message}\n#{e.backtrace.join("\n\t")}"
24
+ end
25
+ end
26
+
27
+ def Easyhttp.put url,payload,headers=$default_headers
28
+ $log.info "PUT - #{url}"
29
+ $log.debug "Headers - #{headers.inspect}"
30
+ begin
31
+ res = http_methods("GET",url,headers,payload)
32
+ return res
33
+ rescue => e
34
+ raise "#{e.message}\n#{e.backtrace.join("\n\t")}"
35
+ end
36
+ end
37
+
38
+ def Easyhttp.post url,payload,headers=$default_headers
39
+ $log.info "POST - #{url}"
40
+ $log.debug "Headers - #{headers.inspect}"
41
+ begin
42
+ res = http_methods("POST",url,headers,payload)
43
+ return res
44
+ rescue => e
45
+ raise "#{e.message}\n#{e.backtrace.join("\n\t")}"
46
+ end
47
+ end
48
+
49
+ def Easyhttp.delete url,headers=$default_headers
50
+ $log.info "DELETE - #{url}"
51
+ $log.debug "Headers - #{headers.inspect}"
52
+ begin
53
+ res = http_methods("DELETE",url,headers)
54
+ return res
55
+ rescue => e
56
+ raise "#{e.message}\n#{e.backtrace.join("\n\t")}"
57
+ end
58
+ end
59
+
60
+ def Easyhttp.setLogLevel level
61
+ if level == "INFO"
62
+ $log.level = Logger::INFO
63
+ elsif level == "DEBUG"
64
+ $log.level = Logger::DEBUG
65
+ elsif level == "WARN"
66
+ $log.level = Logger::WARN
67
+ end
68
+ end
69
+ end
70
+
71
+ end
@@ -0,0 +1,7 @@
1
+ module Easyhttp
2
+ module Headers
3
+ $default_headers = {}
4
+ # {'Content-Type' => "text/plain;application/json;text/xml;text/html",'Accept'=>"text/plain;application/json;text/xml;text/html"}
5
+
6
+ end
7
+ end
@@ -0,0 +1,9 @@
1
+ require 'rubygems'
2
+ require 'logger'
3
+
4
+ module Easyhttp
5
+ module Log
6
+ $log = Logger.new(STDOUT)
7
+ $log.level = Logger::INFO
8
+ end
9
+ end
@@ -0,0 +1,3 @@
1
+ module Easyhttp
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,48 @@
1
+ require 'rubygems'
2
+ require 'net/http'
3
+ require 'net/https'
4
+ require 'uri'
5
+
6
+ module Easyhttp
7
+ module Web
8
+ private
9
+ def http_methods(method,url,headers,payload=nil)
10
+ uri = URI.parse(url)
11
+ http = Net::HTTP.new(uri.host,uri.port)
12
+ if url =~ /^https\:\/\//
13
+ $log.info "Enabling ssl"
14
+ http.use_ssl = true
15
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE
16
+ end
17
+
18
+ case method
19
+ when "GET"
20
+ if(uri.query)
21
+ urlpath = uri.path+"?"+uri.query
22
+ else
23
+ urlpath = uri.path
24
+ end
25
+ response = http.get(urlpath,headers)
26
+ return response
27
+ when "POST"
28
+ response = http.post(uri.path,payload,headers)
29
+ return response
30
+ when "PUT"
31
+ response = http.put(uri.path,payload,headers)
32
+ return response
33
+ when "DELETE"
34
+ response = http.delete(uri.path,headers)
35
+ return response
36
+ end
37
+ end
38
+
39
+ private
40
+ def catch_redirections http_response
41
+ if http_response.class.name == "Net::HTTPRedirection"
42
+ location = http_response.to_hash['location']
43
+ $log.info "redirected to #{location}"
44
+ end
45
+ return
46
+ end
47
+ end
48
+ end
metadata ADDED
@@ -0,0 +1,70 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: easyhttp
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Srinivas Prabhu Ramanna
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2013-09-24 00:00:00 Z
19
+ dependencies: []
20
+
21
+ description: This is a wrapper around ruby http lib
22
+ email: Srinivas.prabhur@gmail.com
23
+ executables: []
24
+
25
+ extensions: []
26
+
27
+ extra_rdoc_files: []
28
+
29
+ files:
30
+ - easyhttp.gemspec
31
+ - lib/easyhttp/headers.rb
32
+ - lib/easyhttp/log.rb
33
+ - lib/easyhttp/version.rb
34
+ - lib/easyhttp/web.rb
35
+ - lib/easyhttp.rb
36
+ homepage: ""
37
+ licenses: []
38
+
39
+ post_install_message:
40
+ rdoc_options: []
41
+
42
+ require_paths:
43
+ - lib
44
+ required_ruby_version: !ruby/object:Gem::Requirement
45
+ none: false
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ hash: 3
50
+ segments:
51
+ - 0
52
+ version: "0"
53
+ required_rubygems_version: !ruby/object:Gem::Requirement
54
+ none: false
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ hash: 3
59
+ segments:
60
+ - 0
61
+ version: "0"
62
+ requirements: []
63
+
64
+ rubyforge_project:
65
+ rubygems_version: 1.8.25
66
+ signing_key:
67
+ specification_version: 3
68
+ summary: Hides the complexity and provides a simple interface to interact with ruby http class
69
+ test_files: []
70
+