mudbug 0.4.6.6 → 0.4.6.7

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -19,7 +19,7 @@ Install the gem:
19
19
 
20
20
  Or, if using bundler, add to your Gemfile:
21
21
 
22
- gem 'bateman'
22
+ gem 'mudbug', '~> 0'
23
23
 
24
24
  Quick Start
25
25
  -----------
@@ -40,6 +40,7 @@ Usage
40
40
  -----
41
41
  You can pass through persistent [options to rest-client](https://github.com/rest-client/rest-client/blob/master/lib/restclient/request.rb):
42
42
 
43
+ require 'mudbug'
43
44
  mb = Mudbug.new 'google.com', max_redirects: 3
44
45
 
45
46
  Declare what you accept: (optional, default shown)
@@ -74,7 +75,7 @@ Call Mudbug#resource directly for finer-grained response handling:
74
75
  # process resp.body
75
76
  # etc.
76
77
 
77
- Here is the heart of the Mudbug's [response processing](https://github.com/rickhull/mudbug/blob/master/lib/mudbug.rb#L37):
78
+ Here is the heart of the Mudbug's [response processing](https://github.com/rickhull/mudbug/blob/master/lib/mudbug.rb#L45):
78
79
 
79
80
  # this structure declares what we support in the request Accept: header
80
81
  # and defines automatic processing of the response based on the
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.4.6.6
1
+ 0.4.6.7
@@ -20,9 +20,8 @@ unless ARGV.shift == 'skip'
20
20
  accepts.each { |acp|
21
21
  b.accept(acp)
22
22
 
23
- print "GET #{url} [#{acp}] "
23
+ puts "GET #{url} [#{acp}]"
24
24
  b.get path
25
- puts
26
25
  }
27
26
  puts
28
27
  }
@@ -1,3 +1,4 @@
1
+ require 'logger'
1
2
  require 'rest-client'
2
3
  require 'json'
3
4
 
@@ -28,6 +29,43 @@ class Mudbug
28
29
  },
29
30
  }
30
31
 
32
+ def self.log_to dest=nil
33
+ case dest
34
+ when nil, 'stderr', 'STDERR'
35
+ dest = $stderr
36
+ when 'stdout', 'STDOUT'
37
+ dest = $stdout
38
+ when IO
39
+ # do nothing
40
+ when String
41
+ # assume file path, do nothing
42
+ else
43
+ raise "unable to log_to #{dest} (#{dest.class})"
44
+ end
45
+ if defined?(@@log)
46
+ l = Logger.new dest
47
+ l.formatter = @@log.formatter
48
+ l.level = @@log.level
49
+ @@log = l
50
+ else
51
+ @@log = Logger.new dest
52
+ @@log.formatter = proc { |sev, time, progname, msg|
53
+ line = "[#{time.strftime('%Y-%m-%d %H:%M:%S')}] #{sev.to_s.upcase}: "
54
+ line << "(#{progname}) " if progname
55
+ line << msg << "\n"
56
+ }
57
+ @@log.level = Logger::WARN
58
+ end
59
+ @@log
60
+ end
61
+
62
+ def self.log_level=(sym)
63
+ log_to unless defined?(@@log)
64
+ level = Logger.const_get(sym.to_s.upcase)
65
+ raise "unknown log level #{sym}" unless level
66
+ @@log.level = level
67
+ end
68
+
31
69
  # map our internal symbols to HTTP content types
32
70
  # assign q scores based on the parameter order
33
71
  # construct the right side of the Accept: header
@@ -43,39 +81,45 @@ class Mudbug
43
81
  # do stuff based on response's Content-type
44
82
  #
45
83
  def self.process(resp, accept = nil)
46
- case resp.code
47
- when 200..299
48
- # 2xx, OK
49
- else
50
- warn "proceeding with HTTP Status Code #{resp.code}"
84
+ log_to unless defined?(@@log)
85
+
86
+ @@log.debug { "response code: #{resp.code}" }
87
+ @@log.debug { "response headers:\n" << resp.raw_headers.inspect }
88
+
89
+ unless (200..299).include?(resp.code)
90
+ @@log.warn { "processing with HTTP Status Code #{resp.code}" }
51
91
  end
52
92
 
53
93
  # do you even Content-type, bro?
54
94
  ct = resp.headers[:content_type]
55
95
  unless ct
56
- warn "process NOOP -- no response Content-type"
96
+ @@log.warn { "abort processing -- no response Content-type" }
57
97
  return resp.body
58
98
  end
59
99
 
60
100
  # warn if we got Content-type we didn't ask for
61
101
  ct, charset = ct.split(';').map { |s| s.strip }
62
- warn "Asked for #{accept} but got #{ct}" if accept and !accept.include?(ct)
102
+ if accept and !accept.include?(ct)
103
+ @@log.warn { "Asked for #{accept} but got #{ct}" }
104
+ end
63
105
 
64
106
  # process the response for known content types
65
107
  CONTENT.each { |sym, hsh|
66
108
  return hsh[:proc].call(resp.body) if ct == hsh[:type]
67
109
  }
68
110
 
69
- warn "process NOOP -- unrecognized Content-type: #{ct}"
70
- return response.body
111
+ @@log.warn { "abort processing -- unrecognized Content-type: #{ct}" }
112
+ return resp.body
71
113
  end
72
114
 
115
+
73
116
  attr_reader :options
74
117
  attr_accessor :host
75
118
 
76
- def initialize(host, options = nil)
119
+ def initialize(host, options = {})
120
+ self.class.log_to options.delete :log_to
77
121
  @host = host
78
- @options = options || {}
122
+ @options = options
79
123
  accept :json, :html, :text
80
124
  end
81
125
 
@@ -98,10 +142,7 @@ class Mudbug
98
142
  #
99
143
  def resource(path, options = {})
100
144
  path = "/#{path}" unless path[0,1] == '/'
101
- url = "http://#{@host}#{path}"
102
- options = @options.merge(options)
103
-
104
- RestClient::Resource.new(url, options)
145
+ RestClient::Resource.new "http://#{@host}#{path}", @options.merge(options)
105
146
  end
106
147
 
107
148
  # no payload
@@ -39,6 +39,7 @@ task :build do
39
39
 
40
40
  s.add_runtime_dependency "rest-client", ["~> 1"]
41
41
  s.add_runtime_dependency "json", ["~> 1"]
42
+ s.add_runtime_dependency "lager", [">= 0"]
42
43
  s.add_development_dependency "minitest", [">= 0"]
43
44
  s.add_development_dependency "rake", [">= 0"]
44
45
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mudbug
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.6.6
4
+ version: 0.4.6.7
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -43,6 +43,22 @@ dependencies:
43
43
  - - ~>
44
44
  - !ruby/object:Gem::Version
45
45
  version: '1'
46
+ - !ruby/object:Gem::Dependency
47
+ name: lager
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
46
62
  - !ruby/object:Gem::Dependency
47
63
  name: minitest
48
64
  requirement: !ruby/object:Gem::Requirement