simple_uri 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: ec702d34f2557e5de6826359b3ed063f1ec45ddd
4
+ data.tar.gz: e363ffc17f368545752ca46c4cac3f3a990115b6
5
+ SHA512:
6
+ metadata.gz: fd98e25200c345adf2cb110dcf58ce68d11b2ba49a818b9eb9365b92bb9b376ef8fdd44999146ecb149ece1dc5008477119b949083196004f37d6506e21acb81
7
+ data.tar.gz: b09024e3b7d317bcab29e18045b4ec2d49a03dca086fd71157db10f1189f3f86113f5bed6b182913fdc81c0ea3927068d268ad496a23b32cbb1fa42345744e00
@@ -0,0 +1,3 @@
1
+ module SimpleUri
2
+ VERSION = "0.0.1"
3
+ end
data/lib/simple_uri.rb ADDED
@@ -0,0 +1,73 @@
1
+ require 'logger'
2
+ require 'uri'
3
+ require 'net/http'
4
+ require 'openssl'
5
+ require 'active_support/inflector'
6
+
7
+ module SimpleUri
8
+
9
+ class << self
10
+
11
+ @@debug_mode = false
12
+
13
+ #parameters:url-String, method-Symbol||String, params-String, user-String, password:String, debug-Boolean
14
+ def connect(url: nil, method: nil, params: nil, user: nil, password: nil, debug: @@debug_mode)
15
+ enable_debug_mode(debug)
16
+ uri = URI.parse(URI.encode(url))
17
+ http = Net::HTTP.new(uri.host, uri.port)
18
+ http.read_timeout = 1000
19
+ debug_http(http)
20
+ if url.match(/^https/)
21
+ debug_msg 'Use SSL'
22
+ http.use_ssl = true
23
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE
24
+ end
25
+ req = "Net::HTTP::#{method.to_s.capitalize}".constantize.new(uri.path+params.to_s)
26
+ req.basic_auth user, password if user && password
27
+ [req, http]
28
+ end
29
+
30
+ #parameters:url-String, method-Symbol||String, params-String, req_body-String, req_headers-Hash, user-String, password:String, debug-Boolean, cookies-Boolean
31
+ def send_request(url: nil, method: nil, params: nil, req_body: nil, req_headers: nil, user: nil, password: nil, debug: @@debug_mode, cookies: false)
32
+ req, http = connect(url: url, method: method, params: params, user: user, password: password, debug: debug)
33
+ req.body = req_body
34
+ req_headers.each { |k, v| req[k] = v } if req_headers
35
+ res = http.request(req)
36
+ res.body
37
+ res_body = begin
38
+ JSON.parse(res.body)
39
+ rescue
40
+ debug_msg 'Can\'t convert response to JSON'
41
+ res.body
42
+ end
43
+ cookies ? { body: res_body, cookies: res.response['set-cookie'] } : res_body
44
+ end
45
+
46
+ private
47
+
48
+ def enable_debug_mode(enabled)
49
+ if enabled
50
+ @@debug_mode = true
51
+ @log = Logger.new(debug_output)
52
+ @log.level = Logger::DEBUG
53
+ end
54
+ end
55
+
56
+ def debug_msg(msg)
57
+ @log.debug(msg) if @log && @@debug_mode
58
+ end
59
+
60
+ def debug_http(http)
61
+ http.set_debug_output(debug_output) if @@debug_mode
62
+ end
63
+
64
+ def debug_output
65
+ STDOUT
66
+ end
67
+
68
+ def body_to_str_params(body)
69
+ body.map { |k, v| "#{k.to_s}=#{v.to_s}" }.join('&')
70
+ end
71
+ end
72
+
73
+ end
metadata ADDED
@@ -0,0 +1,45 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: simple_uri
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Nikulin Aleksander
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-10-15 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: To simplify working with url
14
+ email: nikulinaleksandr@gmail.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/simple_uri.rb
20
+ - lib/simple_uri/version.rb
21
+ homepage: https://github.com/niksan/simple_uri
22
+ licenses:
23
+ - MIT
24
+ metadata: {}
25
+ post_install_message:
26
+ rdoc_options: []
27
+ require_paths:
28
+ - lib
29
+ required_ruby_version: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ required_rubygems_version: !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - ">="
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ requirements: []
40
+ rubyforge_project:
41
+ rubygems_version: 2.2.2
42
+ signing_key:
43
+ specification_version: 4
44
+ summary: To simplify working with url
45
+ test_files: []