worochi 0.0.0 → 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (53) hide show
  1. checksums.yaml +4 -4
  2. data/LICENSE +20 -0
  3. data/lib/worochi/agent/dropbox.rb +26 -4
  4. data/lib/worochi/agent/github.rb +14 -9
  5. data/lib/worochi/agent/sample.rb +8 -3
  6. data/lib/worochi/agent.rb +64 -23
  7. data/lib/worochi/config.rb +34 -2
  8. data/lib/worochi/error.rb +1 -0
  9. data/lib/worochi/helper/github.rb +1 -0
  10. data/lib/worochi/helper.rb +10 -8
  11. data/lib/worochi/item.rb +18 -9
  12. data/lib/worochi/log.rb +11 -0
  13. data/lib/worochi/version.rb +4 -0
  14. data/lib/worochi.rb +23 -16
  15. data/spec/cassettes/Worochi_Agent_Dropbox/_list/works_with_absolute_paths.yml +60 -0
  16. data/spec/cassettes/Worochi_Agent_Dropbox/_push_item/pushes_a_single_item.yml +303 -0
  17. data/spec/cassettes/Worochi_Agent_Dropbox/_push_item_chunked/pushes_a_single_item_in_chunks.yml +349 -0
  18. data/spec/cassettes/Worochi_Agent_Dropbox/it_should_behave_like_a_service_agent/_files/accepts_a_different_path.yml +60 -0
  19. data/spec/cassettes/Worochi_Agent_Dropbox/it_should_behave_like_a_service_agent/_files/accepts_a_different_relative_path.yml +60 -0
  20. data/spec/cassettes/Worochi_Agent_Dropbox/it_should_behave_like_a_service_agent/_files/contains_file1.yml +79 -0
  21. data/spec/cassettes/Worochi_Agent_Dropbox/it_should_behave_like_a_service_agent/_folders/accepts_a_different_path.yml +60 -0
  22. data/spec/cassettes/Worochi_Agent_Dropbox/it_should_behave_like_a_service_agent/_folders/accepts_a_different_relative_path.yml +60 -0
  23. data/spec/cassettes/Worochi_Agent_Dropbox/it_should_behave_like_a_service_agent/_folders/contains_folder1.yml +79 -0
  24. data/spec/cassettes/Worochi_Agent_Github/_list/works_with_absolute_paths.yml +133 -0
  25. data/spec/cassettes/Worochi_Agent_Github/_push_all/pushes_a_list_of_items_to_create_a_new_commit.yml +989 -0
  26. data/spec/cassettes/Worochi_Agent_Github/_push_all/pushes_the_file_to_the_right_place.yml +687 -0
  27. data/spec/cassettes/Worochi_Agent_Github/_source_branch/retrieves_the_master_branch_correctly.yml +68 -0
  28. data/spec/cassettes/Worochi_Agent_Github/it_should_behave_like_a_service_agent/_files/accepts_a_different_path.yml +133 -0
  29. data/spec/cassettes/Worochi_Agent_Github/it_should_behave_like_a_service_agent/_files/accepts_a_different_relative_path.yml +133 -0
  30. data/spec/cassettes/Worochi_Agent_Github/it_should_behave_like_a_service_agent/_files/contains_file1.yml +133 -0
  31. data/spec/cassettes/Worochi_Agent_Github/it_should_behave_like_a_service_agent/_folders/accepts_a_different_path.yml +133 -0
  32. data/spec/cassettes/Worochi_Agent_Github/it_should_behave_like_a_service_agent/_folders/accepts_a_different_relative_path.yml +133 -0
  33. data/spec/cassettes/Worochi_Agent_Github/it_should_behave_like_a_service_agent/_folders/contains_folder1.yml +133 -0
  34. data/spec/cassettes/Worochi_Item/_open/opens_a_single_file/with_Hash_parameter.yml +107 -0
  35. data/spec/cassettes/Worochi_Item/_open/opens_a_single_file/with_String_parameter.yml +107 -0
  36. data/spec/cassettes/Worochi_Item/_open/opens_multiple_files/with_Hash_parameters.yml +211 -0
  37. data/spec/cassettes/Worochi_Item/_open/opens_multiple_files/with_String_parameters.yml +107 -0
  38. data/spec/cassettes/Worochi_Item/_open_single/accepts_a_String.yml +107 -0
  39. data/spec/cassettes/Worochi_Item/_open_single/works_with_AWS_S3_paths.yml +46 -0
  40. data/spec/helper.rb +46 -0
  41. data/spec/support/aws_uri_matcher.rb +12 -0
  42. data/spec/support/shared_exampes_for_agents.rb +36 -0
  43. data/spec/support/test_file +1 -0
  44. data/spec/support/test_files.rb +34 -0
  45. data/spec/worochi/agent/dropbox_spec.rb +48 -0
  46. data/spec/worochi/agent/github_spec.rb +77 -0
  47. data/spec/worochi/agent_spec.rb +2 -0
  48. data/spec/worochi/helper/github_spec.rb +57 -0
  49. data/spec/worochi/helper_spec.rb +23 -0
  50. data/spec/worochi/item_spec.rb +85 -0
  51. data/spec/worochi_spec.rb +109 -0
  52. data/worochi.gemspec +27 -4
  53. metadata +193 -4
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2ca726002689f7fe66fe07d7ff2af93a5fc6b2e4
4
- data.tar.gz: 27f7cfdcd912eeb33d9103969c59b192a79d47ab
3
+ metadata.gz: 69806f4b59d55c009448f571be6a511b5aa374d3
4
+ data.tar.gz: 107d2fd5ce53ca7fca92803a493c136f37b2e51e
5
5
  SHA512:
