knack 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -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
@@ -1,42 +1,99 @@
1
1
  require 'json'
2
2
  require 'net/http'
3
3
  require 'uri'
4
- require "knack/version"
4
+ require 'knack/error'
5
+ require 'knack/response'
6
+ require 'knack/version'
5
7
 
6
8
  module Knack
7
- class Error < StandardError
8
- attr_reader :response
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
- def initialize(response)
11
- super(response.message)
12
- @response = response
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
- def self.r(method, url, headers={}, data=nil, params=nil, auth=nil)
17
- uri = URI.parse(url)
18
- uri_query = URI.encode_www_form(params) if params
21
+ def get(url, options={})
22
+ options = {
23
+ headers: options[:headers],
24
+ params: options[:params],
25
+ auth: options[:auth]
26
+ }
19
27
 
20
- response = Net::HTTP.start(uri.host, uri.port, opts(uri)) do |http|
21
- http.send_request(method, uri, body, headers)
28
+ r('GET', url, options)
22
29
  end
23
30
 
24
- if response.is_a?(Net::HTTPSuccess)
25
- Response.new(response.code, response.to_hash, response.body)
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
- def self.get(url, **kwargs)
32
- self.r('GET', url, **kwargs)
33
- end
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
- class Response
36
- attr :status, :headers, :body
43
+ def delete(url, options={})
44
+ options = {
45
+ headers: options[:headers],
46
+ auth: options[:auth]
47
+ }
37
48
 
38
- def initialize(status, headers, body)
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
@@ -0,0 +1,10 @@
1
+ module Knack
2
+ class Error < StandardError
3
+ attr_reader :response
4
+
5
+ def initialize(response)
6
+ super(response.message)
7
+ @response = response
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,9 @@
1
+ module Knack
2
+ class Response
3
+ attr :status, :headers, :body
4
+
5
+ def initialize(status, headers, body)
6
+ @status, @headers, @body = Integer(status), headers, body
7
+ end
8
+ end
9
+ end
@@ -1,3 +1,3 @@
1
1
  module Knack
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
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.1
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: 1.8.23
78
+ rubygems_version: 2.4.5
83
79
  signing_key:
84
- specification_version: 3
80
+ specification_version: 4
85
81
  summary: HTTP library for Ruby.
86
82
  test_files: []