Class: Motion::HTTP::Client

Inherits:
Object show all
Defined in:
lib/common/http/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base_url = nil) ⇒ Client

Returns a new instance of Client.



6
7
8
9
# File 'lib/common/http/client.rb', line 6

def initialize(base_url = nil)
  @base_url = base_url || ''
  @headers = Headers.new
end

Instance Attribute Details

#base_urlObject (readonly)

Returns the value of attribute base_url.



4
5
6
# File 'lib/common/http/client.rb', line 4

def base_url
  @base_url
end

Instance Method Details

#add_header(key, value) ⇒ Object



15
16
17
# File 'lib/common/http/client.rb', line 15

def add_header(key, value)
  @headers.add(key, value)
end

#delete(path, params = nil, options = nil, &callback) ⇒ Object



51
52
53
# File 'lib/common/http/client.rb', line 51

def delete(path, params = nil, options = nil, &callback)
  Request.new(:delete, base_url + path, headers, params, options).perform(&callback)
end

#get(path, params = nil, options = nil, &callback) ⇒ Object

FIXME: doesn't work on Android for some reason [:get, :post, :put, :patch, :delete].each do |method| define_method "#method", do |path, params = nil, options = nil, &callback| Request.new(method, base_url + path, headers, params, options).perform(&callback) end end



35
36
37
# File 'lib/common/http/client.rb', line 35

def get(path, params = nil, options = nil, &callback)
  Request.new(:get, base_url + path, headers, params, options).perform(&callback)
end

#header(key, value) ⇒ Object



11
12
13
# File 'lib/common/http/client.rb', line 11

def header(key, value)
  @headers.set(key, value)
end

#headers(hash = nil) ⇒ Object



19
20
21
22
23
24
25
26
# File 'lib/common/http/client.rb', line 19

def headers(hash = nil)
  if hash
    hash.each do |key, value|
      @headers.set(key, value)
    end
  end
  @headers
end

#patch(path, params = nil, options = nil, &callback) ⇒ Object



47
48
49
# File 'lib/common/http/client.rb', line 47

def patch(path, params = nil, options = nil, &callback)
  Request.new(:patch, base_url + path, headers, params, options).perform(&callback)
end

#post(path, params = nil, options = nil, &callback) ⇒ Object



39
40
41
# File 'lib/common/http/client.rb', line 39

def post(path, params = nil, options = nil, &callback)
  Request.new(:post, base_url + path, headers, params, options).perform(&callback)
end

#put(path, params = nil, options = nil, &callback) ⇒ Object



43
44
45
# File 'lib/common/http/client.rb', line 43

def put(path, params = nil, options = nil, &callback)
  Request.new(:put, base_url + path, headers, params, options).perform(&callback)
end