dagger 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +20 -5
  3. data/lib/dagger.rb +19 -28
  4. data/lib/dagger/version.rb +1 -1
  5. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 9359ff69009b6e529fc5a3a85b22a0c36e4749ef
4
- data.tar.gz: aba7366cb305baef5e51fc13b2a1be735a7fc6fb
3
+ metadata.gz: d8c2144cb2fc6feb4333d118265b8b33d31df3e2
4
+ data.tar.gz: 3615c376cd385f9731dd7fe022f4dfa56e2b95e9
5
5
  SHA512:
6
- metadata.gz: 3b7051c44a65852facf64ce9584972f1904d962567298aa99a043b0912780bcafb767f00efdfbbbd70ddd78f57b950e83d72496b76c739c342b754518cae9346
7
- data.tar.gz: e2f1f1fe615fb20529e9346f823dd38a7207d599102a8bd698156c657013211b3071ddaae3c5f5d3b0074da2c48f89502cf2b00880e24ef20f5fe2eaaf71a00e
6
+ metadata.gz: 223372582af34f07018cbc0df565b543c57a5b47c6e876081bf9dc0b98b4d402e71ada2abd1aa77772ca31321add90ed71880b73e07e75d13c98c649adb73094
7
+ data.tar.gz: 7b9a7983be101326ab8ba00a810f416c1e225fe9960c335c9c544ba616168f5777ef9cb8026f7b28811c1e0434e1dccbf03bf36e7d4bd847ecc8be7920a27b65
data/README.md CHANGED
@@ -1,6 +1,8 @@
1
1
  # Dagger
2
2
 
