patchboard 0.4.3 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 46fe7f3a6e42bd2248df577e00d2bf4ebff0de19
4
- data.tar.gz: e5cbc55c390be6754d739326283058c2d120f71c
3
+ metadata.gz: 916ee5e5133154c488707325cb9e4125f7b472da
4
+ data.tar.gz: c78813b303ce9464fe02cf914d72324722ca166e
5
5
  SHA512:
6
- metadata.gz: 8d5bd558a0d07e437dab031f50f291eb82ae334492f8c4bf2284b859b66077f7938224bfa661ea82c926b94880fc3ca060c9fad9ee5bf1e58f2131dd72c821af
7
- data.tar.gz: 5096c602c6aee31da7d65dffb3efe1fec7d1539efdd47854dd16c833118c3e23ff3f57b1dc442bb4d46f1df7cf8071cbc3b81ce01ef2236f4b92cc986e7666b7
6
+ metadata.gz: c2ef016046c0629f3d77a8591ba4d5ab89ddc707f5fb3ab962c47a4a6a8ba87458df7e5cfb4062fb2e7bc00d4a290e31c5710420c6ceb9368b6710d24869f1d8
7
+ data.tar.gz: c0d10a0e3577e0e2f14b0aa3e80ee17f59b25ec37b41ae116786fcab1d47aeb3a077b66a5507559ab5bdc479cb24f47d4363444e3d710f102b772a2c67b6fb7e
@@ -14,13 +14,14 @@ class Patchboard
14
14
  @method = definition[:method]
15
15
 
16
16
 
17
- @headers = {
18
- }
17
+ @headers = {}
19
18
 
20
19
  request, response = definition[:request], definition[:response]
21
20
 
22
21
  if request
23
- @auth_scheme = request[:authorization]
22
+ if schemes = request[:authorization]
23
+ @auth_schemes = schemes.is_a?(String) ? [schemes] : schemes
24
+ end
24
25
  if request[:type]
25
26
  @headers["Content-Type"] = request[:type]
26
27
  @request_schema = @schema_manager.find :media_type => request[:type]
@@ -43,7 +44,8 @@ class Patchboard
43
44
  raw = self.http.request @method, url, options.merge(:response => :object)
44
45
  response = Response.new(raw)
45
46
  if response.status != @status
46
- raise ResponseError.new(response.status, response.body), "Unexpected response status: #{response.status} - #{response.body}"
47
+ raise ResponseError.new(response),
48
+ "Unexpected response status: #{response.status} - #{response.body}"
47
49
  end
48
50
  out = @api.decorate(resource.context, @response_schema, response.data)
49
51
  out.response = response
@@ -57,9 +59,10 @@ class Patchboard
57
59
  :url => url, :method => @method, :headers => headers
58
60
  }
59
61
 
60
- if @auth_scheme && context.respond_to?(:authorizer)
61
- credential = context.authorizer(@auth_scheme, resource, @name)
62
- headers["Authorization"] = "#{@auth_scheme} #{credential}"
62
+ if @auth_schemes && context.respond_to?(:authorizer)
63
+ scheme, credential = context.authorizer(@auth_schemes, resource, @name)
64
+
65
+ headers["Authorization"] = "#{scheme} #{credential}"
63
66
  end
64
67
 
65
68
  input_options = self.process_args(args)
@@ -100,13 +103,16 @@ class Patchboard
100
103
 
101
104
 
102
105
  class ResponseError < StandardError
103
- attr_reader :status
104
- attr_reader :body
105
106
 
106
- def initialize(status, body)
107
- @status = status
108
- @body = body
107
+ attr_reader :response, :status, :body, :headers
108
+
109
+ def initialize(response)
110
+ @response = response
111
+ @status = @response.status
112
+ @body = @response.body
113
+ @headers = @response.headers
109
114
  end
115
+
110
116
  end
111
117
 
112
118
  end
@@ -1,8 +1,49 @@
1
1
  class Patchboard
2
2
  class Response
3
3
 
4
+ module Headers
5
+ module_function
6
+
7
+ ## This example Authorization value has two schemes: Custom and Basic
8
+ ## The Custom scheme has two params, key and smurf
9
+ ## The Basic scheme has one param, realm
10
+ # %q[Custom key="otp.fBvQqSSlsNzJbqZcHKsylg", smurf="blue", Basic realm="foo"]
11
+
12
+ WWWAuthRegex = /
13
+ # keys are not quoted
14
+ ([^\s,]+)
15
+ =
16
+
17
+ # value might be quoted
18
+ "?
19
+ # the value currently may not contain whitespace
20
+ ([^\s,"]+)
21
+ "?
22
+ /x # the x flag means whitespace within the Regex definition is ignored
23
+
24
+ def parse_www_auth(string)
25
+ parsed = {}
26
+ # FIXME: This assumes that no quoted strings have spaces within.
27
+ tokens = string.split(" ")
28
+ name = tokens.shift
29
+ parsed[name] = {}
30
+ while token = tokens.shift
31
+ # Now I have two problems
32
+ if md = WWWAuthRegex.match(token)
33
+ full, key, value = md.to_a
34
+ parsed[name][key] = value
35
+ else
36
+ name = token
37
+ parsed[name] = {}
38
+ end
39
+ end
40
+ parsed
41
+ end
42
+
43
+ end
44
+
4
45
  attr_accessor :resource
5
- attr_reader :raw, :data
46
+ attr_reader :raw, :data, :parsed_headers
6
47
  def initialize(raw)
7
48
  @raw = raw
8
49
  if @raw.headers["Content-Type"]
@@ -10,6 +51,17 @@ class Patchboard
10
51
  @data = JSON.parse @raw.body, :symbolize_names => true
11
52
  end
12
53
  end
54
+ @parsed_headers = {}
55
+ parse_headers
56
+ end
57
+
58
+ def parse_headers
59
+ @raw.headers.each do |name, string|
60
+ case name
61
+ when /www-authenticate/i
62
+ @parsed_headers["WWW-Authenticate"] = Headers.parse_www_auth(string)
63
+ end
64
+ end
13
65
  end
14
66
 
15
67
  def method_missing(name, *args, &block)
data/lib/patchboard.rb CHANGED
@@ -94,17 +94,22 @@ class Patchboard
94
94
 
95
95
  def spawn(context=nil)
96
96
  context ||= @context_creator.call
97
- self.class::Client.new(context, @api, @endpoint_classes)
97
+ self.class::Client.new(self, context, @api, @endpoint_classes)
98
98
  end
99
99
 
100
100
  class Client
101
101
 
102
102
  attr_reader :resources, :context
103
- def initialize(context, api, klasses)
103
+ def initialize(main, context, api, klasses)
104
+ @main = main
104
105
  @context = context
105
106
  @resources = Endpoints.new @context, api, klasses
106
107
  end
107
108
 
109
+ def spawn(context=nil)
110
+ @main.spawn(context)
111
+ end
112
+
108
113
  end
109
114
 
110
115
  def create_classes
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: patchboard
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.3
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Matthew King
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-07-27 00:00:00.000000000 Z
11
+ date: 2014-08-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: json