pad_utils 1.8.0 → 1.9.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +0 -5
- data/lib/pad_utils/pad_http.rb +85 -0
- data/lib/pad_utils/version.rb +1 -1
- data/lib/pad_utils.rb +1 -0
- metadata +24 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0471e41fb140abe4e37fd587b465378a0bddf68a
|
4
|
+
data.tar.gz: 52237b1cdf91df4ceafc3b5d63b610af815dc5c6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: edabee76a841be4a573f42fd33be76bcffcae36e3577f8aa0da113726a318a82f425052273ddd9a12eea4496a471b87c02af3214107755f33d88f2150ab68508
|
7
|
+
data.tar.gz: 659c69afea1249b8d26ff77daf479b98e7095e1c7c5a155cc26e7244681f72fad591431aec0bcb439128945910a794ea8244222b61e154443d6ba9c78506c0ad
|
data/README.md
CHANGED
@@ -21,11 +21,6 @@ PadUtils is fully documented at [padutils.padstone.io](http://padutils.padstone.
|
|
21
21
|
[Get in touch](https://twitter.com/nicoschuele) before submitting a pull request, I don't want to waste your time by rejecting it!
|
22
22
|
|
23
23
|
|
24
|
-
## Testing
|
25
|
-
|
26
|
-
The test suite for PadUtils is currently outside of the gem itself. I may add it if I have time.
|
27
|
-
|
28
|
-
|
29
24
|
## License
|
30
25
|
|
31
26
|
Copyright 2016 - Nico Schuele
|
@@ -0,0 +1,85 @@
|
|
1
|
+
require 'httparty'
|
2
|
+
require 'json'
|
3
|
+
module PadUtils
|
4
|
+
|
5
|
+
# Sends a HTTP POST request.
|
6
|
+
#
|
7
|
+
# @note Although it can parse any JSON responses, this method is made to work
|
8
|
+
# in conjunction with PadUtils on the server side code.
|
9
|
+
#
|
10
|
+
# If the server can't be reached, `{error: "Server unreachable"}` is returned.
|
11
|
+
#
|
12
|
+
# If the JSON in the response can't be parsed (or if it's not JSON),
|
13
|
+
# `{error: "Response is not JSON"}` is returned.
|
14
|
+
#
|
15
|
+
# @param url [String] the url to call
|
16
|
+
# @param body [Hash] the hash containing the key/value pairs to send
|
17
|
+
# @param headers [Hash] the hash containing the header key/value pairs
|
18
|
+
# @return [Hash] the response as a hash
|
19
|
+
# @example
|
20
|
+
# url = "http://example.com/api/movies"
|
21
|
+
# body = {year: 2008, director: "Nolan"}
|
22
|
+
# PadUtils.http_post(url: url, body: body) # => {movie: "The Dark Knight"}
|
23
|
+
def self.http_post(url: "", body: {}, headers: {'User-Agent' => 'Padstone'})
|
24
|
+
reply_hash = {}
|
25
|
+
body = PadUtils.hash_to_json(body)
|
26
|
+
reply = HTTParty.post(url, {headers: headers, body: body}).to_s
|
27
|
+
reply_hash = PadUtils.json_to_hash reply
|
28
|
+
reply_hash
|
29
|
+
rescue JSON::ParserError => e
|
30
|
+
reply_hash = {error: "Response is not JSON"}
|
31
|
+
rescue Errno::ECONNREFUSED => e
|
32
|
+
reply_hash = {error: "Server unreachable"}
|
33
|
+
rescue Exception => e
|
34
|
+
reply_hash = {error: e.message}
|
35
|
+
end
|
36
|
+
|
37
|
+
# Sends a HTTP GET request and receives plain text.
|
38
|
+
#
|
39
|
+
#
|
40
|
+
# If the server can't be reached, `"Server unreachable"` is returned.
|
41
|
+
#
|
42
|
+
# @param url [String] the url to call
|
43
|
+
# @param headers [Hash] the hash containing the header key/value pairs
|
44
|
+
# @return [String] the response
|
45
|
+
# @example
|
46
|
+
# url = "http://example.com/api/joke"
|
47
|
+
# PadUtils.http_get_plain(url) # => "There are 10 kinds of programmers."
|
48
|
+
def self.http_get_plain(url, headers: {'User-Agent' => 'Padstone'})
|
49
|
+
reply = HTTParty.get(url, headers: headers).to_s
|
50
|
+
rescue Errno::ECONNREFUSED => e
|
51
|
+
reply = "Server unreachable"
|
52
|
+
rescue Exception => e
|
53
|
+
reply = e.message
|
54
|
+
end
|
55
|
+
|
56
|
+
# Sends a HTTP GET request and receives a hash.
|
57
|
+
#
|
58
|
+
# @note Although it can parse any JSON responses, this method is made to work
|
59
|
+
# in conjunction with PadUtils on the server side code.
|
60
|
+
#
|
61
|
+
# If the server can't be reached, `{error: "Server unreachable"}` is returned.
|
62
|
+
#
|
63
|
+
# If the JSON in the response can't be parsed (or if it's not JSON),
|
64
|
+
# `{error: "Response is not JSON"}` is returned.
|
65
|
+
#
|
66
|
+
# @param url [String] the url to call
|
67
|
+
# @param headers [Hash] the hash containing the header key/value pairs
|
68
|
+
# @return [Hash] the response as a hash
|
69
|
+
# @example
|
70
|
+
# url = "http://example.com/api/weather"
|
71
|
+
# PadUtils.http_get(url) # => {Geneva: "Sunny", Lausanne: "Cloudy"}
|
72
|
+
def self.http_get(url, headers: {'User-Agent' => 'Padstone'})
|
73
|
+
reply_hash = {}
|
74
|
+
reply = HTTParty.get(url, headers: headers).to_s
|
75
|
+
reply_hash = PadUtils.json_to_hash reply
|
76
|
+
reply_hash
|
77
|
+
rescue JSON::ParserError => e
|
78
|
+
reply_hash = {error: "Response is not JSON"}
|
79
|
+
rescue Errno::ECONNREFUSED => e
|
80
|
+
reply_hash = {error: "Server unreachable"}
|
81
|
+
rescue Exception => e
|
82
|
+
reply_hash = {error: e.message}
|
83
|
+
end
|
84
|
+
|
85
|
+
end
|
data/lib/pad_utils/version.rb
CHANGED
data/lib/pad_utils.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: pad_utils
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.9.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Nico Schuele
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-03-
|
11
|
+
date: 2016-03-06 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: json
|
@@ -30,9 +30,28 @@ dependencies:
|
|
30
30
|
- - "<"
|
31
31
|
- !ruby/object:Gem::Version
|
32
32
|
version: '1.9'
|
33
|
+
- !ruby/object:Gem::Dependency
|
34
|
+
name: httparty
|
35
|
+
requirement: !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
37
|
+
- - ">="
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: 0.13.7
|
40
|
+
- - "<"
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
version: '0.14'
|
43
|
+
type: :runtime
|
44
|
+
prerelease: false
|
45
|
+
version_requirements: !ruby/object:Gem::Requirement
|
46
|
+
requirements:
|
47
|
+
- - ">="
|
48
|
+
- !ruby/object:Gem::Version
|
49
|
+
version: 0.13.7
|
50
|
+
- - "<"
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
version: '0.14'
|
33
53
|
description: PadUtils is a simple gem containing common utilities and shortcuts. It
|
34
|
-
is used in the
|
35
|
-
any other Ruby project.
|
54
|
+
is used in the Padstone app builder but can be embedded in any other Ruby project.
|
36
55
|
email:
|
37
56
|
- help@padstone.io
|
38
57
|
executables:
|
@@ -48,6 +67,7 @@ files:
|
|
48
67
|
- lib/pad_utils/pad_code.rb
|
49
68
|
- lib/pad_utils/pad_color.rb
|
50
69
|
- lib/pad_utils/pad_files.rb
|
70
|
+
- lib/pad_utils/pad_http.rb
|
51
71
|
- lib/pad_utils/pad_json.rb
|
52
72
|
- lib/pad_utils/pad_logger.rb
|
53
73
|
- lib/pad_utils/pad_menu.rb
|