ridley 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -10,7 +10,6 @@ A reliable Chef API client with a clean syntax
10
10
 
11
11
  ## Known Issues
12
12
 
13
- * Search has not been implemented
14
13
  * Sandboxes has not been implemented
15
14
  * Full support for Cookbooks is not included
16
15
  * Acceptance test suite needs to be refactored
@@ -268,6 +267,12 @@ Unlike a role, node, client, or environment, a data bag is a container for other
268
267
  dbi[:host] = "reset.local"
269
268
  dbi.save => true
270
269
 
270
+ ## Searching
271
+
272
+ conn = Ridley.connection(...)
273
+ conn.search(:node)
274
+ conn.search(:node, "name:ridley-test.local")
275
+
271
276
  # Authors and Contributors
272
277
 
273
278
  * Jamie Winsor (<jamie@vialstudios.com>)
@@ -27,6 +27,7 @@ module Ridley
27
27
  autoload :DataBag, 'ridley/resources/data_bag'
28
28
  autoload :DataBagItem, 'ridley/resources/data_bag_item'
29
29
  autoload :Cookbook, 'ridley/resources/cookbook'
30
+ autoload :Search, 'ridley/resources/search'
30
31
 
31
32
  class << self
32
33
  def connection(*args)
@@ -0,0 +1,104 @@
1
+ module Ridley
2
+ class Search
3
+ class << self
4
+ # Returns an array of possible search indexes to be search on
5
+ #
6
+ # @param [Ridley::Connection]
7
+ #
8
+ # @return [Array<String, Symbol]
9
+ def indexes(connection)
10
+ connection.get("search").body.collect { |name, _| name }
11
+ end
12
+ end
13
+
14
+ attr_reader :connection
15
+ attr_reader :index
16
+ attr_reader :query
17
+
18
+ attr_accessor :sort
19
+ attr_accessor :rows
20
+ attr_accessor :start
21
+
22
+ def initialize(connection, index, query, options = {})
23
+ @connection = connection
24
+ @index = index
25
+ @query = query
26
+
27
+ @sort = options[:sort]
28
+ @rows = options[:rows]
29
+ @start = options[:start]
30
+ end
31
+
32
+ # Executes the built up query on the search's connection
33
+ #
34
+ # @example
35
+ # Search.new(connection, :role)
36
+ # search.run =>
37
+ # {
38
+ # total: 1,
39
+ # start: 0,
40
+ # rows: [
41
+ # {
42
+ # name: "ridley-test-role",
43
+ # default_attributes: {},
44
+ # json_class: "Chef::Role",
45
+ # env_run_lists: {},
46
+ # run_list: [],
47
+ # description: "a test role for Ridley!",
48
+ # chef_type: "role",
49
+ # override_attributes: {}
50
+ # }
51
+ # ]
52
+ # }
53
+ #
54
+ # @return [Hash]
55
+ def run
56
+ connection.get(query_uri, query_options).body
57
+ end
58
+
59
+ private
60
+
61
+ def query_uri
62
+ File.join("search", self.index.to_s)
63
+ end
64
+
65
+ def query_options
66
+ {}.tap do |options|
67
+ options[:q] = self.query unless self.query.nil?
68
+ options[:sort] = self.sort unless self.sort.nil?
69
+ options[:rows] = self.rows unless self.rows.nil?
70
+ options[:start] = self.start unless self.start.nil?
71
+ end
72
+ end
73
+ end
74
+
75
+ module DSL
76
+ # Creates an runs a new Ridley::Search
77
+ #
78
+ # @see Ridley::Search#run
79
+ #
80
+ # @param [String, Symbol] index
81
+ # @param [String, nil] query
82
+ #
83
+ # @option options [String] :sort
84
+ # @option options [Integer] :rows
85
+ # @option options [Integer] :start
86
+ #
87
+ # @return [Hash]
88
+ def search(index, query = nil, options = {})
89
+ Search.new(self, index, query, options).run
90
+ end
91
+
92
+ # Return the array of all possible search indexes for the including connection
93
+ #
94
+ # @example
95
+ # conn = Ridley.connection(...)
96
+ # conn.search_indexes =>
97
+ # [:client, :environment, :node, :role, :"ridley-two", :"ridley-one"]
98
+ #
99
+ # @return [Array<Symbol, String>]
100
+ def search_indexes
101
+ Search.indexes(self)
102
+ end
103
+ end
104
+ end
@@ -1,3 +1,3 @@
1
1
  module Ridley
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -14,6 +14,7 @@ Gem::Specification.new do |s|
14
14
  s.name = "ridley"
