unirest 1.0.8
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/lib/unirest.rb +97 -0
- data/lib/unirest/http_request.rb +55 -0
- data/lib/unirest/http_response.rb +50 -0
- metadata +77 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA512:
|
3
|
+
data.tar.gz: dade6579029f5e60820341ee1df600a1a94565ae1daef650b48ff64e6fbd5e3389e8128f562c6031e9c77b34e98217f77faa6951509ce969051b68123658b2e3
|
4
|
+
metadata.gz: 7ff5a90e3009a9384df549dd143d3f64db89fa1c51616735f355089c5655824669fa8731ebfe4dbadbb6aae0b2711bf724ac2df95194ba00fc119371587a370b
|
5
|
+
SHA1:
|
6
|
+
data.tar.gz: 16e33f73fdbc1122f86c678581e9fd1bd36cb014
|
7
|
+
metadata.gz: ea7caa8647f337ccbc9e8a1976702c166a991838
|
data/lib/unirest.rb
ADDED
@@ -0,0 +1,97 @@
|
|
1
|
+
# The MIT License
|
2
|
+
#
|
3
|
+
# Copyright (c) 2013 Mashape (http://mashape.com)
|
4
|
+
|
5
|
+
# Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
# a copy of this software and associated documentation files (the
|
7
|
+
# "Software"), to deal in the Software without restriction, including
|
8
|
+
# without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
# distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
# permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
# the following conditions:
|
12
|
+
#
|
13
|
+
# The above copyright notice and this permission notice shall be
|
14
|
+
# included in all copies or substantial portions of the Software.
|
15
|
+
#
|
16
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
23
|
+
#
|
24
|
+
|
25
|
+
require 'rubygems'
|
26
|
+
require 'rest-client'
|
27
|
+
|
28
|
+
require File.join(File.dirname(__FILE__), "/unirest/http_request.rb")
|
29
|
+
require File.join(File.dirname(__FILE__), "/unirest/http_response.rb")
|
30
|
+
|
31
|
+
module Unirest
|
32
|
+
|
33
|
+
USER_AGENT = "unirest-ruby/1.0"
|
34
|
+
|
35
|
+
class HttpClient
|
36
|
+
|
37
|
+
def self.request(method, url, headers, body, &callback)
|
38
|
+
http_request = Unirest::HttpRequest.new(method, url, headers, body)
|
39
|
+
|
40
|
+
if callback
|
41
|
+
return Thread.new do
|
42
|
+
callback.call(self.internal_request(http_request))
|
43
|
+
end
|
44
|
+
else
|
45
|
+
return self.internal_request(http_request)
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
def self.internal_request(http_request)
|
50
|
+
# Set the user agent
|
51
|
+
http_request.add_header("user-agent", USER_AGENT)
|
52
|
+
|
53
|
+
http_response = nil;
|
54
|
+
|
55
|
+
begin
|
56
|
+
case http_request.method
|
57
|
+
when :get
|
58
|
+
http_response = RestClient.get http_request.url, http_request.headers
|
59
|
+
when :post
|
60
|
+
http_response = RestClient.post http_request.url, http_request.body, http_request.headers
|
61
|
+
when :put
|
62
|
+
http_response = RestClient.put http_request.url, http_request.body, http_request.headers
|
63
|
+
when :delete
|
64
|
+
http_response = RestClient.delete http_request.url, http_request.headers
|
65
|
+
when :patch
|
66
|
+
http_response = RestClient.patch http_request.url, http_request.body, http_request.headers
|
67
|
+
end
|
68
|
+
rescue => e
|
69
|
+
http_response = e.response
|
70
|
+
end
|
71
|
+
|
72
|
+
return Unirest::HttpResponse.new(http_response)
|
73
|
+
end
|
74
|
+
|
75
|
+
end
|
76
|
+
|
77
|
+
def self.get(url, headers = {}, &callback)
|
78
|
+
return HttpClient.request(:get, url, headers, nil, &callback)
|
79
|
+
end
|
80
|
+
|
81
|
+
def self.post(url, headers = {}, body = nil, &callback)
|
82
|
+
return HttpClient.request(:post, url, headers, body, &callback)
|
83
|
+
end
|
84
|
+
|
85
|
+
def self.delete(url, headers = {}, &callback)
|
86
|
+
return HttpClient.request(:delete, url, headers, nil, &callback)
|
87
|
+
end
|
88
|
+
|
89
|
+
def self.put(url, headers = {}, body = nil, &callback)
|
90
|
+
return HttpClient.request(:put, url, headers, body, &callback)
|
91
|
+
end
|
92
|
+
|
93
|
+
def self.patch(url, headers = {}, body = nil, &callback)
|
94
|
+
return HttpClient.request(:patch, url, headers, body, &callback)
|
95
|
+
end
|
96
|
+
|
97
|
+
end
|
@@ -0,0 +1,55 @@
|
|
1
|
+
# The MIT License
|
2
|
+
#
|
3
|
+
# Copyright (c) 2013 Mashape (http://mashape.com)
|
4
|
+
|
5
|
+
# Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
# a copy of this software and associated documentation files (the
|
7
|
+
# "Software"), to deal in the Software without restriction, including
|
8
|
+
# without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
# distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
# permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
# the following conditions:
|
12
|
+
#
|
13
|
+
# The above copyright notice and this permission notice shall be
|
14
|
+
# included in all copies or substantial portions of the Software.
|
15
|
+
#
|
16
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
23
|
+
#
|
24
|
+
|
25
|
+
require 'addressable/uri'
|
26
|
+
|
27
|
+
module Unirest
|
28
|
+
|
29
|
+
class HttpRequest
|
30
|
+
attr_reader :method
|
31
|
+
attr_reader :url
|
32
|
+
attr_reader :headers
|
33
|
+
attr_reader :body
|
34
|
+
|
35
|
+
def add_header(name, value)
|
36
|
+
@headers[name] = value
|
37
|
+
end
|
38
|
+
|
39
|
+
def initialize(method, url, headers = {}, body = nil)
|
40
|
+
@method = method
|
41
|
+
|
42
|
+
unless url =~ URI::regexp
|
43
|
+
raise "Invalid URL: " + url
|
44
|
+
end
|
45
|
+
|
46
|
+
@url = URI.escape(url)
|
47
|
+
@headers = {}
|
48
|
+
# Make the header key lowercase
|
49
|
+
headers.each_pair {|key, value| @headers[key.downcase] = value }
|
50
|
+
@body = body
|
51
|
+
end
|
52
|
+
|
53
|
+
end
|
54
|
+
|
55
|
+
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
# The MIT License
|
2
|
+
#
|
3
|
+
# Copyright (c) 2013 Mashape (http://mashape.com)
|
4
|
+
|
5
|
+
# Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
# a copy of this software and associated documentation files (the
|
7
|
+
# "Software"), to deal in the Software without restriction, including
|
8
|
+
# without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
# distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
# permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
# the following conditions:
|
12
|
+
#
|
13
|
+
# The above copyright notice and this permission notice shall be
|
14
|
+
# included in all copies or substantial portions of the Software.
|
15
|
+
#
|
16
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
23
|
+
#
|
24
|
+
|
25
|
+
require 'json'
|
26
|
+
|
27
|
+
module Unirest
|
28
|
+
|
29
|
+
class HttpResponse
|
30
|
+
attr_reader :code
|
31
|
+
attr_reader :raw_body
|
32
|
+
attr_reader :body
|
33
|
+
attr_reader :headers
|
34
|
+
|
35
|
+
def initialize(http_response)
|
36
|
+
@code = http_response.code;
|
37
|
+
@headers = http_response.headers
|
38
|
+
@raw_body = http_response
|
39
|
+
@body = @raw_body
|
40
|
+
|
41
|
+
begin
|
42
|
+
@body = JSON.parse(@raw_body)
|
43
|
+
rescue Exception
|
44
|
+
end
|
45
|
+
|
46
|
+
end
|
47
|
+
|
48
|
+
end
|
49
|
+
|
50
|
+
end
|
metadata
ADDED
@@ -0,0 +1,77 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: unirest
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.8
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Mashape
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2013-04-23 00:00:00 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rest-client
|
16
|
+
prerelease: false
|
17
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
18
|
+
requirements:
|
19
|
+
- &id002
|
20
|
+
- ">="
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: "0"
|
23
|
+
type: :runtime
|
24
|
+
version_requirements: *id001
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: json
|
27
|
+
prerelease: false
|
28
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
29
|
+
requirements:
|
30
|
+
- *id002
|
31
|
+
type: :runtime
|
32
|
+
version_requirements: *id003
|
33
|
+
- !ruby/object:Gem::Dependency
|
34
|
+
name: addressable
|
35
|
+
prerelease: false
|
36
|
+
requirement: &id004 !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- *id002
|
39
|
+
type: :runtime
|
40
|
+
version_requirements: *id004
|
41
|
+
description: Unirest is a set of lightweight HTTP libraries available in PHP, Ruby, Python, Java, Objective-C.
|
42
|
+
email: support@mashape.com
|
43
|
+
executables: []
|
44
|
+
|
45
|
+
extensions: []
|
46
|
+
|
47
|
+
extra_rdoc_files: []
|
48
|
+
|
49
|
+
files:
|
50
|
+
- lib/unirest/http_request.rb
|
51
|
+
- lib/unirest/http_response.rb
|
52
|
+
- lib/unirest.rb
|
53
|
+
homepage: https://unirest.io
|
54
|
+
licenses: []
|
55
|
+
|
56
|
+
metadata: {}
|
57
|
+
|
58
|
+
post_install_message:
|
59
|
+
rdoc_options: []
|
60
|
+
|
61
|
+
require_paths:
|
62
|
+
- lib
|
63
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
64
|
+
requirements:
|
65
|
+
- *id002
|
66
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
67
|
+
requirements:
|
68
|
+
- *id002
|
69
|
+
requirements: []
|
70
|
+
|
71
|
+
rubyforge_project:
|
72
|
+
rubygems_version: 2.0.2
|
73
|
+
signing_key:
|
74
|
+
specification_version: 4
|
75
|
+
summary: Unirest, the lightweight HTTP library
|
76
|
+
test_files: []
|
77
|
+
|