requests 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 94a0808cfc297bb1b590e6afa037aca415c9acff
4
+ data.tar.gz: d91a4f9e7828da7f2b48ecabc550a48fc43dd7e8
5
+ SHA512:
6
+ metadata.gz: e53ae395eb628e01ced4fc25f4e12b29675ca1744cac6881a45589b9870b51c31093ee85c1395601cc2e4d3600a6922a05f612da4a0c8be560b8bef4ab146449
7
+ data.tar.gz: 404ac732d9e2b56789a8d2348456fc986f8d62b9b42c45fdf050b3d0af0b954da83ffa0ff9c50b2a6d0d060156ef0429293503bbacbab54da45f86cbbe626591
data/LICENSE ADDED
@@ -0,0 +1,19 @@
1
+ Copyright (c) 2012 Cyril David
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in
11
+ all copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ THE SOFTWARE.
data/README ADDED
@@ -0,0 +1,17 @@
1
+ requests
2
+ ====
3
+
4
+ Requests: HTTP for Humans (Ruby port)
5
+
6
+ Description
7
+ -----------
8
+
9
+ Because Requests for Python is awesome
10
+
11
+ ## Installation
12
+
13
+ As usual, you can install it using rubygems.
14
+
15
+ ```
16
+ $ gem install requests
17
+ ```
data/lib/requests.rb ADDED
@@ -0,0 +1,93 @@
1
+ require 'json'
2
+ require 'net/http'
3
+ require 'uri'
4
+
5
+ module Requests
6
+ def self.get(url, **kwargs)
7
+ request('GET', url, **kwargs)
8
+ end
9
+
10
+ def self.post(url, data: nil, **kwargs)
11
+ request('POST', url, data: data, **kwargs)
12
+ end
13
+
14
+ def self.put(url, data: nil, **kwargs)
15
+ request('PUT', url, data: data, **kwargs)
16
+ end
17
+
18
+ def self.delete(url, **kwargs)
19
+ request('DELETE', url, **kwargs)
20
+ end
21
+
22
+ def self.head(url, **kwargs)
23
+ request('HEAD', url, **kwargs)
24
+ end
25
+
26
+ def self.options(url, **kwargs)
27
+ request('OPTIONS', url, **kwargs)
28
+ end
29
+
30
+ def self.request(method, url,
31
+ headers: {},
32
+ data: nil,
33
+ params: nil,
34
+ auth: nil)
35
+
36
+ uri = URI.parse(url)
37
+ uri.query = URI.encode_www_form(params) if params
38
+
39
+ body = _encode_params(headers: headers, data: data) if data
40
+
41
+ _basic_auth(headers, *auth) if auth
42
+
43
+ response = Net::HTTP.start(uri.host, uri.port) do |http|
44
+ http.send_request(method, uri, body, headers)
45
+ end
46
+
47
+ if response.is_a?(Net::HTTPSuccess)
48
+ Response.new(response.code, response.to_hash, response.body)
49
+ else
50
+ raise response.inspect
51
+ end
52
+ end
53
+
54
+ private
55
+ def self._basic_auth(headers, user, pass)
56
+ headers['Authorization'] = 'Basic ' + ["#{user}:#{pass}"].pack('m0')
57
+ end
58
+
59
+ def self._encode_params(headers: headers, data: data)
60
+ if not data.kind_of?(Enumerable)
61
+ data
62
+ else
63
+ headers['content-type'] = 'application/x-www-form-urlencoded'
64
+
65
+ URI.encode_www_form(data)
66
+ end
67
+ end
68
+
69
+ class Response
70
+ attr :status_code
71
+ attr :headers
72
+ attr :content
73
+
74
+ def initialize(status_code, headers, content)
75
+ @status_code, @headers, @content = Integer(status_code), headers, content
76
+ end
77
+
78
+ # TODO Verify that JSON can parse data without encoding stuff
79
+ def json
80
+ JSON.parse(@content)
81
+ end
82
+
83
+ # TODO Verify that this is based on content-type header
84
+ def encoding
85
+ @content.encoding
86
+ end
87
+
88
+ # TODO this will probably do something related to encoding if necessary
89
+ def text
90
+ @content
91
+ end
92
+ end
93
+ end
data/requests.gemspec ADDED
@@ -0,0 +1,23 @@
1
+ # encoding: utf-8
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = "requests"
5
+ s.version = "0.0.1"
6
+ s.summary = "Requests: HTTP for Humans (Ruby port)"
7
+ s.description = "Because Requests for Python is awesome"
8
+ s.authors = ["Cyril David"]
9
+ s.email= ["cyx@cyx.is"]
10
+ s.homepage = "http://cyx.is/"
11
+ s.files = Dir[
12
+ "LICENSE",
13
+ "README",
14
+ "makefile",
15
+ "lib/*.rb",
16
+ "tests/*.rb",
17
+ "*.gemspec"
18
+ ]
19
+
20
+ s.license = "MIT"
21
+ s.require_paths = ["lib"]
22
+ s.add_development_dependency "cutest"
23
+ end
@@ -0,0 +1,51 @@
1
+ require File.expand_path('../lib/requests', File.dirname(__FILE__))
2
+
3
+ test "basic auth" do
4
+ r = Requests.get("http://httpbin.org/basic-auth/u/p", auth: ['u', 'p'])
5
+
6
+ assert_equal r.json["authenticated"], true
7
+ assert_equal r.json["user"], "u"
8
+ end
9
+
10
+ test "GET" do
11
+ r = Requests.get("http://httpbin.org/get", params: { foo: "bar" })
12
+
13
+ assert_equal 200, r.status_code
14
+ assert_equal ["application/json"], r.headers["content-type"]
15
+ assert_equal "UTF-8", r.encoding.to_s
16
+
17
+ assert(r.json["args"] && r.json["args"]["foo"] == "bar")
18
+ end
19
+
20
+ test "POST data" do
21
+ r = Requests.post("http://httpbin.org/post", data: '{ "plan": "test" }')
22
+
23
+ assert_equal 200, r.status_code
24
+ assert_equal ["application/json"], r.headers["content-type"]
25
+ assert_equal "UTF-8", r.encoding.to_s
26
+
27
+ assert(r.json["json"] && r.json["json"] == { "plan" => "test" })
28
+ end
29
+
30
+ test "PUT data" do
31
+
32
+ end
33
+
34
+ test "POST params" do
35
+ payload = [
36
+ ['a[]', 'a1'],
37
+ ['a[]', 'a2'],
38
+ ['b', '3'],
39
+ ['c', '4']
40
+ ]
41
+
42
+ r = Requests.post("http://httpbin.org/post", data: payload)
43
+
44
+ assert_equal 200, r.status_code
45
+
46
+ form = r.json["form"]
47
+
48
+ assert_equal form['a[]'], ['a1', 'a2']
49
+ assert_equal form['b'], '3'
50
+ assert_equal form['c'], '4'
51
+ end
metadata ADDED
@@ -0,0 +1,63 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: requests
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Cyril David
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-05-30 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: cutest
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ description: Because Requests for Python is awesome
28
+ email:
29
+ - cyx@cyx.is
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - LICENSE
35
+ - README
36
+ - lib/requests.rb
37
+ - tests/requests_test.rb
38
+ - requests.gemspec
39
+ homepage: http://cyx.is/
40
+ licenses:
41
+ - MIT
42
+ metadata: {}
43
+ post_install_message:
44
+ rdoc_options: []
45
+ require_paths:
46
+ - lib
47
+ required_ruby_version: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - '>='
50
+ - !ruby/object:Gem::Version
51
+ version: '0'
52
+ required_rubygems_version: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - '>='
55
+ - !ruby/object:Gem::Version
56
+ version: '0'
57
+ requirements: []
58
+ rubyforge_project:
59
+ rubygems_version: 2.0.0
60
+ signing_key:
61
+ specification_version: 4
62
+ summary: 'Requests: HTTP for Humans (Ruby port)'
63
+ test_files: []