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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f20532eea9d248ad0a3a09a1f38fec0aee429e8a
4
- data.tar.gz: eedd6b1ab92a02888509016854a7d1dede571f82
3
+ metadata.gz: 0471e41fb140abe4e37fd587b465378a0bddf68a
4
+ data.tar.gz: 52237b1cdf91df4ceafc3b5d63b610af815dc5c6
5
5
  SHA512:
6
- metadata.gz: b1789c6831bf3fd7e3624b1edd77e1840a5e9a474fa6ab6439d6a7320ade187945a8f2b023ca0d1467be0c8e73702e503afcf3c9ce905f7f9d9cde8e0dd84fd8
7
- data.tar.gz: d0c74437e400de8032969f035b9749e7e0e4e4acd40a55213b1d6219655c250e75b1d0b304d6f0bb38df3c5d33eedc4023e6d4bb47d94a70b148b76515e4aa48
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
@@ -1,4 +1,4 @@
1
1
  module PadUtils
2
2
  # PadUtils version number
3
- VERSION = "1.8.0"
3
+ VERSION = "1.9.0"
4
4
  end
data/lib/pad_utils.rb CHANGED
@@ -8,6 +8,7 @@ require_relative "pad_utils/pad_json"
8
8
  require_relative "pad_utils/pad_color"
9
9
  require_relative "pad_utils/pad_code"
10
10
  require_relative "pad_utils/pad_security"
11
+ require_relative "pad_utils/pad_http"
11
12
 
12
13
  # Main namespace for PadUtils.
13
14
  #
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.8.0
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-05 00:00:00.000000000 Z
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 [Padstone](http://padstone.io) app builder but can be embedded in
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