6
- metadata.gz: 57fbb09d74618b9544fe5e2c8651637b1393cd97a76dec396474286393c6552ed8bfc6a568a57f60986f38665f1498352d87e0cb893e7a341d2e1034b3f39bf0
7
- data.tar.gz: c0519b4338dbeabc4365776293935c6a6e6156c4ded75ed86756ab4d4d8c9c17807a27c013b640c33a0a4de359d44dc9c8d579871fd52e181b99518d54514639
6
+ metadata.gz: 7eeacb62b1a23e3e720b9ae6a274f9d6eab6737155c2ffb952ab12a99852a7192251152e360add6158cf8d3ef221ea7e2607aaf4a02aa7ba376f9c4070b897d1
7
+ data.tar.gz: fac7680732d95214bc6176faf2760cce1a04e76c85e51d0f1b9fab55c35a4eef09d7fe51881564a27899b5af856aba8fd16ce67855ac81bf4df25cca30c3f0e9
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2013 Pixelapse
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
6
+ this software and associated documentation files (the "Software"), to deal in
7
+ the Software without restriction, including without limitation the rights to
8
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9
+ the Software, and to permit persons to whom the Software is furnished to do so,
10
+ subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17
+ FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18
+ COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19
+ IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20
+ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -7,6 +7,7 @@ class Worochi
7
7
  # @return [Hash] default options for Dropbox
8
8
  def default_options
