nifty-utils 1.1.4 → 1.1.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/nifty/utils/http.rb +72 -0
- data/lib/nifty/utils/version.rb +1 -1
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4cd572d9cee5d232284ee6891b65d54322cc6e97
|
4
|
+
data.tar.gz: af7c8916f38893ca045c5fa08f68e3f282b1a973
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7dc0e5e08d837e9fc8bd9f41ab1f07d7d75b4cde6b113b08cfeca24154159cb94db5c35541fdeb5feaffc52e8c5528726a357309478725cd2693ff92cc01e00f
|
7
|
+
data.tar.gz: a40ba6cb5fc8cbb38508df409c6df1e748301f47ee93c37dc7609db300baf431b21a33a576aac1c4f2f23f3b23171fe30eda15f861970550f9d29e37a848c98a
|
@@ -0,0 +1,72 @@
|
|
1
|
+
require 'net/https'
|
2
|
+
require 'uri'
|
3
|
+
|
4
|
+
module Nifty
|
5
|
+
module Utils
|
6
|
+
module HTTP
|
7
|
+
|
8
|
+
def self.get(url, options = {})
|
9
|
+
request(Net::HTTP::Get, url, options)
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.post(url, options = {})
|
13
|
+
request(Net::HTTP::Post, url, options)
|
14
|
+
end
|
15
|
+
|
16
|
+
def self.request(method, url, options = {})
|
17
|
+
options[:headers] ||= {}
|
18
|
+
uri = URI.parse(url)
|
19
|
+
request = method.new(uri.path.length == 0 ? "/" : uri.path)
|
20
|
+
options[:headers].each { |k,v| request.add_field k, v }
|
21
|
+
|
22
|
+
if options[:username]
|
23
|
+
request.basic_auth(options[:username], options[:password])
|
24
|
+
end
|
25
|
+
|
26
|
+
if options[:params].is_a?(Hash)
|
27
|
+
# If params has been provided, sent it them as form encoded values
|
28
|
+
request.set_form_data(options[:params])
|
29
|
+
|
30
|
+
elsif options[:json].is_a?(String)
|
31
|
+
# If we have a JSON string, set the content type and body to be the JSON
|
32
|
+
# data
|
33
|
+
request.add_field 'Content-Type', 'application/json'
|
34
|
+
request.body = options[:json]
|
35
|
+
end
|
36
|
+
|
37
|
+
if options[:user_agent]
|
38
|
+
request['User-Agent'] = options[:user_agent]
|
39
|
+
end
|
40
|
+
|
41
|
+
connection = Net::HTTP.new(uri.host, uri.port)
|
42
|
+
|
43
|
+
if uri.scheme == 'https'
|
44
|
+
connection.use_ssl = true
|
45
|
+
connection.verify_mode = OpenSSL::SSL::VERIFY_PEER
|
46
|
+
end
|
47
|
+
|
48
|
+
begin
|
49
|
+
timeout = options[:timeout] || 60
|
50
|
+
Timeout.timeout(timeout) do
|
51
|
+
result = connection.request(request)
|
52
|
+
{
|
53
|
+
:code => result.code.to_i,
|
54
|
+
:body => result.body
|
55
|
+
}
|
56
|
+
end
|
57
|
+
rescue SocketError, Errno::ECONNRESET, EOFError, Errno::EINVAL => e
|
58
|
+
{
|
59
|
+
:code => -2,
|
60
|
+
:body => e.message
|
61
|
+
}
|
62
|
+
rescue Timeout::Error => e
|
63
|
+
{
|
64
|
+
:code => -1,
|
65
|
+
:body => "Timed out after #{timeout}s"
|
66
|
+
}
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
data/lib/nifty/utils/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: nifty-utils
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.1.
|
4
|
+
version: 1.1.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Adam Cooke
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-05-14 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: A set of useful utilties for Rails applications.
|
14
14
|
email:
|
@@ -25,6 +25,7 @@ files:
|
|
25
25
|
- lib/nifty/utils/extensions/core.rb
|
26
26
|
- lib/nifty/utils/extensions/object.rb
|
27
27
|
- lib/nifty/utils/extensions/string.rb
|
28
|
+
- lib/nifty/utils/http.rb
|
28
29
|
- lib/nifty/utils/networks.rb
|
29
30
|
- lib/nifty/utils/railtie.rb
|
30
31
|
- lib/nifty/utils/random_string.rb
|
@@ -57,3 +58,4 @@ signing_key:
|
|
57
58
|
specification_version: 4
|
58
59
|
summary: A collection of functions which expand upon ActiveRecord and ActionView.
|
59
60
|
test_files: []
|
61
|
+
has_rdoc:
|