Class: QTest::REST::QueryBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/qtest/rest/query_builder.rb

Constant Summary collapse

RESOURCES =
[
  :project,
  :test_case,
  :release,
  :test_cycle,
  :test_suite,
  :test_run,
  :test_step,
  :module
].freeze

Instance Method Summary collapse

Constructor Details

#initialize(*opts) ⇒ QueryBuilder



17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/qtest/rest/query_builder.rb', line 17

def initialize(*opts)
  RESOURCES.each do |resource|
    define_resource(resource)
    define_resources(resource)
  end

  @options = opts

  @path = []
  @body = {}
  @query = {}
  @headers = {}
  @parent = {}
end

Instance Method Details

#buildObject



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/qtest/rest/query_builder.rb', line 90

def build
  unless @options.include?(:without_api_path)
    @path = [QTest::REST::API::BASE_PATH, @path].flatten
  end

  @query.merge!(@parent)
  sanitize_params!
  sanitize_body!
  encode_body!

  {
    path: @path.join('/'),
    query: @query,
    headers: @headers,
    body: @body
  }
end

#data(params = {}) ⇒ Object Also known as: body



65
66
67
68
69
# File 'lib/qtest/rest/query_builder.rb', line 65

def data(params = {})
  @body.merge!(params)

  self
end

#determine_parent!(opts = {}) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
# File 'lib/qtest/rest/query_builder.rb', line 78

def determine_parent!(opts = {})
  if opts[:release]
    parent(release: opts[:release])
  elsif opts[:test_suite]
    parent(test_suite: opts[:test_suite])
  elsif opts[:test_cycle]
    parent(test_cycle: opts[:test_cycle])
  end

  self
end

#header(key, value) ⇒ Object



58
59
60
61
62
63
# File 'lib/qtest/rest/query_builder.rb', line 58

def header(key, value)
  key = encode_for_header(key)
  @headers[key] = value

  self
end

#options(*opts) ⇒ Object



32
33
34
35
36
37
38
# File 'lib/qtest/rest/query_builder.rb', line 32

def options(*opts)
  opts.each do |opt|
    @options << opt unless @options.include?(opt)
  end

  self
end

#param(key, value) ⇒ Object



72
73
74
75
76
# File 'lib/qtest/rest/query_builder.rb', line 72

def param(key, value)
  @query[key.to_s] = value

  self
end

#parent(value) ⇒ Object Also known as: under



47
48
49
50
51
52
53
54
55
# File 'lib/qtest/rest/query_builder.rb', line 47

def parent(value)
  key = value.keys[0]
  @parent = {
    'parentType' => key.to_s.dasherize,
    'parentId' => value[key]
  }

  self
end

#with(*paths) ⇒ Object



40
41
42
43
44
45
# File 'lib/qtest/rest/query_builder.rb', line 40

def with(*paths)
  paths.map! { |path| path.to_s.tr('_', '-') }

  @path << paths
  self
end