15
15
  s.require_paths = ["lib"]
16
16
  s.version = Ridley::VERSION
17
+ s.required_ruby_version = ">= 1.9.1"
17
18
 
18
19
  s.add_runtime_dependency 'yajl-ruby'
19
20
  s.add_runtime_dependency 'mixlib-log'
@@ -0,0 +1,56 @@
1
+ require 'spec_helper'
2
+
3
+ describe "Role API operations", type: "acceptance" do
4
+ let(:server_url) { "https://api.opscode.com" }
5
+ let(:client_name) { "reset" }
6
+ let(:client_key) { "/Users/reset/.chef/reset.pem" }
7
+ let(:organization) { "ridley" }
8
+
9
+ let(:connection) do
10
+ Ridley.connection(
11
+ server_url: server_url,
12
+ client_name: client_name,
13
+ client_key: client_key,
14
+ organization: organization
15
+ )
16
+ end
17
+
18
+ before(:all) { WebMock.allow_net_connect! }
19
+ after(:all) { WebMock.disable_net_connect! }
20
+
21
+ describe "listing indexes" do
22
+ it "returns an array of indexes" do
23
+ indexes = connection.search_indexes
24
+
25
+ indexes.should include(:role)
26
+ indexes.should include(:node)
27
+ indexes.should include(:client)
28
+ end
29
+ end
30
+
31
+ describe "showing an index" do
32
+ before(:each) do
33
+ @result = connection.search(:node)
34
+ end
35
+
36
+ it "returns a hash with a total key" do
37
+ @result.should have_key(:total)
38
+ end
39
+
40
+ it "returns a hash with a start key" do
41
+ @result.should have_key(:start)
42
+ end
43
+
44
+ it "returns a hash with a rows key" do
45
+ @result.should have_key(:rows)
46
+ end
47
+ end
48
+
49
+ describe "searching an index that doesn't exist" do
50
+ it "it raises a Ridley::Errors::HTTPNotFound error" do
51
+ lambda {
52
+ connection.search(:notthere)
53
+ }.should raise_error(Ridley::Errors::HTTPNotFound)
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,65 @@
1
+ require 'spec_helper'
2
+
3
+ describe Ridley::Search do
4
+ let(:connection) { double('connection') }
5
+ let(:index) { :role }
6
+ let(:query) { "*:*" }
7
+ let(:response) { double("response", body: Hash.new) }
8
+
9
+ describe "ClassMethods" do
10
+ subject { Ridley::Search }
11
+
12
+ describe "::indexes" do
13
+ it "sends a get request to the connection to receive the indexes" do
14
+ connection.should_receive(:get).with("search").and_return(response)
15
+
16
+ subject.indexes(connection)
17
+ end
18
+ end
19
+ end
20
+
21
+ describe "#run" do
22
+ subject do
23
+ Ridley::Search.new(connection, index, query)
24
+ end
25
+
26
+ it "sends a get request to the connection to the index's location with the given query" do
27
+ connection.should_receive(:get).with("search/#{index}", q: query).and_return(response)
28
+
29
+ subject.run
30
+ end
31
+
32
+ context "when 'sort' is set" do
33
+ let(:sort) { "DESC" }
34
+ before(:each) { subject.sort = sort }
35
+
36
+ it "sends a get request to the connection with a query parameter for 'set'" do
37
+ connection.should_receive(:get).with("search/#{index}", q: query, sort: sort).and_return(response)
38
+
39
+ subject.run
40
+ end
41
+ end
42
+
43
+ context "when 'start' is set" do
44
+ let(:start) { 1 }
45
+ before(:each) { subject.start = start }
46
+
47
+ it "sends a get request to the connection with a query parameter for 'start'" do
48
+ connection.should_receive(:get).with("search/#{index}", q: query, start: start).and_return(response)
49
+
50
+ subject.run
51
+ end
52
+ end
53
+
54
+ context "when 'rows' is set" do
55
+ let(:rows) { 1 }
56
+ before(:each) { subject.rows = rows }
57
+
58
+ it "sends a get request to the connection with a query parameter for 'rows'" do
59
+ connection.should_receive(:get).with("search/#{index}", q: query, rows: rows).and_return(response)
60
+
61
+ subject.run
62
+ end
63
+ end
64
+ end
65
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ridley
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-08-21 00:00:00.000000000 Z
12
+ date: 2012-08-22 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: yajl-ruby
@@ -363,6 +363,7 @@ files:
363
363
  - lib/ridley/resources/environment.rb