3
- Very simple wrapper around Net::HTTP.
3
+ Featherweight wrapper around Net::HTTP.
4
+
5
+ Follows redirects if instructed, and comes with out-of-the-box parsing of JSON and XML, via [oj](https://github.com/ohler55/oj) and [ox](https://github.com/ohler55/ox), respectively.
4
6
 
5
7
  # Installation
6
8
 
@@ -10,34 +12,47 @@ In your Gemfile:
10
12
 
11
13
  # Usage
12
14
 
13
- ## `get(url)`
15
+ ## `get(url, [params], [options])`
14
16
 
15
17
  ```rb
16
18
  require 'dagger'
17
19
  resp = Dagger.get('http://google.com')
18
20
 
19
21
  puts resp.body # => "<!doctype html...>"
22
+
23
+ # if query is passed, it is appended as a query string
24
+ Dagger.get('google.com/search', { q: 'dagger' }) # => requests '/search?q=dagger'
20
25
  ```
21
26
 
22
- ## `post(url, data)`
27
+ ## `post(url, params, [options])`
23
28
 
24
29
  ```rb
25
30
  resp = Dagger.post('http://api.server.com', { foo: 'bar' })
26
31
  puts resp.status # => 200
27
32
 
28
- # if the endpoint returned a parseable content-type (e.g. 'application/json')
33
+ # if you want to send JSON to the server, you can pass the { json: true } option,
34
+ # which converts your params object to JSON, and also sets Content-Type to 'application/json'
35
+ resp = Dagger.put('http://server.com', { foo: 'bar' }, { json: true })
36
+
37
+ # now, if the endpoint returned a parseable content-type (e.g. 'application/json')
29
38
  # then `resp.data` will return the parsed result. `body` contains the raw data.
30
39
  puts resp.data # => { result: 'GREAT SUCCESS!' }
31
40
  ```
32
41
 
33
- Same syntax applies for `put`, `patch` and `delete` requests. You can also pass options as the third param:
42
+ Same syntax applies for `put`, `patch` and `delete` requests.
43
+
44
+ # Options
45
+
46
+ These are all the available options.
34
47
 
35
48
  ```rb
36
49
  opts = {
50
+ json: true, # converts params object to JSON and sets Content-Type header. (POST/PUT/PATCH only)
37
51
  follow: true, # follow redirects (10 by default)
38
52
  headers: { 'Accept': 'text/xml' },
39
53
  username: 'dagger', # for HTTP auth
40
54
  password: 'fidelio',
55
+ verify_ssl: false, # true by default
41
56
  open_timeout: 30,
42
57
  read_timeout: 30
43
58
  }
@@ -1,18 +1,4 @@
1
1
  # Dagger.
2
- # Simple Net::HTTP wrapper (in less than 100 LOC)
3
- # Written by Tomas Pollak
4
-
5
- # Example Usage ---------------------------------
6
- #
7
- # resp, body = Dagger.get('http://google.com')
8
- # puts body if resp.code == 200
9
- #
10
- # opts = { username: 'foobar', password: 'secret' }
11
- # resp, body = Dagger.get('http://api.server.com', opts)
12
- #
13
- # opts = { verify_ssl: false, open_timeout: 30 }
14
- # resp, body = Dagger.post('http://twitter.com', { foo: 'bar' }, opts)
15
-
16
2
  require 'dagger/version'
17
3
  require 'dagger/response'
18
4
  require 'dagger/parsers'
@@ -28,16 +14,14 @@ module Dagger
28
14
  }
29
15
 
30
16
  def self.get(uri, query = nil, opts = {})
31
- raise ArgumentError.new("Empty URL!") if (uri || '').strip == ''
32
-
33
17
  opts[:follow] = 10 if opts[:follow] == true
34
- uri = parse_uri(uri)
35
- uri.query = encode(opts[:query]) if opts[:query]
36
18
 
37
- http = client(uri, opts)
38
- request = Net::HTTP::Get.new(uri.request_uri, DEFAULT_HEADERS.merge(opts[:headers] || {}))
19
+ uri = parse_uri(uri)
20
+ uri.query = encode(opts[:query]) if opts[:query]
21
+ http = client(uri, opts)
22
+ request = Net::HTTP::Get.new(uri.request_uri, DEFAULT_HEADERS.merge(opts[:headers] || {}))
39
23
 
40
- if opts[:username] && opts[:password]
24
+ if opts[:username] # && opts[:password]
41
25
  request.basic_auth(opts.delete(:username), opts.delete(:password))
42
26
  end
43
27
 
@@ -71,14 +55,21 @@ module Dagger
71
55
  private
72
56
 
73
57
  def self.request(method, uri, params, opts = {})
74
- # raise "Params should be a hash." unless params.is_a?(Hash)
75
- uri = parse_uri(uri)
76
- query = params.is_a?(String) ? params : encode(params)
77
-
58
+ uri = parse_uri(uri)
78
59
  headers = opts[:headers] || {}
79
60
 
80
- if opts[:username] && opts[:password]
81
- headers['Authorization'] = "Basic " + Base64.encode64("#{opts[:username]}:#{opts[:password]}")
61
+ query = if params.is_a?(String)
62
+ params
63
+ elsif opts[:json]
64
+ Oj.dump(params) # convert to JSON
65
+ headers['Content-Type'] = 'application/json'
66
+ else
67
+ encode(params)
68
+ end
69
+
70
+ if opts[:username] # opts[:password] is optional
71
+ str = [opts[:username], opts[:password]].compact.join(':')
72
+ headers['Authorization'] = "Basic " + Base64.encode64(str)
82
73
  end
83
74
 
84
75
  args = [method, uri.path, query, headers]
@@ -99,7 +90,7 @@ module Dagger
99
90
  http.open_timeout = opts[:open_timeout] if opts[:open_timeout]
100
91
  http.read_timeout = opts[:read_timeout] if opts[:read_timeout]
101
92
  http.use_ssl = true if uri.port == 443
102
- http.verify_mode = opts[:verify_ssl] ? OpenSSL::SSL::VERIFY_PEER : OpenSSL::SSL::VERIFY_NONE
93
+ http.verify_mode = opts[:verify_ssl] === false ? OpenSSL::SSL::VERIFY_NONE : OpenSSL::SSL::VERIFY_PEER
103
94
  http
104
95
  end
105
96
 
@@ -1,6 +1,6 @@
1
1
  module Dagger
2
2
  MAJOR = 0
3
- MINOR = 1
3
+ MINOR = 2
4
4
  PATCH = 0
5
5
 
6
6
  VERSION = [MAJOR, MINOR, PATCH].join('.')
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dagger
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tomás Pollak