codeunion 0.0.2 → 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 4974f6eb01104b8573b4e0deaa5639e1086032cc
4
- data.tar.gz: dbe02e702d20aa4879662b91dfe0b736931d1c45
3
+ metadata.gz: 52ea6972b9909f54b51efc7492a367c1a52b390e
4
+ data.tar.gz: b107ddbc578eb45603bbac1f1aa804938508f48b
5
5
  SHA512:
6
- metadata.gz: efec75d73b5607462b3fda7b39354b96125916273602895ee26178c3de172dfe7f79f699d7a707433969020e7ab167617bc8cff3c70403c9b10016017dda2958
7
- data.tar.gz: 20861af23710e2f96c2ee2486bab9261f988fe5e7dc4d29478687a01d540779eabb392da261f1925218f74692b7c930e47844fde47eb7e23ac90330e8b950392
6
+ metadata.gz: cfa18400fa8db69c0d40157fc8f1f2bd28b8475e0875278abf50585f50f8ae6c67dae18db445b8997da2abb2dfc881ca24d9097e88b1a358b070ab2730211ef3
7
+ data.tar.gz: 0ad6a777f8a1949284c155c49b73cca2185ce251068ef437fac55c953f45d6182fcf253bf6b1ca1d371decbdd93d4440b6680b19c6b0bdcc3f68f3ca711f6395
data/.gitignore CHANGED
@@ -80,3 +80,5 @@ Desktop.ini
80
80
 
81
81
  # KDE directory preferences
82
82
  .directory
83
+
84
+ test/unit/fixtures/mutable_config
@@ -1,6 +1,7 @@
1
1
  AllCops:
2
2
  Exclude:
3
3
  - '*.gemspec'
4
+ - 'test/fixtures/**/*.rb'
4
5
 
5
6
  Style/Documentation:
6
7
  Exclude:
@@ -9,7 +10,41 @@ Style/Documentation:
9
10
  Style/FileName:
10
11
  Exclude:
11
12
  - 'bin/*'
13
+ - 'libexec/*'
14
+
15
+ Style/LineEndConcatenation:
16
+ Enabled: false
17
+
18
+ Style/HashSyntax:
19
+ Enabled: true
20
+ EnforcedStyle: hash_rockets
21
+
22
+ Style/IndentHash:
23
+ EnforcedStyle: consistent
24
+
25
+
26
+ Style/BracesAroundHashParameters:
27
+ EnforcedStyle: braces
28
+
29
+
30
+ Style/WordArray:
31
+ Enabled: false
12
32
 
13
33
  Style/StringLiterals:
14
34
  Enabled: true
15
35
  EnforcedStyle: double_quotes
36
+
37
+ Style/StringLiteralsInInterpolation:
38
+ Enabled: false
39
+
40
+ Style/MethodCallParentheses:
41
+ Enabled: false
42
+
43
+ Metrics/MethodLength:
44
+ Max: 20
45
+
46
+ Metrics/LineLength:
47
+ Max: 120
48
+
49
+ Metrics/AbcSize:
50
+ Max: 25
@@ -0,0 +1,101 @@
1
+ 193_version = 1.9.3-p551
2
+ 193_env = RBENV_VERSION=$(193_version)
3
+
4
+ 20_version = 2.0.0-p598
5
+ 20_env = RBENV_VERSION=$(20_version)
6
+
7
+ 21_version = 2.1.5
8
+ 21_env = RBENV_VERSION=$(21_version)
9
+
10
+ rerun = wach -e test/unit/fixtures/\* make
11
+ test_unit = bundle exec ruby test/unit.rb
12
+ test_feature = bundle exec ruby test/features.rb
13
+
14
+ test: unit-test feature-test
15
+
16
+ unit-test: unit-test-193 unit-test-20 unit-test-21
17
+
18
+ retest-193:
19
+ $(rerun) unit-test-193
20
+
21
+ unit-test-193:
22
+ $(193_env) $(test_unit)
23
+
24
+ retest-20:
25
+ $(rerun) unit-test-20
26
+
27
+ unit-test-20:
28
+ $(20_env) $(test_unit)
29
+
30
+ retest-21:
31
+ $(rerun) unit-test-21
32
+
33
+ unit-test-21:
34
+ $(21_env) $(test_unit)
35
+
36
+
37
+ feature-test: feature-test-193 feature-test-20 feature-test-21
38
+
39
+ feature-test-193:
40
+ $(193_env) $(test_feature)
41
+
42
+ feature-test-20:
43
+ $(20_env) $(test_feature)
44
+
45
+ feature-test-21:
46
+ $(21_env) $(test_feature)
47
+
48
+ install: install-deps install-193 install-20 install-21
49
+
50
+ install-deps: install-wach install-rbenv install-ruby-build
51
+ brew install npm
52
+ npm -g install wach
53
+
54
+ install-ruby-build:
55
+ brew install ruby-build
56
+
57
+ install-rbenv:
58
+ brew install rbenv
59
+
60
+ install-wach: install-npm
61
+ npm -g install wach
62
+
63
+ install-npm:
64
+ brew install npm
65
+
66
+ install-193: install-ruby-193 install-gems-193
67
+ install-20: install-ruby-20 install-gems-20
68
+ install-21: install-ruby-21 install-gems-21
69
+
70
+ install_ruby = rbenv install -s
71
+
72
+ install-ruby-193:
73
+ $(install_ruby) $(193_version)
74
+
75
+ install-ruby-20:
76
+ $(install_ruby) $(20_version)
77
+
78
+ install-ruby-21:
79
+ $(install_ruby) $(21_version)
80
+
81
+
82
+ install_bundler = gem install bundler
83
+ install_gems = bundle install
84
+
85
+ install-gems-193:
86
+ $(193_env) $(install_bundler)
87
+ $(193_env) $(install_gems)
88
+
89
+ install-gems-20:
90
+ $(20_env) $(install_bundler)
91
+ $(20_env) $(install_gems)
92
+
93
+ install-gems-21:
94
+ $(21_env) $(install_bundler)
95
+ $(21_env) $(install_gems)
96
+
97
+ lint:
98
+ bundle exec rubocop
99
+
100
+ relint:
101
+ $(rerun) lint
data/README.md CHANGED
@@ -33,3 +33,16 @@ $ codeunion waffles
33
33
  the CodeUnion tool would look for an executable named `codeunion-waffles`. If
