uri-requests 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/uri_requests.rb +74 -0
  3. metadata +100 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 8a8987f08fce58ed8208cca7bc1df6d70ef85310
4
+ data.tar.gz: 2a4354c422f1f478ae0304f896917d76edf6d6e5
5
+ SHA512:
6
+ metadata.gz: 30d9b1fd25dd5b40fedc9ad4b0df2bde70f8de371e8ba4ac4e403af488a121144381cf3c18074f43feca73a152c10a39e05ea35053ebd5d415678327d23e64d4
7
+ data.tar.gz: 8ac0502532e33e7ef1e25d618be22ece75793e9b483269866ff4fadb4d955b06992c6951194cb51f0e05489f1e95865bce1a053829c9b729f818db00c73d7f46
@@ -0,0 +1,74 @@
1
+ require 'json'
2
+ require 'net/http'
3
+ require 'uri'
4
+ require 'byebug'
5
+
6
+
7
+ module UriRequests
8
+ VERSION = '0.0.1'
9
+
10
+
11
+ def get data = nil, opts = {}
12
+ data ||= {}
13
+ uri = clone
14
+
15
+ unless data.is_a? Hash
16
+ # if data is scalar, kindly repackage
17
+ data = { data => nil }
18
+ end
19
+
20
+ unless data.empty?
21
+ uri.query = URI.encode_www_form(data)
22
+ end
23
+
24
+ request Net::HTTP::Get.new(uri), **opts
25
+ end
26
+
27
+
28
+ def post data = nil, opts = {}
29
+ data ||= {}
30
+ opts = opts.clone
31
+
32
+ req = Net::HTTP::Post.new itself
33
+
34
+ if opts.delete(:json)
35
+ req['Content-Type'] = 'application/json'
36
+ req.body = data.to_json
37
+ else
38
+ req.set_form_data data
39
+ end
40
+
41
+ request req, **opts
42
+ end
43
+
44
+
45
+ private
46
+
47
+ def request request, opts = {}
48
+ use_ssl = scheme == 'https'
49
+ res = Net::HTTP.start(host, port, use_ssl: use_ssl) do |http|
50
+ # set headers
51
+ (opts[:headers] or {}).each {|k,v| request[k] = v}
52
+
53
+ # set other request options
54
+ opts.each {|k,v| http.send k, v if k != :headers }
55
+
56
+ http.request request
57
+ end
58
+
59
+ unless res.is_a?(Net::HTTPSuccess)
60
+ raise Net::HTTPError.new "#{res.code}: #{res.message}\n#{res.body}", res
61
+ end
62
+
63
+ if 'application/json' == res['content-type']
64
+ JSON::parse res.body
65
+ else
66
+ res.body
67
+ end
68
+ end
69
+
70
+
71
+ end
72
+
73
+
74
+ URI::HTTP.include UriRequests
metadata ADDED
@@ -0,0 +1,100 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: uri-requests
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Daniel Pepper
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-01-13 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rake
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '10'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '10'
27
+ - !ruby/object:Gem::Dependency
28
+ name: minitest
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '5'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '5'
41
+ - !ruby/object:Gem::Dependency
42
+ name: byebug
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '9'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '9'
55
+ - !ruby/object:Gem::Dependency
56
+ name: awesome_print
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1'
69
+ description: URI::HTTP requests made easier
70
+ email:
71
+ executables: []
72
+ extensions: []
73
+ extra_rdoc_files: []
74
+ files:
75
+ - lib/uri_requests.rb
76
+ homepage: https://github.com/dpep/rb_uri_requests
77
+ licenses:
78
+ - MIT
79
+ metadata: {}
80
+ post_install_message:
81
+ rdoc_options: []
82
+ require_paths:
83
+ - lib
84
+ required_ruby_version: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ required_rubygems_version: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ requirements: []
95
+ rubyforge_project:
96
+ rubygems_version: 2.6.11
97
+ signing_key:
98
+ specification_version: 4
99
+ summary: UriRequests
100
+ test_files: []