Class: Diplomat::Query
- Inherits:
-
RestClient
- Object
- RestClient
- Diplomat::Query
- Includes:
- ApiOptions
- Defined in:
- lib/diplomat/query.rb
Overview
Methods for interacting with the Consul query API endpoint
Instance Method Summary collapse
-
#create(definition, options = nil) ⇒ String
Create a prepared query or prepared query template.
-
#delete(key, options = nil) ⇒ Boolean
Delete a prepared query or prepared query template.
-
#execute(key, options = nil) ⇒ OpenStruct
Execute a prepared query or prepared query template rubocop:disable PerceivedComplexity, CyclomaticComplexity, AbcSize.
-
#explain(key, options = nil) ⇒ OpenStruct
Get the fully rendered query template.
-
#get(key, options = nil) ⇒ OpenStruct
Get a prepared query by it’s key.
-
#get_all(options = nil) ⇒ OpenStruct
Get all prepared queries.
-
#update(key, definition, options = nil) ⇒ Boolean
Update a prepared query or prepared query template.
Methods included from ApiOptions
#check_acl_token, #use_cas, #use_consistency, #valid_transaction_verbs, #valid_value_transactions
Methods inherited from RestClient
access_method?, #concat_url, #initialize, method_missing, respond_to?, respond_to_missing?, #use_named_parameter
Constructor Details
This class inherits a constructor from Diplomat::RestClient
Instance Method Details
#create(definition, options = nil) ⇒ String
Create a prepared query or prepared query template
41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/diplomat/query.rb', line 41 def create(definition, = nil) url = ['/v1/query'] url += check_acl_token url << use_named_parameter('dc', [:dc]) if && [:dc] @raw = @conn.post do |req| req.url concat_url url req.body = JSON.dump(definition) end parse_body rescue Faraday::ClientError raise Diplomat::QueryAlreadyExists end |
#delete(key, options = nil) ⇒ Boolean
Delete a prepared query or prepared query template
59 60 61 62 63 64 65 66 |
# File 'lib/diplomat/query.rb', line 59 def delete(key, = nil) url = ["/v1/query/#{key}"] url += check_acl_token url << use_named_parameter('dc', [:dc]) if && [:dc] ret = @conn.delete concat_url url ret.status == 200 end |
#execute(key, options = nil) ⇒ OpenStruct
Execute a prepared query or prepared query template rubocop:disable PerceivedComplexity, CyclomaticComplexity, AbcSize
95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/diplomat/query.rb', line 95 def execute(key, = nil) url = ["/v1/query/#{key}/execute"] url += check_acl_token url << use_named_parameter('dc', [:dc]) if && [:dc] url << use_named_parameter('near', [:near]) if && [:near] url << use_named_parameter('limit', [:limit]) if && [:limit] ret = @conn.get concat_url url OpenStruct.new JSON.parse(ret.body) rescue Faraday::ClientError raise Diplomat::QueryNotFound end |
#explain(key, options = nil) ⇒ OpenStruct
Get the fully rendered query template
113 114 115 116 117 118 119 120 121 122 |
# File 'lib/diplomat/query.rb', line 113 def explain(key, = nil) url = ["/v1/query/#{key}/explain"] url += check_acl_token url << use_named_parameter('dc', [:dc]) if && [:dc] ret = @conn.get concat_url url OpenStruct.new JSON.parse(ret.body) rescue Faraday::ClientError raise Diplomat::QueryNotFound end |
#get(key, options = nil) ⇒ OpenStruct
Get a prepared query by it’s key
12 13 14 15 16 17 18 19 20 21 |
# File 'lib/diplomat/query.rb', line 12 def get(key, = nil) url = ["/v1/query/#{key}"] url += check_acl_token url << use_named_parameter('dc', [:dc]) if && [:dc] ret = @conn.get concat_url url JSON.parse(ret.body).map { |query| OpenStruct.new query } rescue Faraday::ClientError raise Diplomat::QueryNotFound end |
#get_all(options = nil) ⇒ OpenStruct
Get all prepared queries
26 27 28 29 30 31 32 33 34 35 |
# File 'lib/diplomat/query.rb', line 26 def get_all( = nil) url = ['/v1/query'] url += check_acl_token url << use_named_parameter('dc', [:dc]) if && [:dc] ret = @conn.get concat_url url JSON.parse(ret.body).map { |query| OpenStruct.new query } rescue Faraday::ClientError raise Diplomat::PathNotFound end |
#update(key, definition, options = nil) ⇒ Boolean
Update a prepared query or prepared query template
73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/diplomat/query.rb', line 73 def update(key, definition, = nil) url = ["/v1/query/#{key}"] url += check_acl_token url << use_named_parameter('dc', [:dc]) if && [:dc] json_definition = JSON.dump(definition) ret = @conn.put do |req| req.url concat_url url req.body = json_definition end ret.status == 200 end |