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, options = nil) ⇒ Client

Returns a new instance of Client.



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

def initialize(base_url = nil, options = nil)
  @base_url = base_url || ''
  options ||= {}
  @headers = Headers.new(options.delete(:headers))
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



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

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

#auth(header_value) ⇒ Object



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

def auth(header_value)
  @headers.set 'Authorization', header_value
  self
end

#basic_auth(username, password) ⇒ Object



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

def basic_auth(username, password)
  header_value = 'Basic ' + Base64.encode("#{username}:#{password}")
  auth(header_value)
  self
end

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



56
57
58
# File 'lib/common/http/client.rb', line 56

def delete(path, options = nil, &callback)
  request(:delete, path, options, &callback)
end

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



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

def get(path, options = nil, &callback)
  request(:get, path, options, &callback)
end

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



60
61
62
# File 'lib/common/http/client.rb', line 60

def head(path, options = nil, &callback)
  request(:head, path, options, &callback)
end

#header(key, value) ⇒ Object



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

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

#headers(hash = nil) ⇒ Object



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

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

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



64
65
66
# File 'lib/common/http/client.rb', line 64

def options(path, options = nil, &callback)
  request(:options, path, options, &callback)
end

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



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

def patch(path, options = nil, &callback)
  request(:patch, path, options, &callback)
end

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



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

def post(path, options = nil, &callback)
  request(:post, path, options, &callback)
end

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



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

def put(path, options = nil, &callback)
  request(:put, path, options, &callback)
end

#request(http_method, path, options = nil, &callback) ⇒ Object



72
73
74
75
76
77
78
79
80
# File 'lib/common/http/client.rb', line 72

def request(http_method, path, options = nil, &callback)
  options ||= {}
  headers_dup = headers.dup
  if options[:headers]
    options.delete(:headers).each {|key, value| headers_dup.set(key, value) }
  end
  options[:headers] = headers_dup
  Request.new(http_method, base_url + path, options).perform(&callback)
end

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



68
69
70
# File 'lib/common/http/client.rb', line 68

def trace(path, options = nil, &callback)
  request(:trace, path, options, &callback)
end