catalog 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: cd0b3cd70c85b5bd03633460099dbe638c5276da
4
+ data.tar.gz: 4778008a6d5863df65ba2f8d01bee7408ab82253
5
+ SHA512:
6
+ metadata.gz: 274bcea64b0b28a55d5fa8f9abd5cd961ada060d8c8d35e8931dbb38adf53163f58a59d31fae4da4b7fb8bb6965281452bbabd86db05926bc66d51cfc65d10e8
7
+ data.tar.gz: 3bb23e9d4ba7070a23afd16301936a3f09a5e20039d8c1cfdc061d2467460369660387b98880ac6d0c2112957809441a7fed92ad8d8c4b27af6ed40a64c6d3c3
@@ -0,0 +1 @@
1
+ *.gem
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source 'https://rubygems.org'
2
+ gemspec
@@ -0,0 +1,33 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ catalog (0.1.1)
5
+ hashie
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ diff-lcs (1.2.5)
11
+ hashie (3.3.1)
12
+ rake (10.3.2)
13
+ rspec (3.0.0)
14
+ rspec-core (~> 3.0.0)
15
+ rspec-expectations (~> 3.0.0)
16
+ rspec-mocks (~> 3.0.0)
17
+ rspec-core (3.0.4)
18
+ rspec-support (~> 3.0.0)
19
+ rspec-expectations (3.0.4)
20
+ diff-lcs (>= 1.2.0, < 2.0)
21
+ rspec-support (~> 3.0.0)
22
+ rspec-mocks (3.0.4)
23
+ rspec-support (~> 3.0.0)
24
+ rspec-support (3.0.4)
25
+
26
+ PLATFORMS
27
+ ruby
28
+
29
+ DEPENDENCIES
30
+ bundler (~> 1.3)
31
+ catalog!
32
+ rake
33
+ rspec
@@ -0,0 +1,24 @@
1
+ lib = File.expand_path('../lib', __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+
4
+ require 'catalog/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'catalog'
8
+ spec.version = Catalog::VERSION
9
+ spec.authors = ['Samuel Garneau']
10
+ spec.email = ['sam@garno.me']
11
+ spec.description = 'Catalog prevent your download folder from being a mess on Mac Os X.'
12
+ spec.summary = 'Catalog prevent your download folder from being a mess on Mac Os X.'
13
+ spec.homepage = 'https://github.com/garno/catalog'
14
+ spec.license = 'BSD 3-Clause'
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.require_paths = ['lib']
18
+
19
+ spec.add_development_dependency 'bundler', '~> 1.3'
20
+ spec.add_development_dependency 'rake'
21
+ spec.add_development_dependency 'rspec'
22
+
23
+ spec.add_dependency 'hashie'
24
+ end
@@ -0,0 +1,21 @@
1
+ require 'yaml'
2
+ require 'hashie'
3
+ require 'shellwords'
4
+
5
+ # Core
6
+ require 'catalog/organizer'
7
+ require 'catalog/config'
8
+
9
+ # Models
10
+ require 'catalog/models/document'
11
+ require 'catalog/models/drawer'
12
+
13
+ # Services
14
+ require 'catalog/services/drawer_matcher'
15
+ require 'catalog/services/document_mover'
16
+
17
+ # Errors
18
+ require 'catalog/errors/configuration_missing'
19
+
20
+ module Catalog
21
+ end
@@ -0,0 +1,21 @@
1
+ module Catalog
2
+ module Config
3
+ def self.drawers
4
+ config.drawers
5
+ end
6
+
7
+ private
8
+
9
+ def self.config
10
+ unless @config
11
+ raw_content = File.read(File.expand_path('~/.catalog.yml'))
12
+ yaml_content = YAML.load(raw_content)
13
+ @config = Hashie::Mash.new(yaml_content)
14
+ end
15
+
16
+ @config
17
+ rescue Errno::ENOENT
18
+ raise Catalog::Error::ConfigurationMissing
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,9 @@
1
+ module Catalog
2
+ module Error
3
+ class ConfigurationMissing < StandardError
4
+ def to_s
5
+ '~/.catalog.yml is missing'
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,9 @@
1
+ module Catalog
2
+ class Document
3
+ attr_accessor :path
4
+
5
+ def initialize(path:)
6
+ @path = path
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,14 @@
1
+ module Catalog
2
+ class Drawer
3
+ attr_accessor :rule, :path
4
+
5
+ def initialize(rule:, path:)
6
+ @rule = rule
7
+ @path = path
8
+ end
9
+
10
+ def self.all
11
+ Catalog::Config.drawers.map { |attributes| self.class.new(attributes) }
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,27 @@
1
+ module Catalog
2
+ class Organizer
3
+ def initialize(base_path:)
4
+ @base_path = base_path
5
+ end
6
+
7
+ def run!
8
+ new_documents.each do |document|
9
+ matcher = DrawerMatcher.new(document: document)
10
+
11
+ if matcher.match?
12
+ DocumentMover.new(document: document, drawer: matcher.drawer).move!
13
+ end
14
+ end
15
+ end
16
+
17
+ private
18
+
19
+ def new_documents
20
+ unorganized_files.map { |path| Document.new(path: path) }
21
+ end
22
+
23
+ def unorganized_files
24
+ Dir[File.expand_path(@base_path)].reject { |path| File.directory?(path) }
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,23 @@
1
+ module Catalog
2
+ class DocumentMover
3
+ def initialize(document:, drawer:)
4
+ @document = document
5
+ @drawer = drawer
6
+ end
7
+
8
+ def move!
9
+ create_drawer_folder!
10
+ move_file!
11
+ end
12
+
13
+ private
14
+
15
+ def create_drawer_folder!
16
+ FileUtils.mkdir_p(@drawer.path) unless File.directory?(@drawer.path)
17
+ end
18
+
19
+ def move_file!
20
+ FileUtils.mv(@document.path, @drawer.path)
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,32 @@
1
+ module Catalog
2
+ class DrawerMatcher
3
+ # Accessors
4
+ attr_reader :drawer
5
+
6
+ def initialize(document:)
7
+ @document = document
8
+ end
9
+
10
+ def match?
11
+ Catalog::Drawer.all.each do |drawer|
12
+ document_where_froms.each do |url|
13
+ @drawer = drawer if /#{drawer.rule}/.match(url)
14
+
15
+ break if @drawer
16
+ end
17
+
18
+ break if @drawer
19
+ end
20
+
21
+ !@drawer.nil?
22
+ end
23
+
24
+ private
25
+
26
+ def document_where_froms
27
+ # TODO: Have an helper to execute shell commands. Will
28
+ # be a lot easier to test.
29
+ @document_where_froms ||= URI.extract(`mdls -name kMDItemWhereFroms #{Shellwords.shellescape(@document.path)}`)
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,3 @@
1
+ module Catalog
2
+ VERSION = '0.1.1'
3
+ end
@@ -0,0 +1,25 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe Catalog::DocumentMover do
4
+ let(:mover) { Catalog::DocumentMover.new(document: document, drawer: drawer) }
5
+ let(:document) { Catalog::Document.new(path: '/path/to/document.pdf') }
6
+ let(:drawer) { Catalog::Drawer.new(path: '/path/to/drawer', rule: nil) }
7
+
8
+ describe :move! do
9
+ specify do
10
+ expect(File).to receive(:directory?).with(drawer.path).and_return(true)
11
+ expect(FileUtils).to receive(:mv).with(document.path, drawer.path)
12
+
13
+ mover.move!
14
+ end
15
+ end
16
+
17
+ describe :create_drawer_folder! do
18
+ specify do
19
+ expect(File).to receive(:directory?).with(drawer.path).and_return(false)
20
+ expect(FileUtils).to receive(:mkdir_p).with(drawer.path)
21
+
22
+ mover.send(:create_drawer_folder!)
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,48 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe Catalog::DrawerMatcher do
4
+ let(:matcher) { Catalog::DrawerMatcher.new(document: document) }
5
+ let(:document) { Catalog::Document.new(path: '/path/to/document') }
6
+
7
+ before { expect(Catalog::Drawer).to receive(:all).and_return(drawers) }
8
+
9
+ describe :match? do
10
+ let(:drawers) { [Catalog::Drawer.new(rule: 'basecamp', path: '/path/to/drawer')] }
11
+
12
+ context 'with a matching drawer' do
13
+ specify do
14
+ expect(matcher).to receive(:document_where_froms).and_return(['http://basecamp.com/path/to/document.pdf'])
15
+ expect(matcher.match?).to be_truthy
16
+ end
17
+ end
18
+
19
+ context 'with no matching drawer' do
20
+ specify do
21
+ expect(matcher).to receive(:document_where_froms).and_return(['http://pivotaltracker.com/path/to/document.pdf'])
22
+ expect(matcher.match?).to be_falsey
23
+ end
24
+ end
25
+
26
+ context 'with two matching drawers' do
27
+ let(:drawers) do
28
+ [
29
+ Catalog::Drawer.new(rule: 'basecamp', path: '/path/to/drawer'),
30
+ Catalog::Drawer.new(rule: 'base', path: '/path/to/drawer')
31
+ ]
32
+ end
33
+
34
+ specify do
35
+ expect(matcher).to receive(:document_where_froms).and_return(['http://basecamp.com/path/to/document.pdf'])
36
+ expect(matcher.match?).to be_truthy
37
+
38
+ # For now, Catalog simply select the first
39
+ # matching drawer. Keep that in mind.
40
+ expect(matcher.drawer).to eq(drawers.first)
41
+ end
42
+ end
43
+ end
44
+
45
+ describe :document_where_froms do
46
+ pending
47
+ end
48
+ end
@@ -0,0 +1,9 @@
1
+ $LOAD_PATH.unshift File.expand_path('../lib', __FILE__)
2
+
3
+ require 'catalog'
4
+
5
+ RSpec.configure do |config|
6
+ config.expect_with :rspec do |c|
7
+ c.syntax = :expect
8
+ end
9
+ end
metadata ADDED
@@ -0,0 +1,116 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: catalog
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Samuel Garneau
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-09-03 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: hashie
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: Catalog prevent your download folder from being a mess on Mac Os X.
70
+ email:
71
+ - sam@garno.me
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - Gemfile
78
+ - Gemfile.lock
79
+ - catalog.gemspec
80
+ - lib/catalog.rb
81
+ - lib/catalog/config.rb
82
+ - lib/catalog/errors/configuration_missing.rb
83
+ - lib/catalog/models/document.rb
84
+ - lib/catalog/models/drawer.rb
85
+ - lib/catalog/organizer.rb
86
+ - lib/catalog/services/document_mover.rb
87
+ - lib/catalog/services/drawer_matcher.rb
88
+ - lib/catalog/version.rb
89
+ - spec/services/document_mover_spec.rb
90
+ - spec/services/drawer_matcher_spec.rb
91
+ - spec/spec_helper.rb
92
+ homepage: https://github.com/garno/catalog
93
+ licenses:
94
+ - BSD 3-Clause
95
+ metadata: {}
96
+ post_install_message:
97
+ rdoc_options: []
98
+ require_paths:
99
+ - lib
100
+ required_ruby_version: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ required_rubygems_version: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - ">="
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ requirements: []
111
+ rubyforge_project:
112
+ rubygems_version: 2.2.2
113
+ signing_key:
114
+ specification_version: 4
115
+ summary: Catalog prevent your download folder from being a mess on Mac Os X.
116
+ test_files: []