Class: Motion::HTTP::Client

Inherits:
Object
  • 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

#headersObject (readonly)

Returns the value of attribute headers.



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

def headers
  @headers
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

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

FIXME: doesn’t work on Android [:get, :post, :put, :patch, :delete].each do |method|

define_method "#{method}", do |path, params = nil, &callback|
  Request.new(method, base_url + path, headers, params, &callback).call
end

end



26
27
28
# File 'lib/common/http/client.rb', line 26

def get(path, params = nil, &callback)
  Request.new(:get, base_url + path, headers, params, &callback).call
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

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



30
31
32
# File 'lib/common/http/client.rb', line 30

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