artrest 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (61) hide show
  1. checksums.yaml +15 -0
  2. data/.gitignore +20 -0
  3. data/Gemfile +4 -0
  4. data/LICENSE.txt +22 -0
  5. data/README.md +29 -0
  6. data/Rakefile +31 -0
  7. data/artrest.gemspec +30 -0
  8. data/bin/artrest +194 -0
  9. data/lib/artrest.rb +81 -0
  10. data/lib/artrest/build.rb +31 -0
  11. data/lib/artrest/buildnumber.rb +26 -0
  12. data/lib/artrest/builds.rb +46 -0
  13. data/lib/artrest/dir_entry.rb +115 -0
  14. data/lib/artrest/repositories.rb +61 -0
  15. data/lib/artrest/repository.rb +42 -0
  16. data/lib/artrest/resource.rb +366 -0
  17. data/lib/artrest/resources.rb +54 -0
  18. data/lib/artrest/system.rb +74 -0
  19. data/lib/artrest/system_general_configuration.rb +45 -0
  20. data/lib/artrest/version.rb +3 -0
  21. data/spec/artrest/build_spec.rb +62 -0
  22. data/spec/artrest/buildnumber_spec.rb +45 -0
  23. data/spec/artrest/builds_spec.rb +69 -0
  24. data/spec/artrest/folder_spec.rb +88 -0
  25. data/spec/artrest/repositories_spec.rb +47 -0
  26. data/spec/artrest/repository_spec.rb +63 -0
  27. data/spec/artrest/resource_spec.rb +385 -0
  28. data/spec/artrest/resources_spec.rb +66 -0
  29. data/spec/artrest/system_general_configuration_spec.rb +72 -0
  30. data/spec/artrest/system_spec.rb +50 -0
  31. data/spec/artrest_spec.rb +30 -0
  32. data/spec/fixtures/build/build_response_correct.txt +7 -0
  33. data/spec/fixtures/buildnumber/buildnumber_25_response.txt +7 -0
  34. data/spec/fixtures/builds/build_api_response_correct.txt +7 -0
  35. data/spec/fixtures/folder/pom_file_response_1.txt +7 -0
  36. data/spec/fixtures/folder/sub_folder_response.txt +7 -0
  37. data/spec/fixtures/folder/top_folder_response.txt +7 -0
  38. data/spec/fixtures/repositories/libs_snapshot_local_folder_response.txt +7 -0
  39. data/spec/fixtures/repositories/repositories_response.txt +7 -0
  40. data/spec/fixtures/repository/libs_snapshot_local_response.txt +7 -0
  41. data/spec/fixtures/resource/artifact_response.txt +7 -0
  42. data/spec/fixtures/resource/build_response.txt +7 -0
  43. data/spec/fixtures/resource/buildnumber_response.txt +7 -0
  44. data/spec/fixtures/resource/builds_response.txt +7 -0
  45. data/spec/fixtures/resource/folder_response.txt +7 -0
  46. data/spec/fixtures/resource/json_response.txt +7 -0
  47. data/spec/fixtures/resource/repositories_response.txt +7 -0
  48. data/spec/fixtures/resource/repository_not_found_response.txt +7 -0
  49. data/spec/fixtures/resource/repository_response.txt +7 -0
  50. data/spec/fixtures/resource/repository_unauthorized_response.txt +8 -0
  51. data/spec/fixtures/resource/string_response.txt +188 -0
  52. data/spec/fixtures/resource/sub_response.txt +188 -0
  53. data/spec/fixtures/resource/system_general_configuration_response.txt +654 -0
  54. data/spec/fixtures/resource/system_response.txt +188 -0
  55. data/spec/fixtures/resources/resources_response.txt +7 -0
  56. data/spec/fixtures/system/200_OK_ping_response.txt +7 -0
  57. data/spec/fixtures/system/general_configuration_response.txt +654 -0
  58. data/spec/fixtures/system/system_response.txt +188 -0
  59. data/spec/spec.opts +5 -0
  60. data/spec/spec_helper.rb +38 -0
  61. metadata +269 -0
