requests 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 94a0808cfc297bb1b590e6afa037aca415c9acff
4
- data.tar.gz: d91a4f9e7828da7f2b48ecabc550a48fc43dd7e8
3
+ metadata.gz: 4384da6fa569599a07f1892ee44b3a95d5b46c77
4
+ data.tar.gz: bd021ee8370d0ee4e49dfc5709803eb38e6e9af4
5
5
  SHA512:
6
- metadata.gz: e53ae395eb628e01ced4fc25f4e12b29675ca1744cac6881a45589b9870b51c31093ee85c1395601cc2e4d3600a6922a05f612da4a0c8be560b8bef4ab146449
7
- data.tar.gz: 404ac732d9e2b56789a8d2348456fc986f8d62b9b42c45fdf050b3d0af0b954da83ffa0ff9c50b2a6d0d060156ef0429293503bbacbab54da45f86cbbe626591
6
+ metadata.gz: 331205ff6ad5a1fe0c6af031c7941f6017a13d88cbd04c141937925f89c4c07701bbaf55f2c0261be721ec9282ee204741b19ecc907ac6add0a6360fa938c50f
7
+ data.tar.gz: ea1c2ee5d1b82b5d73400ab934a056b362ee7ce2b072efc54b2b0398ea97a996a8586b3f7b95ef236e33c3a60c8aba6c43eef3add290ca9db33bea6ebce452ef
data/lib/requests.rb CHANGED
@@ -1,31 +1,13 @@
1
1
  require 'json'
2
2
  require 'net/http'
3
+ require 'openssl'
3
4
  require 'uri'
4
5
 
5
6
  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
7
+ Error = Class.new(StandardError)
13
8
 
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
9
+ CA_FILE = ENV.fetch('REQUESTS_CA_FILE',
10
+ File.expand_path('../cacert.pem', __FILE__))
29
11
 
30
12
  def self.request(method, url,
31
13
  headers: {},
@@ -40,18 +22,27 @@ module Requests
40
22
 
41
23
  _basic_auth(headers, *auth) if auth
42
24
 
43
- response = Net::HTTP.start(uri.host, uri.port) do |http|
25
+ response = Net::HTTP.start(uri.host, uri.port, opts(uri)) do |http|
44
26
  http.send_request(method, uri, body, headers)
45
27
  end
46
28
 
47
29
  if response.is_a?(Net::HTTPSuccess)
48
30
  Response.new(response.code, response.to_hash, response.body)
49
31
  else
50
- raise response.inspect
32
+ raise Error, response.inspect
51
33
  end
52
34
  end
53
35
 
54
36
  private
37
+ def self.opts(uri)
38
+ if uri.scheme == 'https'
39
+ { use_ssl: true,
40
+ verify_mode: OpenSSL::SSL::VERIFY_PEER,
41
+ ca_file: CA_FILE
42
+ }
43
+ end
44
+ end
45
+
55
46
  def self._basic_auth(headers, user, pass)
56
47
  headers['Authorization'] = 'Basic ' + ["#{user}:#{pass}"].pack('m0')
57
48
  end
data/makefile ADDED
@@ -0,0 +1,2 @@
1
+ test:
2
+ RUBYLIB=./lib cutest tests/*_test.rb
data/requests.gemspec CHANGED
@@ -2,12 +2,12 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = "requests"
5
- s.version = "0.0.1"
5
+ s.version = "0.0.2"
6
6
  s.summary = "Requests: HTTP for Humans (Ruby port)"
7
7
  s.description = "Because Requests for Python is awesome"
8
8
  s.authors = ["Cyril David"]
9
9
  s.email= ["cyx@cyx.is"]
10
- s.homepage = "http://cyx.is/"
10
+ s.homepage = "http://github.com/cyx/requests"
11
11
  s.files = Dir[
12
12
  "LICENSE",
13
13
  "README",
@@ -1,37 +1,37 @@
1
- require File.expand_path('../lib/requests', File.dirname(__FILE__))
1
+ require 'requests/sugar'
2
2
 
3
- test "basic auth" do
4
- r = Requests.get("http://httpbin.org/basic-auth/u/p", auth: ['u', 'p'])
3
+ test 'basic auth' do
4
+ r = Requests.get('http://httpbin.org/basic-auth/u/p', auth: ['u', 'p'])
5
5
 
6
- assert_equal r.json["authenticated"], true
7
- assert_equal r.json["user"], "u"
6
+ assert_equal r.json['authenticated'], true
7
+ assert_equal r.json['user'], 'u'
8
8
  end
9
9
 
10
- test "GET" do
11
- r = Requests.get("http://httpbin.org/get", params: { foo: "bar" })
10
+ test 'GET' do
11
+ r = Requests.get('http://httpbin.org/get', params: { foo: 'bar' })
12
12
 
13
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
14
+ assert_equal ['application/json'], r.headers['content-type']
15
+ assert_equal 'UTF-8', r.encoding.to_s
16
16
 
17
- assert(r.json["args"] && r.json["args"]["foo"] == "bar")
17
+ assert(r.json['args'] && r.json['args']['foo'] == 'bar')
18
18
  end
19
19
 
20
- test "POST data" do
21
- r = Requests.post("http://httpbin.org/post", data: '{ "plan": "test" }')
20
+ test 'POST data' do
21
+ r = Requests.post('http://httpbin.org/post', data: '{ 'plan': 'test' }')
22
22
 
23
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
24
+ assert_equal ['application/json'], r.headers['content-type']
25
+ assert_equal 'UTF-8', r.encoding.to_s
26
26
 
27
- assert(r.json["json"] && r.json["json"] == { "plan" => "test" })
27
+ assert(r.json['json'] && r.json['json'] == { 'plan' => 'test' })
28
28
  end
29
29
 
30
- test "PUT data" do
30
+ test 'PUT data' do
31
31
 
32
32
  end
33
33
 
34
- test "POST params" do
34
+ test 'POST params' do
35
35
  payload = [
36
36
  ['a[]', 'a1'],
37
37
  ['a[]', 'a2'],
@@ -39,11 +39,11 @@ test "POST params" do
39
39
  ['c', '4']
40
40
  ]
41
41
 
42
- r = Requests.post("http://httpbin.org/post", data: payload)
42
+ r = Requests.post('http://httpbin.org/post', data: payload)
43
43
 
44
44
  assert_equal 200, r.status_code
45
45
 
46
- form = r.json["form"]
46
+ form = r.json['form']
47
47
 
48
48
  assert_equal form['a[]'], ['a1', 'a2']
49
49
  assert_equal form['b'], '3'
data/tests/ssl_test.rb ADDED
@@ -0,0 +1,7 @@
1
+ require 'requests'
2
+
3
+ test 'ssl' do
4
+ response = Requests.request('GET', 'https://httpbin.org/get')
5
+
6
+ assert_equal 200, response.status_code
7
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: requests
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Cyril David
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-05-30 00:00:00.000000000 Z
11
+ date: 2013-05-31 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: cutest
@@ -32,11 +32,12 @@ extensions: []
32
32
  extra_rdoc_files: []
33
33
  files:
34
34
  - LICENSE
35
- - README
35
+ - makefile
36
36
  - lib/requests.rb
37
37
  - tests/requests_test.rb
38
+ - tests/ssl_test.rb
38
39
  - requests.gemspec
39
- homepage: http://cyx.is/
40
+ homepage: http://github.com/cyx/requests
40
41
  licenses:
41
42
  - MIT
42
43
  metadata: {}
data/README DELETED
@@ -1,17 +0,0 @@
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
- ```