Class: Quinoa::Service

Inherits:
Object
  • Object
show all
Defined in:
lib/quinoa/service.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url) ⇒ Service

Returns a new instance of Service.



11
12
13
14
15
16
17
18
19
# File 'lib/quinoa/service.rb', line 11

def initialize url
  self.time = ""
  self.path = ""
  self.authorization = ""
  self.custom_headers = {}
  self.expectations = {}
  self.assertions = {}
  self.url = url
end

Instance Attribute Details

#acceptObject

Returns the value of attribute accept.



9
10
11
# File 'lib/quinoa/service.rb', line 9

def accept
  @accept
end

#assertionsObject

Returns the value of attribute assertions.



9
10
11
# File 'lib/quinoa/service.rb', line 9

def assertions
  @assertions
end

#authorizationObject

Returns the value of attribute authorization.



9
10
11
# File 'lib/quinoa/service.rb', line 9

def authorization
  @authorization
end

#bodyObject

Returns the value of attribute body.



9
10
11
# File 'lib/quinoa/service.rb', line 9

def body
  @body
end

#content_typeObject

Returns the value of attribute content_type.



9
10
11
# File 'lib/quinoa/service.rb', line 9

def content_type
  @content_type
end

#custom_headersObject

Returns the value of attribute custom_headers.



9
10
11
# File 'lib/quinoa/service.rb', line 9

def custom_headers
  @custom_headers
end

#expectationsObject

Returns the value of attribute expectations.



9
10
11
# File 'lib/quinoa/service.rb', line 9

def expectations
  @expectations
end

#pathObject

Returns the value of attribute path.



9
10
11
# File 'lib/quinoa/service.rb', line 9

def path
  @path
end

#responseObject

Returns the value of attribute response.



9
10
11
# File 'lib/quinoa/service.rb', line 9

def response
  @response
end

#timeObject

Returns the value of attribute time.



9
10
11
# File 'lib/quinoa/service.rb', line 9

def time
  @time
end

#urlObject

Returns the value of attribute url.



9
10
11
# File 'lib/quinoa/service.rb', line 9

def url
  @url
end

Instance Method Details

#add_custom_header(custom_header_name, custom_header_value) ⇒ Object



73
74
75
# File 'lib/quinoa/service.rb', line 73

def add_custom_header custom_header_name, custom_header_value
  self.custom_headers.merge! custom_header_name.to_sym => custom_header_value.to_s
end

#add_expected_body_string(value) ⇒ Object



117
118
119
# File 'lib/quinoa/service.rb', line 117

def add_expected_body_string value
  add_expectation  "body", value, "contains"
end

#add_expected_max_response_time(value) ⇒ Object



121
122
123
# File 'lib/quinoa/service.rb', line 121

def add_expected_max_response_time value
  add_expectation  "response_time", value, "under"
end

#add_expected_status(value) ⇒ Object



113
114
115
# File 'lib/quinoa/service.rb', line 113

def add_expected_status value
  add_expectation  "status_code", value, "eq", :fail
end

#check!Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/quinoa/service.rb', line 95

def check!
  #TO DO: Handle case when response is nil
  exit 0 if self.response.nil?
  self.expectations.each do | expectation |
    assertion_item = expectation[0]
    expectation_map = Hash[*expectation][assertion_item]

    self.assertions.merge! get_assertion_record(
      assertion_item,
      expectation_map[:value],
      check_attribute?(
        assertion_item,
        expectation_map[:value],
        expectation_map[:compare_using]),
      expectation_map[:level])
  end
end

#get!(url = nil) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/quinoa/service.rb', line 29

def get! url=nil
  begin
    if url == nil
      get_time {
        RestClient.get(
          self.url + self.path,
          {:accept => self.accept,
           :authorization => self.authorization}.merge!(self.custom_headers)
        )
      }
    else
      get_time {
        RestClient.get(
          url,
          {:accept => self.accept,
           :authorization => self.authorization}.merge!(self.custom_headers)
        )
      }
    end
  rescue => e
    self.response = e.response
  end
end

#post!(url = nil) ⇒ Object



25
26
27
# File 'lib/quinoa/service.rb', line 25

def post! url=nil
  p_methods("post", url)
end

#put!(url = nil) ⇒ Object



21
22
23
# File 'lib/quinoa/service.rb', line 21

def put! url=nil
  p_methods("put", url)
end

#remove_custom_header(custom_header_name) ⇒ Object



77
78
79
# File 'lib/quinoa/service.rb', line 77

def remove_custom_header custom_header_name
  self.custom_headers.delete custom_header_name.to_sym
end

#reportObject



81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/quinoa/service.rb', line 81

def report
  JSON.pretty_generate(
    {
      :health => get_health(self.assertions.map{|a| a[1][:status]}),
      :response => {
        :status_code => self.response.code,
        :response_body => self.response.body,
        :response_time => self.time.real
      },
      :assertions => self.assertions
    }
  )
end

#response_acceptObject



57
58
59
# File 'lib/quinoa/service.rb', line 57

def response_accept
  self.response.headers[:accept]
end

#response_bodyObject



65
66
67
# File 'lib/quinoa/service.rb', line 65

def response_body
  self.response.body
end

#response_codeObject



53
54
55
# File 'lib/quinoa/service.rb', line 53

def response_code
  self.response.code
end

#response_content_typeObject



61
62
63
# File 'lib/quinoa/service.rb', line 61

def response_content_type
  self.response.headers[:content_type]
end

#response_locationObject



69
70
71
# File 'lib/quinoa/service.rb', line 69

def response_location
  self.response.headers[:location]
end