Class: PollEverywhere::HTTP::RequestBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/polleverywhere/http/request_builder.rb

Overview

DSL for building requests within our application that build a Request object and send them to an adapter to fulfill the request.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(adapter = PollEverywhere.config.http_adapter, base_url = PollEverywhere.config.url) {|_self| ... } ⇒ RequestBuilder

Returns a new instance of RequestBuilder.

Yields:

  • (_self)

Yield Parameters:


8
9
10
11
12
13
14
15
16
17
18
# File 'lib/polleverywhere/http/request_builder.rb', line 8

def initialize(adapter=PollEverywhere.config.http_adapter, base_url=PollEverywhere.config.url)
  self.adapter = adapter
  self.base_url = base_url
  self.headers = { 
    'Content-Type' => 'application/json',
    'Accept' => 'application/json',
    'Authorization' => PollEverywhere.config.http_basic_credentials
  }
  
  yield self if block_given?
end

Instance Attribute Details

#adapterObject

Returns the value of attribute adapter


6
7
8
# File 'lib/polleverywhere/http/request_builder.rb', line 6

def adapter
  @adapter
end

#base_urlObject

Returns the value of attribute base_url


6
7
8
# File 'lib/polleverywhere/http/request_builder.rb', line 6

def base_url
  @base_url
end

#bodyObject

Returns the value of attribute body


6
7
8
# File 'lib/polleverywhere/http/request_builder.rb', line 6

def body
  @body
end

#formatObject

Returns the value of attribute format


6
7
8
# File 'lib/polleverywhere/http/request_builder.rb', line 6

def format
  @format
end

#headersObject

Returns the value of attribute headers


6
7
8
# File 'lib/polleverywhere/http/request_builder.rb', line 6

def headers
  @headers
end

#methodObject

Returns the value of attribute method


6
7
8
# File 'lib/polleverywhere/http/request_builder.rb', line 6

def method
  @method
end

#paramsObject

Returns the value of attribute params


6
7
8
# File 'lib/polleverywhere/http/request_builder.rb', line 6

def params
  @params
end

#response(&block) ⇒ Object

Generate a Request object, send it to an adapter, and have it fullfill the response by yielding a Response object.


59
60
61
# File 'lib/polleverywhere/http/request_builder.rb', line 59

def response
  @response
end

#urlObject

Returns the value of attribute url


6
7
8
# File 'lib/polleverywhere/http/request_builder.rb', line 6

def url
  @url
end

Instance Method Details

#as(format) ⇒ Object

Specify the mime-type/format of the request.


48
49
50
51
# File 'lib/polleverywhere/http/request_builder.rb', line 48

def as(format)
  self.format = format
  self
end

#delete(params = {}) ⇒ Object


35
36
37
38
# File 'lib/polleverywhere/http/request_builder.rb', line 35

def delete(params={})
  self.method, self.params = :delete, params
  self
end

#get(params = {}) ⇒ Object


30
31
32
33
# File 'lib/polleverywhere/http/request_builder.rb', line 30

def get(params={})
  self.method, self.params = :get, params
  self
end

#post(body = nil) ⇒ Object


25
26
27
28
# File 'lib/polleverywhere/http/request_builder.rb', line 25

def post(body=nil)
  self.method, self.body = :post, body
  self
end

#put(body = nil) ⇒ Object


20
21
22
23
# File 'lib/polleverywhere/http/request_builder.rb', line 20

def put(body=nil)
  self.method, self.body = :put, body
  self
end

#requestObject


53
54
55
# File 'lib/polleverywhere/http/request_builder.rb', line 53

def request
  Request.new(method, url_for(url), headers, body)
end

#to(url) ⇒ Object Also known as: from

Specify the URL of the request.


41
42
43
44
# File 'lib/polleverywhere/http/request_builder.rb', line 41

def to(url)
  self.url = url
  self
end