Module: AFMotion::ClientShared

Included in:
AFHTTPSessionManager
Defined in:
lib/afmotion/client_shared.rb

Instance Method Summary collapse

Instance Method Details

#all_headersObject



7
8
9
# File 'lib/afmotion/client_shared.rb', line 7

def all_headers
  requestSerializer.HTTPRequestHeaders
end

#authorization=(authorization) ⇒ Object



11
12
13
# File 'lib/afmotion/client_shared.rb', line 11

def authorization=(authorization)
  requestSerializer.authorization = authorization
end

#create_multipart_task(http_method, path, options = {}, &callback) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/afmotion/client_shared.rb', line 23

def create_multipart_task(http_method, path, options = {}, &callback)
  parameters = options[:params]
  headers = options.fetch(:headers, {})
  progress = options[:progress_block]

  inner_callback = Proc.new do |result, form_data|
    case callback.arity
    when 1
      callback.call(result)
    when 2
      callback.call(result, form_data)
    end
  end

  multipart_callback = nil
  if callback.arity > 1
    multipart_callback = lambda { |formData|
      inner_callback.call(nil, formData)
    }
  end

  http_method = http_method.to_s.upcase
  if http_method == "POST"
    task = self.POST(path,
      parameters: parameters,
      headers: headers,
      constructingBodyWithBlock: multipart_callback,
      progress: progress,
      success: success_block_for_http_method(:post, inner_callback),
      failure: failure_block(inner_callback))
  else
    task = self.PUT(path,
      parameters: parameters,
      headers: headers,
      constructingBodyWithBlock: multipart_callback,
      progress: progress,
      success: success_block_for_http_method(:post, inner_callback),
      failure: failure_block(inner_callback))
  end
  task
end

#create_task(http_method, path, options = {}, &callback) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/afmotion/client_shared.rb', line 65

def create_task(http_method, path, options = {}, &callback)
  parameters = options.fetch(:params, {})
  headers = options.fetch(:headers, {})
  progress = options[:progress_block]

  method_signature = "#{http_method.to_s.upcase}:parameters:headers:progress:success:failure"
  success = success_block_for_http_method(http_method, callback)
  failure = failure_block(callback)
  method_and_args = [method_signature, path, parameters, headers, progress, success, failure]

  # HEAD doesn't take a progress arg
  if http_method.to_s.upcase == "HEAD"
    method_signature.gsub!("progress:", "")
    method_and_args.delete_at(4)
  end

  self.public_send(*method_and_args)
end

#failure_block(callback) ⇒ Object



98
99
100
101
102
103
# File 'lib/afmotion/client_shared.rb', line 98

def failure_block(callback)
  ->(task, error) {
    result = AFMotion::HTTPResult.new(task, nil, error)
    callback.call(result)
  }
end

#headersObject



3
4
5
# File 'lib/afmotion/client_shared.rb', line 3

def headers
  requestSerializer.headers
end

#multipart_post(path, options = {}, &callback) ⇒ Object



15
16
17
# File 'lib/afmotion/client_shared.rb', line 15

def multipart_post(path, options = {}, &callback)
  create_multipart_task(:post, path, options, &callback)
end

#multipart_put(path, options = {}, &callback) ⇒ Object



19
20
21
# File 'lib/afmotion/client_shared.rb', line 19

def multipart_put(path, options = {}, &callback)
  create_multipart_task(:put, path, options, &callback)
end

#success_block_for_http_method(http_method, callback) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/afmotion/client_shared.rb', line 84

def success_block_for_http_method(http_method, callback)
  if http_method.downcase.to_sym == :head
    return ->(task) {
      result = AFMotion::HTTPResult.new(task, nil, nil)
      callback.call(result)
    }
  end

  ->(task, responseObject) {
    result = AFMotion::HTTPResult.new(task, responseObject, nil)
    callback.call(result)
  }
end