364
364
  - lib/ridley/resources/node.rb
365
365
  - lib/ridley/resources/role.rb
366
+ - lib/ridley/resources/search.rb
366
367
  - lib/ridley/version.rb
367
368
  - ridley.gemspec
368
369
  - spec/acceptance/client_resource_spec.rb
@@ -372,6 +373,7 @@ files:
372
373
  - spec/acceptance/environment_resource_spec.rb
373
374
  - spec/acceptance/node_resource_spec.rb
374
375
  - spec/acceptance/role_resource_spec.rb
376
+ - spec/acceptance/search_resource_spec.rb
375
377
  - spec/fixtures/reset.pem
376
378
  - spec/spec_helper.rb
377
379
  - spec/support/each_matcher.rb
@@ -390,6 +392,7 @@ files:
390
392
  - spec/unit/ridley/resources/environment_spec.rb
391
393
  - spec/unit/ridley/resources/node_spec.rb
392
394
  - spec/unit/ridley/resources/role_spec.rb
395
+ - spec/unit/ridley/resources/search_spec.rb
393
396
  - spec/unit/ridley_spec.rb
394
397
  homepage: https://github.com/reset/ridley
395
398
  licenses: []
@@ -402,10 +405,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
402
405
  requirements:
403
406
  - - ! '>='
404
407
  - !ruby/object:Gem::Version
405
- version: '0'
406
- segments:
407
- - 0
408
- hash: 3057633279662549305
408
+ version: 1.9.1
409
409
  required_rubygems_version: !ruby/object:Gem::Requirement
410
410
  none: false
411
411
  requirements:
@@ -414,7 +414,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
414
414
  version: '0'
415
415
  segments:
416
416
  - 0
417
- hash: 3057633279662549305
417
+ hash: -2920625488512692680
418
418
  requirements: []
419
419
  rubyforge_project:
420
420
  rubygems_version: 1.8.23
@@ -429,6 +429,7 @@ test_files:
429
429
  - spec/acceptance/environment_resource_spec.rb
430
430
  - spec/acceptance/node_resource_spec.rb
431
431
  - spec/acceptance/role_resource_spec.rb
432
+ - spec/acceptance/search_resource_spec.rb
432
433
  - spec/fixtures/reset.pem
433
434
  - spec/spec_helper.rb
434
435
  - spec/support/each_matcher.rb
@@ -447,5 +448,6 @@ test_files:
447
448
  - spec/unit/ridley/resources/environment_spec.rb
448
449
  - spec/unit/ridley/resources/node_spec.rb
449
450
  - spec/unit/ridley/resources/role_spec.rb
451
+ - spec/unit/ridley/resources/search_spec.rb
450
452
  - spec/unit/ridley_spec.rb
451
453
  has_rdoc: