Module: RestCore::Middleware

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

Constant Summary

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

Constants included from RestCore

ASYNC, CLIENT, DRY, FAIL, HIJACK, LOG, PROMISE, REQUEST_HEADERS, REQUEST_METHOD, REQUEST_PATH, REQUEST_PAYLOAD, REQUEST_QUERY, REQUEST_URI, RESPONSE_BODY, RESPONSE_HEADERS, RESPONSE_KEY, RESPONSE_SOCKET, RESPONSE_STATUS, TIMER, Universal, VERSION

Class Method Summary (collapse)

Instance Method Summary (collapse)

Methods included from RestCore

eagerload, id

Class Method Details

+ (Boolean) contain_binary?(payload)

Returns:

  • (Boolean)


89
90
91
92
93
94
95
96
97
98
# File 'lib/rest-core/middleware.rb', line 89

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)



84
85
86
# File 'lib/rest-core/middleware.rb', line 84

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

+ (Object) included(mod)



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/rest-core/middleware.rb', line 8

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)



72
73
74
75
76
77
78
79
80
# File 'lib/rest-core/middleware.rb', line 72

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)



61
62
63
64
65
66
67
68
69
# File 'lib/rest-core/middleware.rb', line 61

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)



101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/rest-core/middleware.rb', line 101

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)



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

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

- (Object) fail(env, obj)



38
39
40
41
42
43
44
# File 'lib/rest-core/middleware.rb', line 38

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

- (Object) id



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

def id          ; RC.id                    ; end

- (Object) log(env, obj)



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

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

- (Object) run(app = app)



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

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