Class: Hactor::HTTP::Client

Inherits:
Delegator
  • Object
show all
Defined in:
lib/hactor/http/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Client

Returns a new instance of Client.



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

def initialize(options={})
  @response_class = options[:response_class] || Hactor::HTTP::Response
  @backend = options[:backend] || Faraday.new
end

Instance Attribute Details

#backendObject (readonly)

Returns the value of attribute backend.



8
9
10
# File 'lib/hactor/http/client.rb', line 8

def backend
  @backend
end

#response_classObject (readonly)

Returns the value of attribute response_class.



8
9
10
# File 'lib/hactor/http/client.rb', line 8

def response_class
  @response_class
end

Instance Method Details

#__getobj__Object



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

def __getobj__
  backend
end

#follow(link, options) ⇒ Object



15
16
17
18
19
20
# File 'lib/hactor/http/client.rb', line 15

def follow(link, options)
  context_url = options.fetch(:context_url)

  url = context_url.merge(link.href(options[:expand_with]))
  get(url, options)
end

#get(url, options) ⇒ Object



36
37
38
39
40
41
42
# File 'lib/hactor/http/client.rb', line 36

def get(url, options)
  actor = options[:actor] || Hactor::NullActor.new
  headers = options[:headers]
  response = response_class.new backend.get(url, nil, headers),
                                http_client: self
  actor.call response
end

#traverse(link, options) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/hactor/http/client.rb', line 22

def traverse(link, options)
  context_url = options.fetch(:context_url)
  method = options.fetch(:method).to_s.downcase

  actor = options[:actor] || Hactor::NullActor.new
  headers = options[:headers]
  body = options[:body]

  url = context_url.merge link.href(options[:expand_with])
  response = response_class.new backend.send(method, url, body, headers),
                                http_client: self
  actor.call response
end