micky 0.2.0 → 0.3.0
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 +4 -4
- data/README.md +42 -4
- data/lib/micky/errors.rb +31 -0
- data/lib/micky/request.rb +12 -2
- data/lib/micky/response.rb +2 -2
- data/lib/micky/version.rb +1 -1
- data/lib/micky.rb +3 -0
- metadata +11 -10
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d8856a3b9bdf0070da933541f52c3571e5499783
|
4
|
+
data.tar.gz: fbcd0010c28a491fe267a27d0617774906d30b2a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f6f1d6969aa4957ec2983d44b841d3a5515cbadb067858b6b6bf859f785b5e8814e324d06db993cbc5960eda6a1e15d1ecc9bfd228db8f808ac56eadcaa250ee
|
7
|
+
data.tar.gz: 41d5a0daa0d33f7655082863beea63bc6a233f1f5e43e433ed18f90ff5e7d43a7ea6e8b8f82b10886d922802c2ff262de8126784840ecfcfe90ffec93f74f856
|
data/README.md
CHANGED
@@ -15,15 +15,21 @@ don’t want to add heavy dependencies to your app.
|
|
15
15
|
|
16
16
|
Add this line to your application’s Gemfile:
|
17
17
|
|
18
|
-
|
18
|
+
```ruby
|
19
|
+
gem 'micky'
|
20
|
+
```
|
19
21
|
|
20
22
|
And then execute:
|
21
23
|
|
22
|
-
|
24
|
+
```sh
|
25
|
+
$ bundle
|
26
|
+
```
|
23
27
|
|
24
28
|
Or install it yourself as:
|
25
29
|
|
26
|
-
|
30
|
+
```sh
|
31
|
+
$ gem install micky
|
32
|
+
```
|
27
33
|
|
28
34
|
## Usage
|
29
35
|
|
@@ -61,6 +67,38 @@ else
|
|
61
67
|
end
|
62
68
|
```
|
63
69
|
|
70
|
+
### Headers and query strings
|
71
|
+
|
72
|
+
Request headers and query string params can be passed as `:headers` and `:query`.
|
73
|
+
|
74
|
+
```ruby
|
75
|
+
Micky.get('http://drpm.me/unwz.jpg', headers: { 'Accept' => 'text/html' })
|
76
|
+
Micky.get('http://urls.api.twitter.com/1/urls/count.json', query: { url: 'dropmeme.com' })
|
77
|
+
```
|
78
|
+
|
79
|
+
### OAuth `Authorization` header
|
80
|
+
|
81
|
+
Micky supports creating a OAuth `Authorization` header with the help of the
|
82
|
+
[SimpleOAuth](https://github.com/laserlemon/simple_oauth) gem.
|
83
|
+
|
84
|
+
```ruby
|
85
|
+
Micky.get(
|
86
|
+
'https://api.twitter.com/1.1/statuses/user_timeline.json',
|
87
|
+
oauth: {
|
88
|
+
consumer_key: 'l0tSAl3tT3RsAnD1G1tS',
|
89
|
+
consumer_secret: 'l0tSAl3tT3RsAnD1G1tS',
|
90
|
+
token: 'l0tSAl3tT3RsAnD1G1tS',
|
91
|
+
token_secret: 'l0tSAl3tT3RsAnD1G1tS',
|
92
|
+
},
|
93
|
+
)
|
94
|
+
```
|
95
|
+
|
96
|
+
To use the `:oauth` argument, just ensure [`simple_oauth`](http://rubygems.org/gems/simple_oauth) is available:
|
97
|
+
|
98
|
+
```ruby
|
99
|
+
gem 'simple_oauth'
|
100
|
+
```
|
101
|
+
|
64
102
|
### Automatically parse responses into Ruby objects
|
65
103
|
|
66
104
|
`Micky::Response#body` always returns the response as a string. To parse this
|
@@ -123,9 +161,9 @@ end
|
|
123
161
|
|
124
162
|
## TODO
|
125
163
|
|
164
|
+
- Support :basic_auth and :digest_auth through [HTTPauth](https://github.com/Manfred/HTTPauth)
|
126
165
|
- Add tests
|
127
166
|
- Better document configuration options in README
|
128
|
-
- Add `raise_errors: true` option
|
129
167
|
|
130
168
|
## Contributing
|
131
169
|
|
data/lib/micky/errors.rb
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
module Micky
|
2
|
+
class Error < StandardError
|
3
|
+
attr_reader :response
|
4
|
+
|
5
|
+
def initialize(response = nil)
|
6
|
+
@response = response
|
7
|
+
end
|
8
|
+
|
9
|
+
def to_s
|
10
|
+
"#{response_code} #{response_message} at #{request_uri}"
|
11
|
+
end
|
12
|
+
|
13
|
+
def request_uri
|
14
|
+
response.uri if response
|
15
|
+
end
|
16
|
+
|
17
|
+
def response_code
|
18
|
+
response.code.to_i if response
|
19
|
+
end
|
20
|
+
|
21
|
+
def response_message
|
22
|
+
response.message if response
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
class ClientError < Error
|
27
|
+
end
|
28
|
+
|
29
|
+
class ServerError < Error
|
30
|
+
end
|
31
|
+
end
|
data/lib/micky/request.rb
CHANGED
@@ -5,7 +5,7 @@ module Micky
|
|
5
5
|
class Request
|
6
6
|
def initialize(opts = {})
|
7
7
|
# Options can be set per request and fallback to module-level defaults
|
8
|
-
[:max_redirects, :timeout, :skip_resolve, :resolve_timeout, :oauth, :query, :headers, :parsers].each do |name|
|
8
|
+
[:raise_errors, :max_redirects, :timeout, :skip_resolve, :resolve_timeout, :oauth, :query, :headers, :parsers].each do |name|
|
9
9
|
value = opts.has_key?(name) ? opts[name] : Micky.public_send(name)
|
10
10
|
instance_variable_set "@#{name}", value
|
11
11
|
end
|
@@ -36,7 +36,17 @@ module Micky
|
|
36
36
|
else
|
37
37
|
log response
|
38
38
|
log response.body
|
39
|
-
|
39
|
+
|
40
|
+
if @raise_errors
|
41
|
+
case response
|
42
|
+
when Net::HTTPClientError
|
43
|
+
raise Micky::ClientError.new(response)
|
44
|
+
when Net::HTTPServerError
|
45
|
+
raise Micky::ServerError.new(response)
|
46
|
+
end
|
47
|
+
else
|
48
|
+
nil
|
49
|
+
end
|
40
50
|
end
|
41
51
|
end
|
42
52
|
|
data/lib/micky/response.rb
CHANGED
@@ -27,12 +27,12 @@ module Micky
|
|
27
27
|
end
|
28
28
|
|
29
29
|
def inspect
|
30
|
-
"#<Micky::
|
30
|
+
"#<Micky::Response #{super}>"
|
31
31
|
end
|
32
32
|
|
33
33
|
# Support for `awesome_print`
|
34
34
|
def ai(*args)
|
35
|
-
"#<Micky::
|
35
|
+
"#<Micky::Response #{super}>"
|
36
36
|
end
|
37
37
|
end
|
38
38
|
end
|
data/lib/micky/version.rb
CHANGED
data/lib/micky.rb
CHANGED
@@ -3,9 +3,11 @@ require 'micky/version'
|
|
3
3
|
require 'micky/uri'
|
4
4
|
require 'micky/request'
|
5
5
|
require 'micky/response'
|
6
|
+
require 'micky/errors'
|
6
7
|
|
7
8
|
module Micky
|
8
9
|
class << self
|
10
|
+
attr_accessor :raise_errors
|
9
11
|
attr_accessor :max_redirects
|
10
12
|
attr_accessor :timeout
|
11
13
|
attr_accessor :skip_resolve
|
@@ -17,6 +19,7 @@ module Micky
|
|
17
19
|
end
|
18
20
|
|
19
21
|
# Reasonable defaults
|
22
|
+
@raise_errors = false
|
20
23
|
@max_redirects = 10
|
21
24
|
@timeout = 5
|
22
25
|
@skip_resolve = false
|
metadata
CHANGED
@@ -1,41 +1,41 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: micky
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Rafaël Blais Masson
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2014-04-09 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- - ~>
|
17
|
+
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
19
|
version: '1.3'
|
20
20
|
type: :development
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
|
-
- - ~>
|
24
|
+
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '1.3'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: rake
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
|
-
- -
|
31
|
+
- - ">="
|
32
32
|
- !ruby/object:Gem::Version
|
33
33
|
version: '0'
|
34
34
|
type: :development
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
|
-
- -
|
38
|
+
- - ">="
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '0'
|
41
41
|
description: Micky makes simple HTTP requests (GET/HEAD), follows redirects, handles
|
@@ -47,12 +47,13 @@ executables: []
|
|
47
47
|
extensions: []
|
48
48
|
extra_rdoc_files: []
|
49
49
|
files:
|
50
|
-
- .gitignore
|
50
|
+
- ".gitignore"
|
51
51
|
- Gemfile
|
52
52
|
- LICENSE.txt
|
53
53
|
- README.md
|
54
54
|
- Rakefile
|
55
55
|
- lib/micky.rb
|
56
|
+
- lib/micky/errors.rb
|
56
57
|
- lib/micky/request.rb
|
57
58
|
- lib/micky/response.rb
|
58
59
|
- lib/micky/uri.rb
|
@@ -68,17 +69,17 @@ require_paths:
|
|
68
69
|
- lib
|
69
70
|
required_ruby_version: !ruby/object:Gem::Requirement
|
70
71
|
requirements:
|
71
|
-
- -
|
72
|
+
- - ">="
|
72
73
|
- !ruby/object:Gem::Version
|
73
74
|
version: '0'
|
74
75
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
75
76
|
requirements:
|
76
|
-
- -
|
77
|
+
- - ">="
|
77
78
|
- !ruby/object:Gem::Version
|
78
79
|
version: '0'
|
79
80
|
requirements: []
|
80
81
|
rubyforge_project:
|
81
|
-
rubygems_version: 2.
|
82
|
+
rubygems_version: 2.2.2
|
82
83
|
signing_key:
|
83
84
|
specification_version: 4
|
84
85
|
summary: Lightweight and worry-free HTTP client
|