34
34
  the executable exists, the tool will run it. If it doesn't exist, we would see
35
35
  a `CommandNotFound` error.
36
+
37
+ ## Development
38
+
39
+ The CodeUnion client targets Ruby 1.9.3, 2.0, and 2.1. Assuming you have
40
+ [homebrew](http://brew.sh) installed already:
41
+
42
+ ```
43
+ make install # Installs npm, wach, rbenv, ruby-build and ruby 1.9.3, 2.0
44
+ # and 2.1
45
+ make unit-test # Runs the unit tests against all ruby versions. Thread-safe.
46
+ make feature-test # Runs the feature tests against all ruby versions. Not-thread-safe.
47
+ make test # Runs unit and feature tests against all ruby versions
48
+ ```
@@ -8,6 +8,14 @@ bin_file = Pathname.new(__FILE__).realpath
8
8
  # Add the gem's "lib" directory to library path
9
9
  $LOAD_PATH.unshift File.expand_path("../../lib", bin_file)
10
10
 
11
+ # Add our gem-specific "libexec" directory to the PATH
12
+ ENV["PATH"] += ":" + File.expand_path("../../libexec", bin_file)
13
+
11
14
  require "codeunion/command/main"
12
15
 
13
- CodeUnion::Command::Main.new(*ARGV).run
16
+ options = {
17
+ :command_name => ARGV.first,
18
+ :command_args => ARGV.drop(1)
19
+ }
20
+
21
+ CodeUnion::Command::Main.new(options).run
@@ -21,6 +21,12 @@ Gem::Specification.new do |spec|
21
21
  spec.add_development_dependency "bundler", "~> 1.7"
22
22
  spec.add_development_dependency "rake", "~> 10.0"
23
23
  spec.add_development_dependency "rubocop", "~> 0.27"
24
+ spec.add_development_dependency "minitest", "~> 5.5"
24
25
 
25
26
  spec.add_dependency "ptools", "~> 1.2"
27
+ spec.add_dependency "faraday", "~> 0.9"
28
+ spec.add_dependency "faraday_middleware", "~> 0.9"
29
+ spec.add_dependency "addressable", "~> 2.3"
30
+ spec.add_dependency "multi_json", "~> 1.10"
31
+ spec.add_dependency "rainbow", "~> 2.0"
26
32
  end
@@ -0,0 +1,16 @@
1
+ # require "codeunion"
2
+ require "codeunion/http_client"
3
+
4
+ module CodeUnion
5
+ # A simple module for interacting with the CodeUnion API
6
+ class API
7
+ attr_reader :http_client
8
+ def initialize(host, version)
9
+ @http_client = HTTPClient.new("http://#{host}/#{version}")
10
+ end
11
+
12
+ def search(params = {})
13
+ http_client.get("search", params)
14
+ end
15
+ end
16
+ end
@@ -2,6 +2,27 @@ require "codeunion"
2
2
 
3
3
  module CodeUnion
4
4
  module Command
5
+ # Raised when a command doesn't exist
5
6
  class CommandNotFound < StandardError; end
7
+
8
+ # Raised when a command is missing required configuration fields
9
+ #
10
+ # Example:
11
+ # fail(MissingConfig, {
12
+ # :name => "codeunion.api_token",
13
+ # :help => "See: http://codeunion.com/guides/creating-a-codeunion-access-token"
14
+ # }
15
+ class MissingConfig < StandardError
16
+ def initialize(field)
17
+ super("Run 'codeunion config set #{field[:name]} VALUE'#{help_text(field)}")
18
+ end
19
+
20
+ def help_text(field)
21
+ field[:help] ? "\n#{field[:help]}" : ""
22
+ end
23
+ end
24
+
25
+ # Raised when input isn't appropriate for a command
26
+ class InvalidInput < StandardError; end
6
27
  end
7
28
  end
@@ -4,13 +4,13 @@ module CodeUnion
4
4
  module Command
5
5
  # A base class for built-in commands
6
6
  class Base
7
- def initialize(*args)
8
- @args = args
7
+ def initialize(options)
8
+ @options = options
9
9
  end
10
10
 
11
11
  private
12
12
 
13
- attr_reader :args
13
+ attr_reader :options
14
14
  end
15
15
  end
16
16
  end
@@ -4,43 +4,30 @@ require "fileutils"
4
4
 
5
5
  module CodeUnion
6
6
  module Command
7
- # The bulit-in `codeunion config` command
7
+ # The built-in `codeunion config` command
8
8
  class Config < Base
9
- CONFIG_DIR = File.join(Dir.home, ".codeunion")
10
- CONFIG_FILE = File.join(CONFIG_DIR, "config")
11
-
12
9
  def run
13
- ensure_config_exists!
14
-
15
- case args.length
16
- when 1
17
- puts config.get(*args)
18
- when 2
19
- config.set(*args)
10
+ edit_config do
11
+ if options[:command] == "get"
12
+ return config.get(options[:input].join(""))
13
+ elsif options[:command] == "set"
14
+ config.set(*options[:input])
15
+ elsif options[:command] == "unset"
16
+ config.unset(options[:input].join(""))
17
+ end
20
18
  end
21
-
22
- save_config!
19
+ nil
23
20
  end
24
21
 
25
22
  private
26
23
 
27
- def config
28
- @config ||= CodeUnion::Config.new(YAML.load_file(CONFIG_FILE))
29
- end
30
-
31
- def ensure_config_exists!
32
- FileUtils.mkdir(CONFIG_DIR) unless Dir.exist?(CONFIG_DIR)
33
- write_config_data({}) unless File.exist?(CONFIG_FILE)
34
- end
35
-
36
- def save_config!
37
- write_config_data(config)
24
+ def edit_config
25
+ yield
26
+ config.write
38
27
  end
39
28
 
40
- def write_config_data(data)
41
- File.open(CONFIG_FILE, "w") do |f|
42
- f.write YAML.dump(Hash(data))
43
- end
29
+ def config
30
+ @config ||= CodeUnion::Config.load
44
31
  end
45
32
  end
46
33
  end
@@ -0,0 +1,39 @@
1
+ require "codeunion/command/base"
2
+ require "codeunion/feedback_request"
3
+ require "codeunion/config"
4
+
5
+ module CodeUnion
6
+ module Command
7
+ # "View" in Traditional MVC for the Feedback command. Validates Input
8
+ # and Builds response
9
+ class Feedback < Base
10
+ CREATE_ACCESS_TOKEN_URL =
11
+ "https://help.github.com/articles/creating-an-access-token-for-command-line-use/"
12
+
13
+ def run
14
+ config = Config.load
15
+ token = config.get("github.access_token")
16
+ repository = config.get("feedback.repository")
17
+ if !token
18
+ fail(CodeUnion::Command::MissingConfig, { :name => "github.access_token",
19
+ :help => "See: #{CREATE_ACCESS_TOKEN_URL }" })
20
+ elsif !repository
21
+ fail(CodeUnion::Command::MissingConfig, { :name => "feedback.repository" })
22
+ end
23
+ @feedback_request = FeedbackRequest.new(input, token, repository)
24
+ ensure_valid_input!
25
+ @feedback_request.send!
26
+ end
27
+
28
+ private
29
+
30
+ def input
31
+ options[:input].join("")
32
+ end
33
+
34
+ def ensure_valid_input!
35
+ fail(CodeUnion::Command::InvalidInput, @feedback_request.errors) unless @feedback_request.valid?
36
+ end
37
+ end
38
+ end
39
+ end
@@ -20,11 +20,11 @@ module CodeUnion
20
20
  private
21
21
 
22
22
  def command_name
23
- args.first
23
+ options[:command_name]
24
24
  end
25
25
 
26
26
  def command_args
27
- args.drop(1)
27
+ options[:command_args]
28
28
  end
29
29
 
30
30
  def executable_name
@@ -0,0 +1,34 @@
1
+ require "codeunion/command/base"
2
+ require "codeunion/api"
3
+ require "codeunion/helpers/text"
4
+ require "codeunion/search"
5
+
6
+ require "faraday"
7
+ require "rainbow"
8
+ require "io/console"
9
+
10
+ module CodeUnion
11
+ module Command
12
+ # The built-in `codeunion search` command
13
+ class Search < Base
14
+ include CodeUnion::Helpers::Text
15
+
16
+ def run
17
+ results.inject([]) do |lines, result|
18
+ lines.push(CodeUnion::Search::ResultPresenter.new(result).to_s)
19
+ lines << format_output("")
20
+ end.join("\n")
21
+ end
22
+
23
+ def results
24
+ @results ||= api.search(options)
25
+ end
26
+
27
+ private
28
+
29
+ def api
30
+ @api ||= CodeUnion::API.new("api.codeunion.io", "v1")
31
+ end
32
+ end
33
+ end
34
+ end
@@ -1,10 +1,17 @@
1
1
  require "yaml"
2
2
 
3
3
  module CodeUnion
4
- # A class to manage CodeUnion configuration data
4
+ # Manages CodeUnion configuration data
5
5
  class Config
6
- def initialize(config_data)
7
- @config = Hash(config_data.dup)
6
+ DEFAULT_CONFIG_FILE = File.join(Dir.home, ".codeunion", "config")
7
+
8
+ def self.load(config_file_path = DEFAULT_CONFIG_FILE)
9
+ CodeUnion::Config.new(FileAdapter.new(config_file_path))
10
+ end
11
+
12
+ def initialize(config_file)
13
+ @file = config_file
14
+ @config = Hash.try_convert(config_file)
8
15
  @config.default_proc = proc { |h, k| h[k] = {} }
9
16
  end
10
17
 
@@ -13,6 +20,11 @@ module CodeUnion
13
20
  self
14
21
  end
15
22
 
23
+ def unset(dotted_key)
24
+ @config = unset_dotted(@config, dotted_key)
25
+ self
26
+ end
27
+
16
28
  def get(dotted_key)
17
29
  get_dotted(@config, dotted_key)
18
30
  end
@@ -23,6 +35,10 @@ module CodeUnion
23
35
 
24
36
  alias_method :to_h, :to_hash
25
37
 
38
+ def write
39
+ @file.write(to_hash)
40
+ end
41
+
26
42
  private
27
43
 
28
44
  def set_dotted(config, dotted_key, value)
@@ -30,7 +46,18 @@ module CodeUnion
30
46
 
31
47
  key, sub_key = extract_subkey(dotted_key)
32
48
 
33
- config.merge(key => set_dotted(config[key], sub_key, value))
49
+ config.merge({ key => set_dotted(config[key], sub_key, value) })
50
+ end
51
+
52
+ def unset_dotted(config, dotted_key)
53
+ key, sub_key = extract_subkey(dotted_key)
54
+ if sub_key
55
+ unset_dotted(config[key], sub_key)
56
+ config.delete(key) if config[key].empty?
57
+ else
58
+ config.delete(key)
59
+ end
60
+ config
34
61
  end
35
62
 
36
63
  def get_dotted(config, dotted_key)
@@ -46,5 +73,40 @@ module CodeUnion
46
73
  def extract_subkey(dotted_key)
47
74
  dotted_key.split(".", 2)
48
75
  end
76
+
77
+ # Reads and writes YAML to the FileSystem
78
+ class FileAdapter
79
+ attr_reader :file_path
80
+
81
+ def initialize(file_path)
82
+ @file_path = file_path
83
+ end
84
+
85
+ def write(data)
86
+ ensure_exists!
87
+ write_fearlessly(data)
88
+ end
89
+
90
+ def to_hash
91
+ ensure_exists!
92
+ YAML.load_file(file_path).dup
93
+ end
94
+
95
+ alias_method :to_h, :to_hash
96
+
97
+ private
98
+
99
+ def write_fearlessly(data)
100
+ File.open(file_path, "w") do |f|
101
+ f.write YAML.dump(Hash.try_convert(data))
102
+ end
103
+ end
104
+
105
+ def ensure_exists!
106
+ config_dir = File.dirname(file_path)
107
+ FileUtils.mkdir(config_dir) unless Dir.exist?(config_dir)
108
+ write_fearlessly({}) unless File.exist?(file_path)
109
+ end
110
+ end
49
111
  end
50
112
  end