@@ -0,0 +1,54 @@
1
+
2
+ module ArtRest
3
+
4
+ # An ArtRest::Resource that doubles as a collection of resources, each such
5
+ # resource being an ArtRest::Resource instance. This is an abstract base
6
+ # class that is *not* meant to be instantiated.
7
+ #
8
+ # === Example
9
+ #
10
+ # An ArtRest::Builds instance is a collection of ArtRest::Build instances.
11
+ #
12
+ class Resources < ArtRest::Resource
13
+ include Enumerable
14
+
15
+ class << self
16
+
17
+ # An ArtRest::Resources instance is represented by a parsed
18
+ # JSON hash. In order to be able to iterate over the contained
19
+ # ArtRest::Resource instances we need a means of converting
20
+ # that JSON hash into an array of ArtRest::Resource instances
21
+ # of an appropriate subtype. We choose a +block+ taking the JSON hash
22
+ # and the +options+ hash and returning an array of ArtRest::Resource
23
+ # instances.
24
+ #
25
+ # This method assigns that block to this class.
26
+ #
27
+ # * *Args* :
28
+ # - +block+ -> A +block+ taking a parsed JSON hash and an +options+
29
+ # hash and returning an array of appropriate ArtRest::Resource
30
+ # instances
31
+ #
32
+ def resources_creator=(block)
33
+ self.singleton_class.class_eval do
34
+ define_method(:resources_creator) do
35
+ block
36
+ end
37
+ end
38
+ end
39
+ end
40
+
41
+ public
42
+
43
+ # Iterate over all ArtRest::Resource instances contained in this
44
+ # instance. Yield each such instance to the supplied +block+.
45
+ #
46
+ # * *Args* :
47
+ # - +block+ -> The +block+ to yield each contained ArtRest::Resource
48
+ # instance to
49
+ #
50
+ def each &block # :yields: content, options
51
+ self.class.resources_creator.call(content, options).each &block
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,74 @@
1
+ module ArtRest
2
+
3
+ # Represents the {Artifactory}[http://www.jfrog.com/home/v_artifactory_opensource_overview]
4
+ # {System}[http://wiki.jfrog.org/confluence/display/RTF/Artifactory's+REST+API#Artifactory'sRESTAPI-SystemInfo]
5
+ # resource. Its content is a formatted multiline string listing
6
+ #
7
+ # * Java version used
8
+ # * Memory consumption
9
+ # * ...
10
+ #
11
+ # === Example
12
+ #
13
+ # sysInfo = ArtRest::System.new('http://localhost:8081/artifactory/api/system', { ... })
14
+ #
15
+ class System < ArtRest::Resource
16
+
17
+ class << self
18
+ public
19
+
20
+ # Shortcut method to retrieve *the* ArtRest::System resource.
21
+ #
22
+ # * *Args* :
23
+ # - +base_url+ -> Our Artifactory server's base URL
24
+ # - +options+ -> A Hash containing username and password
25
+ # * *Returns* :
26
+ # - *The* ArtRest::System resource
27
+ #
28
+ def get(base_url, options)
29
+ System.new("#{base_url}/api/system", options)
30
+ end
31
+
32
+ def matches_path(path, options) # :nodoc:
33
+ path =~ %r|^/api/system/?$|
34
+ end
35
+ end
36
+
37
+ self.mime_type = MIME::Types['text/plain']
38
+
39
+ # Get this system resource's {ping}[http://wiki.jfrog.org/confluence/display/RTF/Artifactory's+REST+API#Artifactory'sRESTAPI-SystemHealthPing]
40
+ # subresource, i.e. issue a ping request to test whether the
41
+ # Artifactory server this resource represents is still in a healthy
42
+ # state.
43
+ #
44
+ # Returns the plain RestClient::Response object returned by the server.
45
+ # In case the server is functioning properly this response will carry
46
+ # an HTTP 200 return code, and its body will contain the string
47
+ # "OK". Otherwise, the response returned will carry a return code from
48
+ # the 5xx family, and its body will be a text describing the problem.
49
+ #
50
+ # * *Returns* :
51
+ # - The plain RestClient::Response object as returned by the server
52
+ #
53
+ def ping
54
+ # Directly accessing the ping resource from this resource contravenes
55
+ # our design. For consistency reasons, we ought to explicitly model
56
+ # this ping resource as e.g. an instance of ArtRest::SystemPing.
57
+ # However, here we try to strike a balance between perfectionism
58
+ # and pragmatism and lean towards the latter.
59
+ RestClient::Resource.new("#{base_url}/api/system/ping", user, password).get
60
+ end
61
+
62
+ # Get this system resource's {general configuration}[http://wiki.jfrog.org/confluence/display/RTF/Artifactory's+REST+API#Artifactory'sRESTAPI-GeneralConfiguration]
63
+ # subresource, i.e. the resource representing Artifactory's
64
+ # +artifactory.config.xml+ file.
65
+ #
66
+ # * *Returns* :
67
+ # - Artifactory's general configuration, represented as an instance of
68
+ # ArtRest::System::GeneralConfiguration
69
+ #
70
+ def configuration
71
+ ArtRest::System::GeneralConfiguration.new("#{base_url}/api/system/configuration", options, block)
72
+ end
73
+ end
74
+ end
@@ -0,0 +1,45 @@
1
+ module ArtRest
2
+
3
+ class System
4
+
5
+ # Represents Artifactory's {General Configuration}[http://wiki.jfrog.org/confluence/display/RTF/Artifactory's+REST+API#Artifactory'sRESTAPI-GeneralConfiguration]
6
+ # resource. Essentially, this is Artifactory's +artifactory.config.xml+
7
+ # file.
8
+ #
9
+ # This class serves to
10
+ #
11
+ # * retrieve Artifactory's general configuration as an in-memory
12
+ # XML data structure,
13
+ # * download Artifactory's general configuration to a folder/file, and
14
+ # * update (post) Artifactory's general configuration.
15
+ #
16
+ class GeneralConfiguration < ArtRest::Resource
17
+
18
+ class << self
19
+ public
20
+
21
+ # Shortcut method to retrieve *the*
22
+ # ArtRest::System::GeneralConfiguration resource.
23
+ #
24
+ # * *Args* :
25
+ # - +base_url+ -> Our Artifactory server's base URL
26
+ # - +options+ -> A Hash containing username and password
27
+ # * *Returns* :
28
+ # - *The* ArtRest::System::GeneralConfiguration resource
29
+ #
30
+ def get(base_url, options)
31
+ System::GeneralConfiguration.new("#{base_url}/api/system/configuration", options)
32
+ end
33
+
34
+ def matches_path(path, options) # :nodoc:
35
+ path =~ %r|^/api/system/configuration/?$|
36
+ end
37
+ end
38
+
39
+ self.mime_type = MIME::Types['application/xml']
40
+
41
+ # Make update! available on this class as it supports updates
42
+ public :update!, :update_with!
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,3 @@
1
+ module ArtRest
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,62 @@
1
+ require File.expand_path("../../spec_helper", __FILE__)
2
+
3
+ describe ArtRest::Build do
4
+
5
+ before(:each) do
6
+ @build_name = 'vnet.sms.common.shell'
7
+ @build_path = "api/build/#{@build_name}"
8
+ @build_url = "#{ARTIFACTORY_URL}/#{@build_path}"
9
+ @artbuild = ArtRest::Build.new @build_url, OPTIONS
10
+ register_stub_request('./build/build_response_correct.txt', "#{@build_path}")
11
+ end
12
+
13
+ describe "#each" do
14
+ it "should iterate over all build numbers" do
15
+ expected_number_of_build_numbers = 63
16
+ actual_number_of_build_numbers = 0
17
+ @artbuild.each do |build_number|
18
+ actual_number_of_build_numbers += 1
19
+ end
20
+ actual_number_of_build_numbers.should equal expected_number_of_build_numbers
21
+ end
22
+
23
+ it "should return each build number as an instance of ArtRest::BuildNumber" do
24
+ @artbuild.each do |build_number|
25
+ build_number.should_not be_nil
26
+ build_number.should be_an_instance_of ArtRest::Buildnumber
27
+ end
28
+ end
29
+ end
30
+
31
+ describe "[all resource attributes]" do
32
+
33
+ before(:each) do
34
+ @resource_attributes = [:uri, :buildsNumbers]
35
+ end
36
+
37
+ context "when no block given" do
38
+ it "should return a non-nil value" do
39
+ @resource_attributes.each do |attr|
40
+ value = @artbuild.send(attr)
41
+ value.should_not be_nil
42
+ end
43
+ end
44
+ end
45
+
46
+ context "when block given" do
47
+ it "should yield a non-nil value to that block" do
48
+ @resource_attributes.each do |attr|
49
+ @artbuild.send(attr) do |value|
50
+ value.should_not be_nil
51
+ end
52
+ end
53
+ end
54
+ end
55
+ end
56
+
57
+ describe "#url" do
58
+ it "should expose the url property" do
59
+ @artbuild.url.should eq @build_url
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,45 @@
1
+ require File.expand_path("../../spec_helper", __FILE__)
2
+
3
+ describe ArtRest::Buildnumber do
4
+
5
+ before(:each) do
6
+ @build_name = 'vnet.sms.common.shell'
7
+ @build_no = 25
8
+ @buildnumber_path = "api/build/#{@build_name}/#{@build_no}"
9
+ @buildnumber_url = "#{ARTIFACTORY_URL}/#{@buildnumber_path}"
10
+ @artbuildnumber = ArtRest::Buildnumber.new(@buildnumber_url, OPTIONS)
11
+ register_stub_request('./buildnumber/buildnumber_25_response.txt', "#{@buildnumber_path}")
12
+ end
13
+
14
+ describe "#url" do
15
+ it "should expose the url property" do
16
+ @artbuildnumber.url.should eq @buildnumber_url
17
+ end
18
+ end
19
+
20
+ describe "[all resource attributes]" do
21
+
22
+ before(:each) do
23
+ @resource_attributes = [:uri, :buildInfo]
24
+ end
25
+
26
+ context "when no block given" do
27
+ it "should return a non-nil value" do
28
+ @resource_attributes.each do |attr|
29
+ value = @artbuildnumber.send(attr)
30
+ value.should_not be_nil
31
+ end
32
+ end
33
+ end
34
+
35
+ context "when block given" do
36
+ it "should yield a non-nil value to that block" do
37
+ @resource_attributes.each do |attr|
38
+ @artbuildnumber.send(attr) do |value|
39
+ value.should_not be_nil
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,69 @@
1
+ require File.expand_path("../../spec_helper", __FILE__)
2
+
3
+ describe ArtRest::Builds do
4
+
5
+ before(:each) do
6
+ @builds_path = "api/build"
7
+ @builds_url = "#{ARTIFACTORY_URL}/#{@builds_path}"
8
+ @artbuilds = ArtRest::Builds.new @builds_url, OPTIONS
9
+ register_stub_request('./builds/build_api_response_correct.txt', @builds_path)
10
+ end
11
+
12
+ describe "::get" do
13
+ it "should expose a shortcut method to retrieve all builds" do
14
+ all_builds = ArtRest::Builds.get ARTIFACTORY_URL, OPTIONS
15
+ all_builds.should_not be_nil
16
+ all_builds.should be_an_instance_of ArtRest::Builds
17
+ end
18
+ end
19
+
20
+ describe "#each" do
21
+ it "should iterate over all builds" do
22
+ expected_number_of_builds = 64
23
+ actual_number_of_builds = 0
24
+ @artbuilds.each do |build|
25
+ actual_number_of_builds += 1
26
+ end
27
+ actual_number_of_builds.should equal expected_number_of_builds
28
+ end
29
+
30
+ it "should return all builds as an instance of ArtRest::Build" do
31
+ @artbuilds.each do |build|
32
+ build.should be_an_instance_of ArtRest::Build
33
+ end
34
+ end
35
+ end
36
+
37
+ describe "[all resource attributes]" do
38
+
39
+ before(:each) do
40
+ @resource_attributes = [:uri, :builds]
41
+ end
42
+
43
+ context "when no block given" do
44
+ it "should return a non-nil value" do
45
+ @resource_attributes.each do |attr|
46
+ value = @artbuilds.send(attr)
47
+ value.should_not be_nil
48
+ end
49
+ end
50
+ end
51
+
52
+ context "when block given" do
53
+ it "should yield a non-nil value to that block" do
54
+ @resource_attributes.each do |attr|
55
+ @artbuilds.send(attr) do |value|
56
+ value.should_not be_nil
57
+ end
58
+ end
59
+ end
60
+ end
61
+ end
62
+
63
+ describe "#url" do
64
+ it "should expose the url property" do
65
+ @artbuilds.url.should eq @builds_url
66
+ end
67
+ end
68
+
69
+ end
@@ -0,0 +1,88 @@
1
+ require File.expand_path("../../spec_helper", __FILE__)
2
+
3
+ describe ArtRest::Folder do
4
+
5
+ before(:each) do
6
+ @repo_name = 'ext-snapshot-local'
7
+ @folder_path = '/vnet/sms/infrastructure/rpm-elasticsearch'
8
+ @folder_url = "#{ARTIFACTORY_URL}/api/storage/#{@repo_name}#{@folder_path}"
9
+ @artfolder = ArtRest::Folder.new @folder_url, OPTIONS
10
+ register_stub_request('./folder/top_folder_response.txt', "api/storage/#{@repo_name}#{@folder_path}")
11
+ register_stub_request('./folder/sub_folder_response.txt', "api/storage/#{@repo_name}#{@folder_path}/1.0.0-SNAPSHOT")
12
+ register_stub_request('./folder/pom_file_response_1.txt', "api/storage/#{@repo_name}#{@folder_path}/1.0.0-SNAPSHOT/rpm-elasticsearch-1.0.0-20120411.155423-1.pom")
13
+ end
14
+
15
+ describe "#each" do
16
+ it "should iterate over all child folders" do
17
+ expected_number_of_folders = 1
18
+ actual_number_of_folders = 0
19
+ @artfolder.each do |folder|
20
+ actual_number_of_folders += 1
21
+ end
22
+ actual_number_of_folders.should equal expected_number_of_folders
23
+ end
24
+
25
+ it "should return each child folder as an ArtRest::Folder instance" do
26
+ @artfolder.each do |folder|
27
+ folder.should be_an_instance_of ArtRest::Folder
28
+ end
29
+ end
30
+ end
31
+
32
+ describe "[all resource attributes]" do
33
+
34
+ before(:each) do
35
+ @resource_attributes = [:path,
36
+ :lastUpdated,
37
+ :repo,
38
+ :uri,
39
+ :modifiedBy,
40
+ :created,
41
+ :createdBy,
42
+ :lastModified,
43
+ :metadataUri]
44
+ end
45
+
46
+ context "when no block given" do
47
+ it "should return a non-nil value" do
48
+ @resource_attributes.each do |attr|
49
+ value = @artfolder.send(attr)
50
+ value.should_not be_nil
51
+ end
52
+ end
53
+ end
54
+
55
+ context "when block given" do
56
+ it "should yield a non-nil value to that block" do
57
+ @resource_attributes.each do |attr|
58
+ @artfolder.send(attr) do |value|
59
+ value.should_not be_nil
60
+ end
61
+ end
62
+ end
63
+ end
64
+ end
65
+
66
+ describe "#[]" do
67
+ it "should return a subfolder as an instance of ArtRest::Folder" do
68
+ folder = @artfolder['1.0.0-SNAPSHOT']
69
+ folder.should_not be_nil
70
+ folder.should be_an_instance_of ArtRest::Folder
71
+ end
72
+
73
+ it "should return a contained artifact as an instance of ArtRest::Artifact" do
74
+ artifact = @artfolder['1.0.0-SNAPSHOT/rpm-elasticsearch-1.0.0-20120411.155423-1.pom']
75
+ artifact.should_not be_nil
76
+ artifact.should be_an_instance_of ArtRest::Artifact
77
+ end
78
+ end
79
+
80
+ describe "#traverse" do
81
+ it "should traverse the whole subtree rooted at itself" do
82
+ @artfolder.traverse do |child|
83
+ child.should be_a_kind_of ArtRest::DirEntry
84
+ end
85
+ end
86
+ end
87
+
88
+ end