Class: Spectre::Http::SpectreHttpRequest

Inherits:
DslClass show all
Defined in:
lib/spectre/http.rb,
lib/spectre/http/keystone.rb,
lib/spectre/http/basic_auth.rb

Defined Under Namespace

Classes: Headers

Instance Method Summary collapse

Methods inherited from DslClass

#_evaluate, #_execute, #method_missing

Constructor Details

#initialize(request) ⇒ SpectreHttpRequest

Returns a new instance of SpectreHttpRequest.



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

def initialize request
  @__req = request
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Spectre::DslClass

Instance Method Details

#authenticate(method) ⇒ Object Also known as: auth



96
97
98
# File 'lib/spectre/http.rb', line 96

def authenticate method
  @__req['auth'] = method
end

#basic_auth(username, password) ⇒ Object



5
6
7
8
9
10
11
12
# File 'lib/spectre/http/basic_auth.rb', line 5

def basic_auth username, password
  @__req['basic_auth'] = {} unless @__req.key? 'basic_auth'

  @__req['basic_auth']['username'] = username
  @__req['basic_auth']['password'] = password

  @__req['auth'] = 'basic_auth'
end

#body(body_content) ⇒ Object



84
85
86
# File 'lib/spectre/http.rb', line 84

def body body_content
  @__req['body'] = body_content.to_s
end

#certificate(path) ⇒ Object Also known as: cert



104
105
106
# File 'lib/spectre/http.rb', line 104

def certificate path
  @__req['cert'] = path
end

#content_type(media_type) ⇒ Object Also known as: media_type



73
74
75
# File 'lib/spectre/http.rb', line 73

def content_type media_type
  @__req['content_type'] = media_type
end

#ensure_success!Object



88
89
90
# File 'lib/spectre/http.rb', line 88

def ensure_success!
  @__req['ensure_success'] = true
end

#ensure_success?Boolean

Returns:

  • (Boolean)


92
93
94
# File 'lib/spectre/http.rb', line 92

def ensure_success?
  @__req['ensure_success']
end

#header(name, value) ⇒ Object



63
64
65
66
# File 'lib/spectre/http.rb', line 63

def header name, value
  @__req['headers'] ||= []
  @__req['headers'].append [name, value.to_s.strip]
end

#json(data) ⇒ Object



77
78
79
80
81
82
# File 'lib/spectre/http.rb', line 77

def json data
  data = data.to_h if data.is_a? OpenStruct
  body JSON.pretty_generate(data)

  content_type('application/json') unless @__req['content_type']
end

#keystone(url, username, password, project, domain, cert = nil) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/spectre/http/keystone.rb', line 5

def keystone url, username, password, project, domain, cert=nil
  @__req['keystone'] = {} unless @__req.key? 'keystone'

  @__req['keystone']['url'] = url
  @__req['keystone']['username'] = username
  @__req['keystone']['password'] = password
  @__req['keystone']['project'] = project
  @__req['keystone']['domain'] = domain
  @__req['keystone']['cert'] = cert

  @__req['auth'] = 'keystone'
end

#method(method_name) ⇒ Object



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

def method method_name
  @__req['method'] = method_name.upcase
end

#no_auth!Object



100
101
102
# File 'lib/spectre/http.rb', line 100

def no_auth!
  @__req['auth'] = 'none'
end

#no_log!Object



112
113
114
# File 'lib/spectre/http.rb', line 112

def no_log!
  @__req['no_log'] = true
end

#param(name, value) ⇒ Object



68
69
70
71
# File 'lib/spectre/http.rb', line 68

def param name, value
  @__req['query'] ||= []
  @__req['query'].append [name, value.to_s.strip]
end

#path(url_path) ⇒ Object



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

def path url_path
  @__req['path'] = url_path
end

#retries(count) ⇒ Object



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

def retries count
  @__req['retries'] = count
end

#timeout(seconds) ⇒ Object



55
56
57
# File 'lib/spectre/http.rb', line 55

def timeout seconds
  @__req['timeout'] = seconds
end

#to_sObject



116
117
118
# File 'lib/spectre/http.rb', line 116

def to_s
  @__req.to_s
end

#url(base_url) ⇒ Object



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

def url base_url
  @__req['base_url'] = base_url
end

#use_ssl!Object



108
109
110
# File 'lib/spectre/http.rb', line 108

def use_ssl!
  @__req['use_ssl'] = true
end