dpr 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. checksums.yaml +4 -4
  2. data/dpr.gemspec +1 -0
  3. data/lib/dpr.rb +47 -23
  4. data/lib/dpr/version.rb +1 -1
  5. metadata +16 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 340d3c1f250c235d63df4a230da9552942414530
4
- data.tar.gz: 15c76733ab1056805fbc83c5540569d2a4ce6f8b
3
+ metadata.gz: 35ddd4b930338c15a57577da1d70745f9c25bb9e
4
+ data.tar.gz: fae130cfc64bfd84d2a9b59b3a953528cbca17f0
5
5
  SHA512:
6
- metadata.gz: 2c53229289a6fd2a0f94c791af6c95c55a40a46a501c7fd224501e981d98c30b9aff62d1d5bf4296863089c799a0a0557b3f3715e344e88aeb70d01ebc238acf
7
- data.tar.gz: b2121ddbdc979c1fc3dc23afd45e31ad65016ad031898e61b95ae6ef43eb7c375ba081e3661a727092bb269705a4204540a0b4e2e3c1a9bd84c27a2b3fe373c3
6
+ metadata.gz: daa2f4a62272e942ccccfa09bb862de00e3b30a84d1c6a6b71f363f8f17ba50c575d0ad2c3bf418e831718508eca8493c3d503b30ae91c8b69b0fee14c3d11ab
7
+ data.tar.gz: f581606ff13ee461e200ab98f6d11120f36142b4f4c6675fae8daa4c2688e2cf0a5f7c4c5678ace5f12265f4c9ce9f198279438610314739db24636bc7ae7e1c
data/dpr.gemspec CHANGED
@@ -26,4 +26,5 @@ Gem::Specification.new do |spec|
26
26
  spec.add_development_dependency 'guard-rspec', '~> 4.6', '>= 4.6.3'
27
27
  spec.add_development_dependency 'pry', '>= 0.9.10', '< 0.11.0'
28
28
  spec.add_runtime_dependency "unirest", '~> 1.1', '>= 1.1.2'
29
+ spec.add_runtime_dependency "rest-client"
29
30
  end
data/lib/dpr.rb CHANGED
@@ -1,40 +1,64 @@
1
1
  require "dpr/version"
2
2
  require 'uri'
3
- require 'unirest'
3
+ require 'rest-client'
4
+ require 'json'
5
+ require 'addressable/uri'
4
6
 
5
7
  class String
6
- def http_get params: {}, headers: {}
7
- to_resp 'get', headers, params
8
+ def http_get params: {}, headers: {}, timeout: 10
9
+ to_resp method: :get, headers: headers, params: params, timeout: timeout
8
10
  end
9
11
 
10
- def http_post params: {}, headers: {}
11
- to_resp 'post', headers, params
12
+ def http_post params: {}, headers: {}, timeout: 10
13
+ to_resp method: :post, headers: headers, params: params, timeout: timeout
12
14
  end
13
15
 
14
- def http_delete params: {}, headers: {}
15
- to_resp 'delete', headers, params
16
+ def http_delete params: {}, headers: {}, timeout: 10
17
+ to_resp method: :delete, headers: headers, params: params, timeout: timeout
16
18
  end
17
19
 
18
- def http_put params: {}, headers: {}
19
- to_resp 'put', headers, params
20
+ def http_put params: {}, headers: {}, timeout: 10
21
+ to_resp method: :put, headers: headers, params: params, timeout: timeout
20
22
  end
21
23
 
22
- def http_patch params: {}, headers: {}
23
- to_resp 'patch', headers, params
24
+ def http_patch params: {}, headers: {}, timeout: 10
25
+ to_resp method: :patch, headers: headers, params: params, timeout: timeout
24
26
  end
25
27
 
26
- def valid_url?
27
- uri = URI.parse self
28
- uri.kind_of? URI::HTTP
29
- rescue URI::InvalidURIError
30
- false
31
- end
32
-
33
- private
34
- def to_resp http_method, headers, params
35
- if valid_url?
36
- response = Unirest.send http_method, self, headers: headers, parameters: params
37
- return response.body, response.code
28
+ def to_resp method: :get, headers: {}, params: {}, timeout: 10
29
+ if self =~ URI.regexp
30
+ begin
31
+ case method
32
+ when :get
33
+ url = self
34
+ if params.is_a?(Hash) && params.length > 0
35
+ if url.include? "?"
36
+ url += "&"
37
+ else
38
+ url += "?"
39
+ end
40
+ uri = Addressable::URI.new
41
+ uri.query_values = params
42
+ url += uri.query
43
+ url = url.gsub(/\s+/, '%20')
44
+ end
45
+ http_response = RestClient::Request.execute(:method => :get, :url => url, :headers => headers, :timeout => timeout)
46
+ when :post
47
+ http_response = RestClient::Request.execute(:method => :post, :url => self, :payload => params, :headers => headers, :timeout => timeout)
48
+ when :put
49
+ http_response = RestClient::Request.execute(:method => :put, :url => self, :payload => params, :headers => headers, :timeout => timeout)
50
+ when :delete
51
+ http_response = RestClient::Request.execute(:method => :delete, :url => self, :payload => params, :headers => headers, :timeout => timeout)
52
+ when :patch
53
+ http_response = RestClient::Request.execute(:method => :patch, :url => self, :payload => params, :headers => headers, :timeout => timeout)
54
+ end
55
+ rescue RestClient::RequestTimeout
56
+ raise 'Request Timeout'
57
+ rescue RestClient::Exception => e
58
+ http_response = e.response
59
+ end
60
+ # response = RestClient::Request.execute(method: http_method, url: self, headers: headers, parameters: params)
61
+ return JSON.parse(http_response.body), http_response.code
38
62
  else
39
63
  return {}, -1
40
64
  end
data/lib/dpr/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Dpr
2
- VERSION = "0.1.1"
2
+ VERSION = "0.1.2"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dpr
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - SudhagarS
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-03-13 00:00:00.000000000 Z
11
+ date: 2016-03-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -132,6 +132,20 @@ dependencies:
132
132
  - - ">="
133
133
  - !ruby/object:Gem::Version
134
134
  version: 1.1.2
135
+ - !ruby/object:Gem::Dependency
136
+ name: rest-client
137
+ requirement: !ruby/object:Gem::Requirement
138
+ requirements:
139
+ - - ">="
140
+ - !ruby/object:Gem::Version
141
+ version: '0'
142
+ type: :runtime
143
+ prerelease: false
144
+ version_requirements: !ruby/object:Gem::Requirement
145
+ requirements:
146
+ - - ">="
147
+ - !ruby/object:Gem::Version
148
+ version: '0'
135
149
  description: The simplest Ruby HTTP client. Dpr lets you do HTTP directly on URL strings.
136
150
  email:
137
151
  - sudhagar@isudhagar.in