9
9
  {
10
+ service: :dropbox,
10
11
  chunk_size: 2*1024*1024,
11
12
  overwrite: true,
12
13
  dir: '/'
@@ -20,11 +21,12 @@ class Worochi
20
21
  # @return [DropboxClient]
21
22
  def init_client
22
23
  @client = DropboxClient.new(options[:token])
24
+ @client
23
25
  end
24
26
 
25
27
  # Push a single {Item} to Dropbox.
26
28
  #
27
- # @param [Item]
29
+ # @param item [Item]
28
30
  # @return [nil]
29
31
  def push_item(item)
30
32
  Worochi::Log.debug "Uploading #{item.path} (#{item.size} bytes) to Dropbox..."
@@ -40,9 +42,15 @@ class Worochi
40
42
  # Returns a list of files and subdirectories at the remote path specified
41
43
  # by `options[:dir]`.
42
44
  #
45
+ # @param path [String] path to list instead of the current directory
43
46
  # @return [Array<Hash>] list of files and subdirectories
44
- def list
45
- remote_path = options[:dir]
47
+ def list(path=nil)
48
+ if path
49
+ remote_path = path[0] == '/' ? path : File.join(options[:dir], path)
50
+ else
51
+ remote_path = options[:dir]
52
+ end
53
+
46
54
  begin
47
55
  response = @client.metadata(remote_path)
48
56
  rescue DropboxError
@@ -62,8 +70,9 @@ class Worochi
62
70
  # Refer to {https://www.dropbox.com/developers/core/docs#chunked-upload
63
71
  # API documentation}.
64
72
  #
65
- # @param [Item]
73
+ # @param item [Item]
66
74
  # @return [nil]
75
+ # @see https://www.dropbox.com/developers/core/docs#chunked-upload
67
76
  def push_item_chunked(item)
68
77
  Worochi::Log.debug "Using chunk uploader..."
69
78
  uploader = @client.get_chunked_uploader(item.content, item.size)
@@ -78,5 +87,18 @@ class Worochi
78
87
  uploader.finish(full_path(item), options[:overwrite])
79
88
  nil
80
89
  end
90
+
91
+ # Deletes the file at `path` from Dropbox.
92
+ #
93
+ # @param path [String] path relative to current directory
94
+ # @return [Boolean] `true` if a file was actually deleted
95
+ def delete(path)
96
+ begin
97
+ @client.file_delete(File.join(options[:dir], path))
98
+ rescue DropboxError
99
+ false
100
+ end
101
+ true
102
+ end
81
103
  end
82
104
  end
@@ -10,7 +10,8 @@ class Worochi
10
10
  # @return [Hash] default options for GitHub
11
11
  def default_options
12
12
  {
13
- source: 'master',
13
+ service: :github,
14
+ source: 'worochi',
14
15
  target: 'worochi',
15
16
  repo: 'darkmirage/test',
16
17
  block_size: Worochi::Helper::Github::BLOCK_SIZE,
@@ -26,12 +27,13 @@ class Worochi
26
27
  # @return [Octokit::Client]
27
28
  def init_client
28
29
  @client = Octokit::Client.new(login: 'me', oauth_token: options[:token])
30
+ @client
29
31
  end
30
32
 
31
33
  # Pushes a list of {Item} to GitHub.
32
34
  #
33
- # @param [Array<Item>]
34
- # @return [nil]
35
+ # @param items [Array<Item>]
36
+ # @return [String] Commit SHA1 hash
35
37
  # @see Agent#push_items
36
38
  def push_all(items)
37
39
  source_sha = source_branch
@@ -39,13 +41,13 @@ class Worochi
39
41
  commit = @client.create_commit(repo, options[:commit_msg], source_sha,
40
42
  target_branch)
41
43
  @client.update_ref(repo, "heads/#{options[:target]}", commit.sha)
42
- nil
44
+ commit.sha
43
45
  end
44
46
 
45
47
  # Pushes a single {Item} to GitHub. This means making a new commit for each
46
48
  # file. Not recommended and should just use {#push_all} instead.
47
49
  #
48
- # @param [Item]
50
+ # @param item [Item]
49
51
  # @return [nil]
50
52
  def push_item(item)
51
53
  Worochi::Log.warn 'push_item should not be used for GitHub'
@@ -55,11 +57,13 @@ class Worochi
55
57
  # Returns a list of files and subdirectories at the remote path specified
56
58
  # by `options[:dir]`.
57
59
  #
60
+ # @param path [String] path to list instead of the current directory
61
+ # @param sha [String] list a different branch than the `:source`
58
62
  # @return [Array<Hash>] list of files and subdirectories
59
- def list
60
- remote_path = options[:dir].sub(/^\//, '').sub(/\/$/, '')
63
+ def list(path=nil, sha=nil)
64
+ remote_path = (path || options[:dir]).sub(/^\//, '').sub(/\/$/, '')
61
65
 
62
- result = @client.tree(repo, source_branch, recursive: true).tree
66
+ result = @client.tree(repo, sha || source_branch, recursive: true).tree
63
67
  result.sort! do |x, y|
64
68
  x.path.split('/').size <=> y.path.split('/').size
65
69
  end
@@ -124,7 +128,8 @@ class Worochi
124
128
  if item.size > options[:block_size]
125
129
  sha = stream_blob(item)
126
130
  else
127
- sha = @client.create_blob(repo, Base64.strict_encode64(item.read), 'base64')
131
+ sha = @client.create_blob(repo, Base64.strict_encode64(item.read),
132
+ 'base64')
128
133
  end
129
134
  Worochi::Log.debug "Uploaded [#{sha}]"
130
135
  sha
@@ -3,11 +3,14 @@ class Worochi
3
3
  # This is a sample of methods that should be implemented by a service agent.
4
4
  class Agent::Sample < Agent
5
5
 
6
+ # This is the minimum set of default options that should be specified.
7
+ #
6
8
  # @return [Hash] default options for sample API
7
9
  # @see Agent#set_options
8
10
  def default_options
9
11
  {
10
- dir: '/'
12
+ service: :sample, # service name
13
+ dir: '/' # remote directory to act on
11
14
  }
12
15
  end
13
16
 
@@ -45,11 +48,13 @@ class Worochi
45
48
  # least the keys `:name`, `:path`, and `:type`, but can also contain other
46
49
  # service-specific meta information.
47
50
  #
51
+ # @param path [String] path to list instead of the current directory
48
52
  # @return [Array<Hash>] list of files and subdirectories
49
53
  # @see Agent#folders
50
54
  # @see Agent#files
51
- def list
52
- result = @client.get_file_list
55
+ def list(path=nil)
56
+ remote_path = path || options[:dir]
57
+ result = @client.get_file_list(remote_path)
53
58
  result.map do |elem|
54
59
  {
55
60
  name: elem.name,
data/lib/worochi/agent.rb CHANGED
@@ -19,14 +19,13 @@ class Worochi
19
19
  # format the file list. An optional `opts` hash can be used to update the
20
20
  # agent options before pushing.
21
21
  #
22
- # @example
23
- # agent = Worochi.create(:github, 'sfsFj41na89cx')
24
- # agent.push({ source: 'http://a.com/file.jpg', path: 'folder/file.jpg' })
25
- #
26
22
  # @param origin [Array<Hash>, Array<String>, Hash, String]
27
23
  # @param opts [Hash] update agent options before pushing
28
24
  # @return [nil]
29
25
  # @see Item.open
26
+ # @example
27
+ # agent = Worochi.create(:github, 'sfsFj41na89cx')
28
+ # agent.push({ source: 'http://a.com/file.jpg', path: 'folder/file.jpg' })
30
29
  def push(origin, opts=nil)
31
30
  set_options(opts) unless opts.nil?
32
31
  items = Item.open(origin)
@@ -59,23 +58,38 @@ class Worochi
59
58
  nil
60
59
  end
61
60
 
62
- # Returns a list of subdirectories at the remote path specified by
63
- # `options[:dir]`. Relies on the service-specific implementation of
64
- # `#list`.
65
- #
66
- # @return [Array<Hash>] list of subdirectories
67
- def folders(details=false)
68
- result = list.reject { |elem| elem[:type] != 'folder' }
69
- result.map { |elem| elem[:name] } unless details
70
- end
71
-
72
61
  # Returns a list of files at the remote path specified by `options[:dir]`.
73
62
  # Relies on the service-specific implementation of `#list`.
63
+ #
64
+ # @overload files(details)
65
+ # @param details [Boolean] display more information
66
+ # @overload files(path, details=false)
67
+ # @param path [String] remote path to list instead of current directory
68
+ # @param details [Boolean] display more information
69
+ # @return [Array<String>, Array<Hash>] list of files
70
+ # @example
71
+ # agent = Worochi.create(:dropbox, 'sfsFj41na89cx', dir: '/abc')
72
+ # agent.files # => ["k.jpg", "t.txt"]
73
+ # agent.files(true)
74
+ # # => [
75
+ # # { name: "k.jpg", type: "file", path: "/abc/k.jpg"},
76
+ # # { name: "t.txt", type: "file", path: "/abc/t.txt"}
77
+ # # ]
78
+ def files(*args)
79
+ list_helper(true, args)
80
+ end
81
+
82
+ # Returns a list of subdirectories at the remote path specified by
83
+ # `options[:dir]`. Relies on the service-specific implementation of
84
+ # `#list`. Refer to {#files} for overloaded prototypes.
74
85
  #
75
- # @return [Array<Hash>] list of files
76
- def files(details=false)
77
- result = list.reject { |elem| elem[:type] != 'file' }
78
- result.map { |elem| elem[:name] } unless details
86
+ # @return [Array<String>, Array<Hash>] list of subdirectories
87
+ # @see #files
88
+ # @example
89
+ # agent = Worochi.create(:dropbox, 'sfsFj41na89cx', dir: '/abc')
90
+ # agent.folders # => ["folder1", "folder2"]
91
+ def folders(*args)
92
+ list_helper(false, args)
79
93
  end
80
94
 
81
95
  # Updates {.options} using `opts`.
@@ -97,26 +111,51 @@ class Worochi
97
111
  options
98
112
  end
99
113
 
114
+ # Returns the display name for the agent's service.
115
+ #
116
+ # @return [String] display name
117
+ def name
118
+ Worochi::Config.humanize_service(type)
119
+ end
120
+
100
121
  private
122
+ # Parses the arguments for {#files} and {#folders}.
123
+ #
124
+ # @param file_only [Boolean] filters files or folders
125
+ # @param args [Array] argument list
126
+ # @return [Array<String>, Array<Hash>] list of files or folders
127
+ def list_helper(file_only, args)
128
+ details = true if args.first == true
129
+ if args.first.kind_of?(String)
130
+ path = args.first
131
+ details = true if args[1] == true
132
+ end
133
+ excluded = file_only ? 'folder' : 'file'
134
+ result = list(path).reject { |elem| elem[:type] == excluded }
135
+ result.map! { |elem| elem[:name] } unless details
136
+ result
137
+ end
138
+
101
139
  # @return [String] full path combining remote directory and item path
102
140
  def full_path(item)
103
141
  File.join(options[:dir], item.path)
104
142
  end
105
143
 
106
- # @return [Hash] default options for agents that do not override this
144
+ # Agents should override this.
145
+ #
146
+ # @return [nil]
107
147
  def default_options
108
- { dir: '/' }
148
+ raise Error, 'Default options not specified.'
109
149
  end
110
150
 
111
151
  class << self
112
152
  public
113
153
  # Creates a new service-specific {Agent} based on `:service`.
114
154
  #
115
- # @example
116
- # Worochi::Agent.new({ service: :github, token:'6st46setsybhd64' })
117
- #
118
155
  # @param opts [Hash] service options; must contain `:service` key.
119
156
  # @return [Agent]
157
+ # @example
158
+ # Worochi::Agent.new({ service: :github, token:'6st46setsybhd64' })
120
159
  def new(opts={})
121
160
  service = opts[:service]
122
161
  if self.name == 'Worochi::Agent'
@@ -131,6 +170,8 @@ class Worochi
131
170
  # Returns the class name for the {Agent} given a service name
132
171
  #
133
172
  # @return [String]
173
+ # @example
174
+ # class_name(:google_drive) # => "GoogleDrive"
134
175
  def class_name(service)
135
176
  service.to_s.split('_').map{|e| e.capitalize}.join
136
177
  end
@@ -1,22 +1,54 @@
1
1
  class Worochi
2
2
  # Configurations for Worochi.
3
3
  module Config
4
- @services = [:github, :dropbox]
4
+ @services = {
5
+ github: [1, 'GitHub'],
6
+ dropbox: [2, 'Dropbox']
7
+ }
5
8
  @s3_bucket = 'data-pixelapse'
6
9
  @s3_prefix = 's3'
7
10
 
8
11
  class << self
9
12
  # Array of service names.
13
+ #
10
14
  # @return [Array<Symbol>]
11
- attr_reader :services
15
+ def services
16
+ @services.keys
17
+ end
18
+
19
+ # Returns display name for the service.
20
+ #
21
+ # @param service [Symbol]
22
+ # @return [String] display name
23
+ def humanize_service(service)
24
+ @services[service][1]
25
+ end
26
+
27
+ # Returns the service ID for the service, which can be used as a
28
+ # primary key for databases.
29
+ #
30
+ # @param service [Symbol]
31
+ # @return [Integer] service ID
32
+ def service_id(service)
33
+ @services[service][0]
34
+ end
12
35
 
13
36
  # Name of S3 bucket.
37
+ #
14
38
  # @return [String]
15
39
  attr_reader :s3_bucket
16
40
 
17
41
  # Prefix for S3 resource paths.
42
+ #
18
43
  # @return [String]
19
44
  attr_reader :s3_prefix
45
+
46
+ # Disable debug and error messages if `true`.
47
+ #
48
+ # @return [Boolean]
49
+ attr_accessor :silent
50
+
51
+ alias_method :silent?, :silent
20
52
  end
21
53
  end
22
54
  end
data/lib/worochi/error.rb CHANGED
@@ -1,2 +1,3 @@
1
+ # Exception class for {Worochi} namespace
1
2
  class Worochi::Error < StandardError
2
3
  end
@@ -4,6 +4,7 @@ class Worochi
4
4
  # Helper classes for GitHub JSON streaming.
5
5
  module Helper::Github
6
6
 
7
+ # Size to read in. Must be a multiple of 3 for Base64 streaming.
7
8
  BLOCK_SIZE = 12288
8
9
 
9
10
  # This is a wrapper that produces a JSON stream that works with
@@ -1,16 +1,9 @@
1
1
  require 'aws-sdk' unless Worochi::Config.s3_bucket.nil?
2
2
 
3
3
  class Worochi
4
+ # Contains any global helper methods not specific to any individual service.
4
5
  module Helper
5
6
  class << self
6
- # A regex generated from {Config.s3_prefix} for determining if a given
7
- # String is an S3 path.
8
- #
9
- # @return [Regexp]
10
- def s3_prefix_re
11
- /^#{Config.s3_prefix}\:/
12
- end
13
-
14
7
  # Given an S3 path, return the full URL for the corresponding object
15
8
  # determined using the AWS SDK.
16
9
  #
@@ -29,6 +22,15 @@ class Worochi
29
22
  def is_s3_path?(path)
30
23
  !s3_prefix_re.match(path).nil?
31
24
  end
25
+
26
+ private
27
+ # A regex generated from {Config.s3_prefix} for determining if a given
28
+ # String is an S3 path.
29
+ #
30
+ # @return [Regexp]
31
+ def s3_prefix_re
32
+ /^#{Config.s3_prefix}\:/
33
+ end
32
34
  end
33
35
  end
34
36
  end
data/lib/worochi/item.rb CHANGED
@@ -15,10 +15,12 @@ class Worochi
15
15
  # An IO object containing the content of the file being pushed.
16
16
  # @return [IO]
17
17
  attr_accessor :content
18
- def initialize(opts={})
19
- @path = opts[:path]
20
- raise Error, 'Missing Item content' if !opts[:content]
21
- @content = opts[:content]
18
+
19
+ # @param path [String] relative destination path including file name
20
+ # @param content [IO] file content
21
+ def initialize(path, content)
22
+ @path = path
23
+ @content = content
22
24
  @content.rewind
23
25
  end
24
26
 
@@ -34,7 +36,15 @@ class Worochi
34
36
  #
35
37
  # @return [String]
36
38
  def read(*args)
37
- content.read(args)
39
+ content.read(*args)
40
+ end
41
+
42
+ # Rewinds the content. This is just a wrapper for the `#rewind` method on
43
+ # {#content}.
44
+ #
45
+ # @return [0]
46
+ def rewind
47
+ content.rewind
38
48
  end
39
49
 
40
50
  class << self
@@ -79,12 +89,11 @@ class Worochi
79
89
  source = entry
80
90
  end
81
91
 
82
- Item.new({
83
- path: path || File.basename(source),
84
- content: retrieve(source)
85
- })
92
+ Item.new(path || File.basename(source), retrieve(source))
86
93
  end
87
94
 
95
+ private
96
+
88
97
  # Retrieves the file content from `source`.
89
98
  #
90
99
  # @param source [String] local or remote location of the file content
data/lib/worochi/log.rb CHANGED
@@ -1,7 +1,9 @@
1
1
  require 'logger'
2
2
 
3
3
  class Worochi
4
+ # Implements colored log messages.
4
5
  class Log
6
+ # Maps severity level to color code for logging.
5
7
  SEVERITY_COLOR = {
6
8
  'DEBUG' => 37,
7
9
  'INFO' => 32,
@@ -10,6 +12,7 @@ class Worochi
10
12
  'FATAL' => 31
11
13
  }
12
14
  class << self
15
+ # Initializes the logging system.
13
16
  def init_log
14
17
  @logger = Logger.new(STDOUT)
15
18
  @logger.formatter = proc do |severity, datetime, progname, msg|
@@ -17,22 +20,30 @@ class Worochi
17
20
  end
18
21
  end
19
22
 
23
+ # Prints DEBUG messages
20
24
  def debug(message)
25
+ return if Worochi::Config.silent?
21
26
  init_log if @logger.nil?
22
27
  @logger.debug message
23
28
  end
24
29
 
30
+ # Prints WARN messages
25
31
  def warn(message)
32
+ return if Worochi::Config.silent?
26
33
  init_log if @logger.nil?
27
34
  @logger.warn message
28
35
  end
29
36
 
37
+ # Prints INFO messages
30
38
  def info(message)
39
+ return if Worochi::Config.silent?
31
40
  init_log if @logger.nil?
32
41
  @logger.info message
33
42
  end
34
43
 
44
+ # Prints ERROR messages
35
45
  def error(message)
46
+ return if Worochi::Config.silent?
36
47
  init_log if @logger.nil?
37
48
  @logger.error message
38
49
  end
@@ -0,0 +1,4 @@
1
+ class Worochi
2
+ # Current version of the gem
3
+ VERSION = '0.0.1'
4
+ end
data/lib/worochi.rb CHANGED
@@ -5,6 +5,8 @@ require 'worochi/helper'
5
5
  require 'worochi/item'
6
6
  require 'worochi/agent'
7
7
 
8
+ # The main class for the gem. This and the {Agent} class are the main
9
+ # endpoints for interacting with the remote services.
8
10
  class Worochi
9
11
  @agents = []
10
12
 
@@ -17,6 +19,11 @@ class Worochi
17
19
  # Creates a new {Worochi::Agent} and adds it to the list of agents
18
20
  # listening to {Worochi.push} requests.
19
21
  #
22
+ # @param service [Symbol] service name as defined in {Config.services}
23
+ # @param token [String] authorization token for the service API
24
+ # @param opts [Hash] additional service-specific options
25
+ # @return [Worochi::Agent]
26
+ # @see Agent.new
20
27
  # @example
21
28
  # Worochi.create(:dropbox, 'as89h38nFBUSHFfuh99f', { dir: '/folder' })
22
29
  # @example
@@ -27,12 +34,6 @@ class Worochi
27
34
  # commit_msg: 'Hello'
28
35
  # }
29
36
  # Worochi.create(:github, '6st46setsytgbhd64', opts)
30
- #
31
- # @param service [Symbol] service name as defined in {Config.services}
32
- # @param token [String] authorization token for the service API
33
- # @param opts [Hash] additional service-specific options
34
- # @return [Worochi::Agent]
35
- # @see Agent.new
36
37
  def create(service, token, opts={})
37
38
  opts[:service] = service
38
39
  opts[:token] = token
@@ -47,7 +48,7 @@ class Worochi
47
48
  # @param agent [Worochi::Agent]
48
49
  # @return [nil]
49
50
  def add(agent)
50
- @agents << agent
51
+ @agents << agent unless @agents.include?(agent)
51
52
  nil
52
53
  end
53
54
 
@@ -62,13 +63,13 @@ class Worochi
62
63
  end
63
64
 
64
65
  # Remove all agents belonging to a given service from the list. Removes
65
- # all agents if service is not specified. (See {.reset}).
66
- #
67
- # @example
68
- # Worochi.remove(:dropbox)
66
+ # all agents if service is not specified.
69
67
  #
70
68
  # @param service [Symbol] service name as defined in {Config.services}
71
69
  # @return [nil]
70
+ # @see Worochi.reset
71
+ # @example
72
+ # Worochi.remove(:dropbox)
72
73
  def remove_service(service=nil)
73
74
  if service.nil?
74
75
  reset
@@ -78,18 +79,24 @@ class Worochi
78
79
  nil
79
80
  end
80
81
 
81
- # Removes all agents from the list
82
+ # Removes all agents from the list.
82
83
  #
83
84
  # @return [nil]
84
85
  def reset
85
86
  @agents.clear
86
87
  end
87
88
 
88
- # List the active agents.
89
+ # List the active agents in a human-readable form.
89
90
  #
90
- # @return [Array<String>]
91
- def list
92
- @agents.map { |a| a.to_s }
91
+ # @param print [Boolean] prints the result if `true`
92
+ # @return [String] formatted list of agents
93
+ def list(print=true)
94
+ list = @agents.each_with_index.map do |a, i|
95
+ " \033[33m# #{i}\033[0m\t#{a.name}"
96
+ end
97
+ result = list.join("\n")
98
+ puts result if print && !result.empty?
99
+ result
93
100
  end
94
101
 
95
102
  # @return [Integer] number of active agents.