Module: RestCore::Middleware

Includes:
RestCore
Included in:
AuthBasic, Auto, Cache, CommonLogger, DefaultHeaders, DefaultPayload, DefaultQuery, DefaultSite, Defaults, Dry, EmHttpRequest, ErrorDetector, ErrorHandler, FollowRedirect, JsonRequest, JsonResponse, Oauth1Header, Oauth2Header, Oauth2Query, RestClient, Timeout
Defined in:
lib/rest-core/middleware.rb

Constant Summary

UNRESERVED =
/[^a-zA-Z0-9\-\.\_\~]/

Constants included from RestCore

ASYNC, DRY, FAIL, LOG, PROMISE, REQUEST_HEADERS, REQUEST_METHOD, REQUEST_PATH, REQUEST_PAYLOAD, REQUEST_QUERY, RESPONSE_BODY, RESPONSE_HEADERS, RESPONSE_STATUS, RootFiber, TIMER, Universal, VERSION

Class Method Summary (collapse)

Instance Method Summary (collapse)

Methods included from RestCore

eagerload

Class Method Details

+ (Boolean) contain_binary?(payload)

Returns:

  • (Boolean)


95
96
97
98
99
100
101
102
103
104
# File 'lib/rest-core/middleware.rb', line 95

def contain_binary? payload
  return false unless payload
  return true  if     payload.respond_to?(:read)
  return true  if     payload.find{ |k, v|
    # if payload is an array, then v would be nil
    (v || k).respond_to?(:read) ||
    # if v is an array, it could contain binary data
    (v.kind_of?(Array) && v.any?{ |vv| vv.respond_to?(:read) }) }
  return false
end

+ (Object) escape(string)



90
91
92
# File 'lib/rest-core/middleware.rb', line 90

def escape string
  URI.escape(string, UNRESERVED)
end

+ (Object) id

identity function



10
11
12
# File 'lib/rest-core/middleware.rb', line 10

def self.id
  @id ||= lambda{ |a| a }
end

+ (Object) included(mod)



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/rest-core/middleware.rb', line 14

def self.included mod
  mod.send(:include, RestCore)
  mod.send(:attr_reader, :app)
  mem = if mod.respond_to?(:members) then mod.members else [] end
  src = mem.map{ |member| <<-RUBY }
    def #{member} env
      if    env.key?('#{member}')
        env['#{member}']
      else
            @#{member}
      end
    end
  RUBY
  args      = [:app] + mem
  para_list = args.map{ |a| "#{a}=nil"}.join(', ')
  args_list = args                     .join(', ')
  ivar_list = args.map{ |a| "@#{a}"   }.join(', ')
  src << <<-RUBY
    def initialize #{para_list}
      #{ivar_list} = #{args_list}
    end
  RUBY
  accessor = Module.new
  accessor.module_eval(src.join("\n"), __FILE__, __LINE__)
  mod.const_set(:Accessor, accessor)
  mod.send(:include, accessor)
end

+ (Object) percent_encode(query)



78
79
80
81
82
83
84
85
86
# File 'lib/rest-core/middleware.rb', line 78

def percent_encode query
  query.sort.map{ |(k, v)|
    if v.kind_of?(Array)
      v.map{ |vv| "#{escape(k.to_s)}=#{escape(vv.to_s)}" }.join('&')
    else
      "#{escape(k.to_s)}=#{escape(v.to_s)}"
    end
  }.join('&')
end

+ (Object) request_uri(env)



67
68
69
70
71
72
73
74
75
# File 'lib/rest-core/middleware.rb', line 67

def request_uri env
  # compacting the hash
  if (query = (env[REQUEST_QUERY] || {}).select{ |k, v| v }).empty?
    env[REQUEST_PATH].to_s
  else
    q = if env[REQUEST_PATH] =~ /\?/ then '&' else '?' end
    "#{env[REQUEST_PATH]}#{q}#{percent_encode(query)}"
  end
end

+ (Object) string_keys(hash)



107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/rest-core/middleware.rb', line 107

def string_keys hash
  hash.inject({}){ |r, (k, v)|
    if v.kind_of?(Hash)
      r[k.to_s] = case k.to_s
                    when REQUEST_QUERY, REQUEST_PAYLOAD, REQUEST_HEADERS
                      string_keys(v)
                    else;         v
                  end
    else
      r[k.to_s] = v
    end
    r
  }
end

Instance Method Details

- (Object) call(env, &k)



42
# File 'lib/rest-core/middleware.rb', line 42

def call env, &k; app.call(env, &(k || id)); end

- (Object) fail(env, obj)



44
45
46
47
48
49
50
# File 'lib/rest-core/middleware.rb', line 44

def fail env, obj
  if obj
    env.merge(FAIL => (env[FAIL] || []) + [obj])
  else
    env
  end
end

- (Object) id



43
# File 'lib/rest-core/middleware.rb', line 43

def id          ; Middleware.id            ; end

- (Object) log(env, obj)



51
52
53
54
55
56
57
# File 'lib/rest-core/middleware.rb', line 51

def log env, obj
  if obj
    env.merge(LOG  => (env[LOG]  || []) + [obj])
  else
    env
  end
end

- (Object) run(app = app)



58
59
60
61
62
63
64
# File 'lib/rest-core/middleware.rb', line 58

def run app=app
  if app.respond_to?(:app) && app.app
    run(app.app)
  else
    app
  end
end