hippie 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
data/lib/hippie.rb ADDED
@@ -0,0 +1,110 @@
1
+ require 'uri'
2
+ require 'net/http'
3
+ require 'openssl'
4
+ require 'json'
5
+
6
+ module Hippie
7
+ class Response
8
+ attr_reader :status, :headers, :body
9
+
10
+ def initialize(status, headers, body)
11
+ @status = status.to_i
12
+ @headers = headers
13
+ @body = body || ''
14
+ end
15
+
16
+ def json
17
+ JSON.parse(@body)
18
+ end
19
+
20
+ def encoding
21
+ @body.encoding
22
+ end
23
+
24
+ def valid?
25
+ @status.between?(200, 299)
26
+ end
27
+
28
+ def error?
29
+ !valid?
30
+ end
31
+ end
32
+
33
+ CA_FILE = ENV.fetch('REQUESTS_CA_FILE') { File.expand_path('../cacert.pem', __FILE__) }
34
+
35
+ def self.get(url, **kwargs)
36
+ request('GET', url, **kwargs)
37
+ end
38
+
39
+ def self.post(url, data: nil, **kwargs)
40
+ request('POST', url, data: data, **kwargs)
41
+ end
42
+
43
+ def self.put(url, data: nil, **kwargs)
44
+ request('PUT', url, data: data, **kwargs)
45
+ end
46
+
47
+ def self.delete(url, **kwargs)
48
+ request('DELETE', url, **kwargs)
49
+ end
50
+
51
+ def self.head(url, **kwargs)
52
+ request('HEAD', url, **kwargs)
53
+ end
54
+
55
+ def self.options(url, **kwargs)
56
+ request('OPTIONS', url, **kwargs)
57
+ end
58
+
59
+ def self.patch(url, **kwargs)
60
+ request('PATCH', url, **kwargs)
61
+ end
62
+
63
+ def self.trace(url, **kwargs)
64
+ request('TRACE', url, **kwargs)
65
+ end
66
+
67
+ private
68
+
69
+ def self.request(method, url, headers: {}, data: nil, params: nil, auth: nil)
70
+ fail "No HTTP(S) scheme in: #{url}" unless url =~ /^https?:\/\//i
71
+
72
+ uri = URI.parse(URI.encode(url))
73
+
74
+ uri.query = URI.encode_www_form(params) if params
75
+ body = process_params(headers: headers, data: data) if data
76
+ basic_auth(headers, *auth) if auth
77
+
78
+ response = Net::HTTP.start(uri.host, uri.port, opts(uri)) do |http|
79
+ if method == 'HEAD'
80
+ http.head(uri)
81
+ else
82
+ http.send_request(method, uri, body, headers)
83
+ end
84
+ end
85
+
86
+ Response.new(response.code, response.to_hash, response.body)
87
+ end
88
+
89
+ def self.opts(uri)
90
+ if uri.scheme == 'https'
91
+ { use_ssl: true,
92
+ verify_mode: OpenSSL::SSL::VERIFY_PEER,
93
+ ca_file: CA_FILE
94
+ }
95
+ end
96
+ end
97
+
98
+ def self.basic_auth(headers, user, pass)
99
+ headers['Authorization'] = 'Basic ' + ["#{user}:#{pass}"].pack('m0')
100
+ end
101
+
102
+ def self.process_params(headers: nil, data: nil)
103
+ if data.is_a?(Enumerable)
104
+ headers['Content-Type'] = 'application/x-www-form-urlencoded'
105
+ URI.encode_www_form(data)
106
+ else
107
+ data
108
+ end
109
+ end
110
+ end
data/makefile ADDED
@@ -0,0 +1,2 @@
1
+ test:
2
+ RUBYLIB=./lib cutest tests/*_test.rb
@@ -0,0 +1,90 @@
1
+ require 'hippie'
2
+
3
+ test 'GET' do
4
+ r = Hippie.get('http://httpbin.org/get', params: { foo: 'bar' })
5
+
6
+ assert_equal 200, r.status
7
+ assert_equal ['application/json'], r.headers['content-type']
8
+ assert_equal 'UTF-8', r.encoding.to_s
9
+
10
+ assert(r.json['args'] && r.json['args']['foo'] == 'bar')
11
+ end
12
+ puts 'GET'
13
+
14
+ test 'POST' do
15
+ r = Hippie.post('http://httpbin.org/post', data: { "plan" => "test" })
16
+
17
+ assert_equal 200, r.status
18
+ assert_equal ['application/json'], r.headers['content-type']
19
+ assert_equal 'UTF-8', r.encoding.to_s
20
+
21
+ assert(r.json['form'] && r.json['form'] == { 'plan' => 'test' })
22
+ end
23
+ puts 'POST'
24
+
25
+ test 'PUT' do
26
+ r = Hippie.put('http://httpbin.org/put', data: { 'plan' => 'test' })
27
+
28
+ assert_equal 200, r.status
29
+ assert_equal ['application/json'], r.headers['content-type']
30
+ assert_equal 'UTF-8', r.encoding.to_s
31
+
32
+ assert(r.json['form'] && r.json['form'] == { 'plan' => 'test' })
33
+ end
34
+ puts 'PUT'
35
+
36
+ test 'DELETE' do
37
+ r = Hippie.delete('http://httpbin.org/delete', data: { 'plan' => 'test' })
38
+
39
+ assert_equal 200, r.status
40
+ assert_equal ['application/json'], r.headers['content-type']
41
+ assert_equal 'UTF-8', r.encoding.to_s
42
+
43
+ assert(r.json['form'] && r.json['form'] == { 'plan' => 'test' })
44
+ end
45
+ puts 'DELETE'
46
+
47
+ test 'HEAD' do
48
+ r = Hippie.head('http://httpbin.org/headers')
49
+
50
+ assert_equal 200, r.status
51
+ assert_equal ['application/json'], r.headers['content-type']
52
+ assert_equal 'UTF-8', r.encoding.to_s
53
+ end
54
+ puts 'HEAD'
55
+
56
+ test 'OPTIONS' do
57
+ # TODO
58
+ end
59
+ puts 'OPTIONS'
60
+
61
+ test 'PATCH' do
62
+ r = Hippie.patch('http://httpbin.org/patch', data: { 'plan' => 'test' })
63
+
64
+ assert_equal 200, r.status
65
+ assert_equal ['application/json'], r.headers['content-type']
66
+ assert_equal 'UTF-8', r.encoding.to_s
67
+
68
+ assert(r.json['form'] && r.json['form'] == { 'plan' => 'test' })
69
+ end
70
+ puts 'PATCH'
71
+
72
+ test 'TRACE' do
73
+ # TODO
74
+ end
75
+ puts 'TRACE'
76
+
77
+ test 'Basic Auth' do
78
+ r = Hippie.get('http://httpbin.org/basic-auth/u/p', auth: ['u', 'p'])
79
+
80
+ assert_equal r.json['authenticated'], true
81
+ assert_equal r.json['user'], 'u'
82
+ end
83
+ puts 'Basic Auth'
84
+
85
+ test 'SSL' do
86
+ response = Hippie.get('https://httpbin.org/get')
87
+
88
+ assert_equal 200, response.status
89
+ end
90
+ puts 'SSL'
metadata ADDED
@@ -0,0 +1,71 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hippie
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.5.0
5
+ platform: ruby
6
+ authors:
7
+ - Martin Manelli
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-10-29 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: '1.2'
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 1.2.0
23
+ type: :development
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - "~>"
28
+ - !ruby/object:Gem::Version
29
+ version: '1.2'
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: 1.2.0
33
+ description: Simple wrapper for Net::HTTP
34
+ email: manelli.ml@gmail.com
35
+ executables: []
36
+ extensions: []
37
+ extra_rdoc_files: []
38
+ files:
39
+ - LICENSE
40
+ - README.md
41
+ - hippie.gemspec
42
+ - lib/cacert.pem
43
+ - lib/hippie.rb
44
+ - makefile
45
+ - tests/hippie_test.rb
46
+ homepage: http://github.com/manelli/hippie
47
+ licenses:
48
+ - MIT
49
+ metadata: {}
50
+ post_install_message:
51
+ rdoc_options: []
52
+ require_paths:
53
+ - lib
54
+ required_ruby_version: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ version: '0'
59
+ required_rubygems_version: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ version: '0'
64
+ requirements: []
65
+ rubyforge_project:
66
+ rubygems_version: 2.2.2
67
+ signing_key:
68
+ specification_version: 4
69
+ summary: Simple HTTP
70
+ test_files: []
71
+ has_rdoc: