micky 0.1.0 → 0.2.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 4c3ca66fab6596cf504eb875cfdac174caf530c6
4
- data.tar.gz: bbaf1adc0f7fdf7e2a1bba530872e0e70f3374d6
3
+ metadata.gz: af45a8a8f7fc7e48b95beeffbff81a51e9948ad1
4
+ data.tar.gz: 057926d44234a5d7cea3205f35e905ee58d9ebeb
5
5
  SHA512:
6
- metadata.gz: da7fe99bed4c2819a06fa22f036a704ade75ae6e731cb712c455365f07818d52d2d11ef2bb7c33ba3c3550ebb39ee128861fc9554bf25223c46372cc3c269183
7
- data.tar.gz: 2868f075661f8bb2b1a0722bb992088b71a86db44cdb8cb624b1e5b882668809ae18883d8256ae85871357d40476b573d3f218ae7c90eb8bccbd83268457c676
6
+ metadata.gz: 0e9b6c1d51655b9e995e4bad8ffe680f8f105a8d29371cbecef8936ac53da0eed02547995e44da155df9d139d44bb1570d044316d58d3631dca734150430c865
7
+ data.tar.gz: a93d83e01d8511b1c56a4fb00b83ecb0ab8291a4a9d5b45d4148770b074ffb43ff2723d200b5a21bcf1193c964770d06adb42ad0427c7fa4ab3e59e7650c3320
data/README.md CHANGED
@@ -52,7 +52,7 @@ if Micky.head(params[:website_url])
52
52
  url.path = '/favicon.ico'
53
53
 
54
54
  if favicon = Micky.get(url)
55
- # Do whatever with the raw `favicon.data`, for whatever reason
55
+ # Do whatever with the raw `favicon.body`, for whatever reason
56
56
  else
57
57
  # This site has no favicon, or a broken one, too bad
58
58
  end
@@ -105,11 +105,11 @@ Micky.parsers['application/json'] = -> (body) {
105
105
  }
106
106
  ```
107
107
 
108
- Parse images into [mini_magick](https://github.com/minimagick/minimagick)
108
+ Parse images into [MiniMagick](https://github.com/minimagick/minimagick)
109
109
  instances:
110
110
 
111
111
  ```ruby
112
- parser = -> (body) {
112
+ image_parser = -> (body) {
113
113
  begin
114
114
  MiniMagick::Image.read(body)
115
115
  rescue MiniMagick::Invalid
@@ -117,7 +117,7 @@ parser = -> (body) {
117
117
  }
118
118
 
119
119
  %w[image/png image/jpeg image/jpg image/gif].each do |type|
120
- Micky.parsers[type] = parser
120
+ Micky.parsers[type] = image_parser
121
121
  end
122
122
  ```
123
123
 
@@ -125,6 +125,7 @@ end
125
125
 
126
126
  - Add tests
127
127
  - Better document configuration options in README
128
+ - Add `raise_errors: true` option
128
129
 
129
130
  ## Contributing
130
131
 
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, :headers, :parsers].each do |name|
8
+ [: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
@@ -13,25 +13,35 @@ module Micky
13
13
 
14
14
  def get(uri)
15
15
  @request_class_name = 'Get'
16
- request(uri)
16
+ request_with_redirect_handling(uri)
17
17
  end
18
18
 
19
19
  def head(uri)
20
20
  @request_class_name = 'Head'
21
- request(uri)
21
+ request_with_redirect_handling(uri)
22
22
  end
23
23
 
24
24
  private
25
25
 
26
- def request(uri)
27
- @uri = uri
28
- request_with_redirect_handling(0)
29
- end
30
-
31
- def request_with_redirect_handling(redirect_count)
26
+ def request_with_redirect_handling(uri, redirect_count = 0)
32
27
  return log "Max redirects reached (#{@max_redirects})" if redirect_count >= @max_redirects
33
28
 
34
- @uri = Micky::URI(@uri)
29
+ case response = request(uri)
30
+ when Net::HTTPSuccess
31
+ Response.new(response)
32
+ when Net::HTTPRedirection
33
+ uri = response['Location']
34
+ log "Redirect to #{uri}"
35
+ request_with_redirect_handling(uri, redirect_count + 1)
36
+ else
37
+ log response
38
+ log response.body
39
+ nil
40
+ end
41
+ end
42
+
43
+ def request(uri)
44
+ @uri = Micky::URI(uri) or return nil
35
45
 
36
46
  unless @skip_resolve == true
37
47
  # Resolv is the only domain validity check that can be wrapped with Timeout.
@@ -60,24 +70,38 @@ module Micky
60
70
  http.read_timeout = @timeout
61
71
  http.ssl_timeout = @timeout
62
72
 
73
+ # Query string
74
+ query = Hash[::URI.decode_www_form(@uri.query || '')]
75
+
76
+ if @query && @query.any?
77
+ query.merge! Hash[@query.map { |k,v| [k.to_s, v] }]
78
+ @uri.query = ::URI.encode_www_form(query)
79
+ end
80
+
81
+ # OAuth
82
+ if @oauth && @oauth.any?
83
+ unless defined? SimpleOAuth
84
+ begin
85
+ require 'simple_oauth'
86
+ rescue LoadError
87
+ raise 'You must install the simple_oauth gem to use the :oauth argument.'
88
+ end
89
+ end
90
+
91
+ uri_without_query = @uri.dup
92
+ uri_without_query.query = ''
93
+ header = SimpleOAuth::Header.new(@request_class_name, uri_without_query, query, @oauth).to_s
94
+ @headers['Authorization'] = header
95
+ end
96
+
63
97
  # Request
64
98
  request = Net::HTTP.const_get(@request_class_name).new(@uri)
65
- @headers.each { |k,v| request[k] = v }
66
99
 
67
- response = http.request(request)
100
+ # Headers
101
+ @headers.each { |k,v| request[k] = v }
68
102
 
69
- case response
70
- when Net::HTTPSuccess
71
- Response.new(response)
72
- when Net::HTTPRedirection
73
- log "Redirect to #{response['Location']}"
74
- @uri = response['Location']
75
- request_with_redirect_handling(redirect_count + 1)
76
- else
77
- log response
78
- nil
79
- end
80
- rescue Timeout::Error, ::URI::InvalidURIError, OpenSSL::SSL::SSLError, SystemCallError, SocketError => e
103
+ http.request(request)
104
+ rescue Timeout::Error, OpenSSL::SSL::SSLError, SystemCallError, SocketError => e
81
105
  log e
82
106
  nil
83
107
  end
data/lib/micky/uri.rb CHANGED
@@ -1,12 +1,30 @@
1
- require 'delegate'
2
1
  require 'uri'
3
2
 
4
3
  module Micky
5
- HTTP_URI_REGEX = /\Ahttps?:\/\//
4
+ HTTP_URI_REGEX = %r{\Ahttps?://}
6
5
 
7
6
  def self.URI(uri)
8
7
  uri = uri.to_s.strip
9
8
  uri = "http://#{uri}" if uri !~ HTTP_URI_REGEX
10
- ::URI.parse(uri) rescue ::URI.parse(::URI.encode(uri))
9
+ begin
10
+ ::URI.parse(uri)
11
+ rescue ::URI::InvalidURIError
12
+ begin
13
+ ::URI.parse(::URI.encode(uri))
14
+ rescue ::URI::InvalidURIError
15
+ end
16
+ end
17
+ end
18
+
19
+ module URI
20
+ def self.extract(text)
21
+ ::URI.extract(text).select { |uri|
22
+ begin
23
+ ::URI.parse(uri).is_a? ::URI::HTTP
24
+ rescue ::URI::InvalidURIError
25
+ false
26
+ end
27
+ }
28
+ end
11
29
  end
12
30
  end
data/lib/micky/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Micky
2
- VERSION = '0.1.0'
2
+ VERSION = '0.2.0'
3
3
  end
data/lib/micky.rb CHANGED
@@ -10,6 +10,8 @@ module Micky
10
10
  attr_accessor :timeout
11
11
  attr_accessor :skip_resolve
12
12
  attr_accessor :resolve_timeout
13
+ attr_accessor :oauth
14
+ attr_accessor :query
13
15
  attr_accessor :headers
14
16
  attr_accessor :parsers
15
17
  end
@@ -19,6 +21,8 @@ module Micky
19
21
  @timeout = 5
20
22
  @skip_resolve = false
21
23
  @resolve_timeout = 2
24
+ @oauth = {}
25
+ @query = {}
22
26
  @headers = {}
23
27
  @parsers = {
24
28
  'application/json' => -> (body) {
data/micky.gemspec CHANGED
@@ -13,7 +13,7 @@ Gem::Specification.new do |spec|
13
13
  'exceptions (invalid hosts/URIs, server errors, timeouts, redirect loops), ' \
14
14
  'automatically parses responses (JSON, etc.), is very lightweight, and has no ' \
15
15
  'dependency.'
16
- spec.summary = 'Simple, worry-free HTTP client'
16
+ spec.summary = 'Lightweight and worry-free HTTP client'
17
17
  spec.homepage = 'http://github.com/rafBM/micky'
18
18
  spec.license = 'MIT'
19
19
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: micky
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
  - Rafaël Blais Masson
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-08-30 00:00:00.000000000 Z
11
+ date: 2013-11-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -78,8 +78,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
78
78
  version: '0'
79
79
  requirements: []
80
80
  rubyforge_project:
81
- rubygems_version: 2.0.3
81
+ rubygems_version: 2.1.9
82
82
  signing_key:
83
83
  specification_version: 4
84
- summary: Simple, worry-free HTTP client
84
+ summary: Lightweight and worry-free HTTP client
85
85
  test_files: []