knack 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/lib/knack.rb +81 -24
- data/lib/knack/error.rb +10 -0
- data/lib/knack/response.rb +9 -0
- data/lib/knack/version.rb +1 -1
- metadata +13 -17
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 6d1ff01238079a379224a94e42d963a495a21c6b
|
4
|
+
data.tar.gz: de114685a531a29e22b37c0c20da4368012f3133
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: e99d1bb7675924dabd406b37f20517d35674173d3a21e9e81f8034762ff9b28b3120a165d5e442cf7d5cada1ebb373ed0820f0ceb02bd49d26200b86aafed3ee
|
7
|
+
data.tar.gz: 2d1a2919574007853111e4f73e65ff0ad32861e3648d064c561221e161b469bb3896861f12197a9549bebdad621cfd8afed3bc830c281215981654cf19db4a2d
|
data/lib/knack.rb
CHANGED
@@ -1,42 +1,99 @@
|
|
1
1
|
require 'json'
|
2
2
|
require 'net/http'
|
3
3
|
require 'uri'
|
4
|
-
require
|
4
|
+
require 'knack/error'
|
5
|
+
require 'knack/response'
|
6
|
+
require 'knack/version'
|
5
7
|
|
6
8
|
module Knack
|
7
|
-
class
|
8
|
-
|
9
|
+
class << self
|
10
|
+
def r(method, url, options={})
|
11
|
+
uri = URI.parse(url)
|
12
|
+
uri_query = form_encode(options[:params]) if options[:params]
|
9
13
|
|
10
|
-
|
11
|
-
|
12
|
-
|
14
|
+
response = Net::HTTP.start(uri.host, uri.port) do |http|
|
15
|
+
http.send_request(method, uri, options[:body], options[:headers])
|
16
|
+
end
|
17
|
+
|
18
|
+
Response.new(response.code, response.to_hash, response.body)
|
13
19
|
end
|
14
|
-
end
|
15
20
|
|
16
|
-
|
17
|
-
|
18
|
-
|
21
|
+
def get(url, options={})
|
22
|
+
options = {
|
23
|
+
headers: options[:headers],
|
24
|
+
params: options[:params],
|
25
|
+
auth: options[:auth]
|
26
|
+
}
|
19
27
|
|
20
|
-
|
21
|
-
http.send_request(method, uri, body, headers)
|
28
|
+
r('GET', url, options)
|
22
29
|
end
|
23
30
|
|
24
|
-
|
25
|
-
|
26
|
-
else
|
27
|
-
raise Error, response
|
31
|
+
def post(url, options={})
|
32
|
+
build_req('POST', url, options)
|
28
33
|
end
|
29
|
-
end
|
30
34
|
|
31
|
-
|
32
|
-
|
33
|
-
|
35
|
+
def patch(url, options={})
|
36
|
+
build_req('PATCH', url, options)
|
37
|
+
end
|
38
|
+
|
39
|
+
def put(url, options={})
|
40
|
+
build_req('PUT', url, options)
|
41
|
+
end
|
34
42
|
|
35
|
-
|
36
|
-
|
43
|
+
def delete(url, options={})
|
44
|
+
options = {
|
45
|
+
headers: options[:headers],
|
46
|
+
auth: options[:auth]
|
47
|
+
}
|
37
48
|
|
38
|
-
|
39
|
-
@status, @headers, @body = Integer(status), headers, body
|
49
|
+
r('DELETE', url, options)
|
40
50
|
end
|
51
|
+
|
52
|
+
private
|
53
|
+
|
54
|
+
def build_req(method, url, options={})
|
55
|
+
headers = options[:headers] || {}
|
56
|
+
body = build_body(headers, options)
|
57
|
+
options = build_options(headers, body, options[:auth])
|
58
|
+
|
59
|
+
r(method, url, options)
|
60
|
+
end
|
61
|
+
|
62
|
+
def build_body(headers, options)
|
63
|
+
if options.key? :data
|
64
|
+
transform_body(headers, options[:data])
|
65
|
+
elsif options.key? :json
|
66
|
+
unless headers['content-type'] == 'application/json'
|
67
|
+
headers['content-type'] = 'application/json'
|
68
|
+
end
|
69
|
+
|
70
|
+
if options[:json].class == Hash
|
71
|
+
JSON.generate(options[:json])
|
72
|
+
else
|
73
|
+
options[:json]
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
def build_options(headers, body, auth)
|
79
|
+
{
|
80
|
+
headers: headers,
|
81
|
+
body: body,
|
82
|
+
auth: auth
|
83
|
+
}
|
84
|
+
end
|
85
|
+
|
86
|
+
def transform_body(headers=nil, data=nil)
|
87
|
+
if !data.kind_of?(Enumerable)
|
88
|
+
data
|
89
|
+
else
|
90
|
+
headers['content-type'] = 'application/x-www-form-urlencoded'
|
91
|
+
form_encode(data)
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
def form_encode(params)
|
96
|
+
URI.encode_www_form(params)
|
97
|
+
end
|
41
98
|
end
|
42
99
|
end
|
data/lib/knack/error.rb
ADDED
data/lib/knack/version.rb
CHANGED
metadata
CHANGED
@@ -1,8 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: knack
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
5
|
-
prerelease:
|
4
|
+
version: 0.0.2
|
6
5
|
platform: ruby
|
7
6
|
authors:
|
8
7
|
- Connor Jacobsen
|
@@ -14,33 +13,29 @@ dependencies:
|
|
14
13
|
- !ruby/object:Gem::Dependency
|
15
14
|
name: bundler
|
16
15
|
requirement: !ruby/object:Gem::Requirement
|
17
|
-
none: false
|
18
16
|
requirements:
|
19
|
-
- - ~>
|
17
|
+
- - "~>"
|
20
18
|
- !ruby/object:Gem::Version
|
21
19
|
version: '1.6'
|
22
20
|
type: :development
|
23
21
|
prerelease: false
|
24
22
|
version_requirements: !ruby/object:Gem::Requirement
|
25
|
-
none: false
|
26
23
|
requirements:
|
27
|
-
- - ~>
|
24
|
+
- - "~>"
|
28
25
|
- !ruby/object:Gem::Version
|
29
26
|
version: '1.6'
|
30
27
|
- !ruby/object:Gem::Dependency
|
31
28
|
name: rake
|
32
29
|
requirement: !ruby/object:Gem::Requirement
|
33
|
-
none: false
|
34
30
|
requirements:
|
35
|
-
- -
|
31
|
+
- - ">="
|
36
32
|
- !ruby/object:Gem::Version
|
37
33
|
version: '0'
|
38
34
|
type: :development
|
39
35
|
prerelease: false
|
40
36
|
version_requirements: !ruby/object:Gem::Requirement
|
41
|
-
none: false
|
42
37
|
requirements:
|
43
|
-
- -
|
38
|
+
- - ">="
|
44
39
|
- !ruby/object:Gem::Version
|
45
40
|
version: '0'
|
46
41
|
description: HTTP Requests library for Ruby..
|
@@ -50,37 +45,38 @@ executables: []
|
|
50
45
|
extensions: []
|
51
46
|
extra_rdoc_files: []
|
52
47
|
files:
|
53
|
-
- .gitignore
|
48
|
+
- ".gitignore"
|
54
49
|
- Gemfile
|
55
50
|
- LICENSE.txt
|
56
51
|
- README.md
|
57
52
|
- Rakefile
|
58
53
|
- knack.gemspec
|
59
54
|
- lib/knack.rb
|
55
|
+
- lib/knack/error.rb
|
56
|
+
- lib/knack/response.rb
|
60
57
|
- lib/knack/version.rb
|
61
58
|
homepage: https://github.com/connorjacobsen/knack
|
62
59
|
licenses:
|
63
60
|
- MIT
|
61
|
+
metadata: {}
|
64
62
|
post_install_message:
|
65
63
|
rdoc_options: []
|
66
64
|
require_paths:
|
67
65
|
- lib
|
68
66
|
required_ruby_version: !ruby/object:Gem::Requirement
|
69
|
-
none: false
|
70
67
|
requirements:
|
71
|
-
- -
|
68
|
+
- - ">="
|
72
69
|
- !ruby/object:Gem::Version
|
73
70
|
version: '0'
|
74
71
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
75
|
-
none: false
|
76
72
|
requirements:
|
77
|
-
- -
|
73
|
+
- - ">="
|
78
74
|
- !ruby/object:Gem::Version
|
79
75
|
version: '0'
|
80
76
|
requirements: []
|
81
77
|
rubyforge_project:
|
82
|
-
rubygems_version:
|
78
|
+
rubygems_version: 2.4.5
|
83
79
|
signing_key:
|
84
|
-
specification_version:
|
80
|
+
specification_version: 4
|
85
81
|
summary: HTTP library for Ruby.
|
86
82
|
test_files: []
|