cog-rb 0.1.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 5858a013f657bf57c9dc372951d33d4f915ce73f
4
+ data.tar.gz: c7318d78922ad2f13da3899069f73b815c2d1904
5
+ SHA512:
6
+ metadata.gz: 4bbb2620093d355de471842989e383b47c72e18fa02f3ae395d07cb856b956fdfe419864a6fc2eda95bb2be44e89beebd036db2b7f1eb6593d2e026b61fe47da
7
+ data.tar.gz: be7ee2c72c195907a2f7d9d2727f6f79fcf9b87cec35e70db0cd3f21b8c5a54087489fb458d6880c94929453445d579dadb3013b9152dce6c6f91bb24be02baf
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
@@ -0,0 +1,3 @@
1
+ This project is subject to the Operable Code of Conduct. See
2
+ [CODE_OF_CONDUCT.md](https://github.com/operable/cog/blob/master/CODE_OF_CONDUCT.md) in the main
3
+ [Cog](https://github.com/operable/cog) repo for more details.
data/CONTRIBUTING.MD ADDED
@@ -0,0 +1 @@
1
+ For information about making contributions to this project please see [CONTRIBUTING.md](https://github.com/operable/cog/blob/master/CONTRIBUTING.md) in the main [Cog](https://github.com/operable/cog) repo.
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in cog-rb.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,13 @@
1
+ Copyright 2015 Operable, Inc.
2
+
3
+ Licensed under the Apache License, Version 2.0 (the "License");
4
+ you may not use this file except in compliance with the License.
5
+ You may obtain a copy of the License at
6
+
7
+ http://www.apache.org/licenses/LICENSE-2.0
8
+
9
+ Unless required by applicable law or agreed to in writing, software
10
+ distributed under the License is distributed on an "AS IS" BASIS,
11
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ See the License for the specific language governing permissions and
13
+ limitations under the License.
data/README.md ADDED
@@ -0,0 +1,45 @@
1
+ # cog-rb
2
+
3
+ Simple, opinionated library for building Cog commands in Ruby.
4
+
5
+ ## Usage
6
+
7
+ First, create a file named `cog-command` at the top level of your project directory. This file should look something like the following, with `format` replaced with the name of your bundle:
8
+
9
+ ```ruby
10
+ #!/usr/bin/env ruby
11
+
12
+ # Make sure we're in the top-level directory for the command
13
+ # since so many paths are relative.
14
+ Dir.chdir(File.dirname(__FILE__))
15
+
16
+ require 'bundler/setup'
17
+ require 'cog'
18
+
19
+ Cog.bundle('format')
20
+ ```
21
+
22
+ This file will be the target `executable` for every command in your bundle. The library handles routing commands to the correct class based on the command name. Note: The permissions on this file must allow it to be run by your Relay.
23
+
24
+ The class that implements your command should be named the same as your command and should be declared in a namespace named after your bundle within the `CogCmd` toplevel namespace. For instance, if you had a bundle named **test** and a command named **dump**, the class that implements the **dump** command would be called `CogCmd::Test::Dump`. The implementation for the command would live in `lib/cog_cmd/test/dump.rb` relative to the location of your `cog-command` script.
25
+
26
+ ## Examples
27
+
28
+ See the [cog-bundles/format](https://github.com/cog-bundles/format) repository for an example of this library in action.
29
+
30
+ ## Installation
31
+
32
+ Add this line to your application's Gemfile:
33
+
34
+ ```ruby
35
+ gem 'cog-rb'
36
+ ```
37
+
38
+ And then execute:
39
+
40
+ $ bundle
41
+
42
+ Or install it yourself as:
43
+
44
+ $ gem install cog-rb
45
+
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "cog/rb"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/cog-rb.gemspec ADDED
@@ -0,0 +1,21 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'cog/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "cog-rb"
8
+ spec.version = Cog::VERSION
9
+ spec.authors = ["Mark Imbriaco"]
10
+ spec.email = ["mark@operable.io"]
11
+
12
+ spec.summary = "Cog command helper library"
13
+ spec.homepage = "https://github.com/cog-bundles/cog-rb"
14
+
15
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
16
+ spec.bindir = "bin"
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.add_development_dependency "bundler", "~> 1.11"
21
+ end
data/lib/cog/bundle.rb ADDED
@@ -0,0 +1,40 @@
1
+ class Cog
2
+ class Bundle
3
+ attr_reader :name, :config
4
+
5
+ def initialize(name, base_dir: nil, config_file: nil)
6
+ @name = name
7
+ @base_dir = base_dir || File.dirname($0)
8
+ @config = load_config(config_file || File.join(@base_dir, 'config.yaml'))
9
+ @module = create_bundle_module
10
+
11
+ load_commands
12
+ end
13
+
14
+ def create_bundle_module
15
+ return if Object.const_defined?('CogCmd')
16
+
17
+ Object.const_set('CogCmd', Module.new)
18
+ CogCmd.const_set(@name.capitalize, Module.new)
19
+ end
20
+
21
+ def load_config(path=nil)
22
+ path ||= File.join(@base_dir, 'config.yaml')
23
+ Cog::Config.new(path)
24
+ end
25
+
26
+ def load_commands
27
+ @config[:commands].each do |command, config|
28
+ require File.join(@base_dir, 'lib', 'cog_cmd', @name, command)
29
+ end
30
+ end
31
+
32
+ def run_command
33
+ command = ENV['COG_COMMAND']
34
+ command_class = command.gsub(/(\A|_)([a-z])/) { $2.upcase }
35
+
36
+ target = @module.const_get(command_class).new
37
+ target.execute
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,65 @@
1
+
2
+ require 'json'
3
+
4
+ class Cog
5
+ class Command
6
+ attr_writer :memory_key, :config
7
+
8
+ def request
9
+ @request ||= Cog::Request.new
10
+ end
11
+
12
+ def response
13
+ @response ||= Cog::Response.new
14
+ end
15
+
16
+ def memory_key
17
+ @memory_key ||= ENV['COG_INVOCATION_ID']
18
+ end
19
+
20
+ def config
21
+ @config ||= { input: self.class.input }
22
+ end
23
+
24
+ def step
25
+ step = ENV['COG_INVOCATION_STEP']
26
+ step.nil? ? nil : step.to_sym
27
+ end
28
+
29
+ def accumulate_input
30
+ Cog::Services::Memory.accum(memory_key, request.input)
31
+ end
32
+
33
+ def fetch_input
34
+ JSON.parse(Cog::Services::Memory.get(memory_key))
35
+ end
36
+
37
+ def execute
38
+ accumulate_input if config[:input] == :accumulate
39
+ run_command
40
+ response.send
41
+ end
42
+
43
+ def env_var(var, suffix: nil, required: false, failure_message: nil)
44
+ key = suffix ? "#{var}_#{suffix.upcase}" : var
45
+ value = ENV[key]
46
+
47
+ if required and value.nil?
48
+ message = failure_message ? failure_message : "Required environment variable #{key} missing!"
49
+ fail(message)
50
+ else
51
+ value
52
+ end
53
+ end
54
+
55
+ def fail(message)
56
+ STDERR.puts(message)
57
+ exit 1
58
+ end
59
+
60
+ def self.input(value=nil)
61
+ return @input if value.nil?
62
+ @input = value
63
+ end
64
+ end
65
+ end
data/lib/cog/config.rb ADDED
@@ -0,0 +1,18 @@
1
+
2
+ require "yaml"
3
+
4
+ class Cog
5
+ class Config
6
+ def initialize(config_file)
7
+ @config = YAML.load(File.read(config_file))
8
+ end
9
+
10
+ def dump
11
+ YAML.dump(@config)
12
+ end
13
+
14
+ def [](key)
15
+ @config[key.to_s]
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,27 @@
1
+
2
+ require 'json'
3
+
4
+ class Cog
5
+ class Request
6
+ attr_reader :options, :args, :input
7
+
8
+ def initialize
9
+ @args = populate_args
10
+ @options = populate_options
11
+ @input = JSON.parse(STDIN.read)
12
+ end
13
+
14
+ private
15
+
16
+ def populate_args
17
+ (0 .. (ENV['COG_ARGC'].to_i - 1)).map { |n| ENV["COG_ARGV_#{n}"] }
18
+ end
19
+
20
+ def populate_options
21
+ return {} if ENV['COG_OPTS'].nil?
22
+
23
+ options = ENV["COG_OPTS"].split(",")
24
+ Hash[options.map { |opt| [ opt, ENV["COG_OPT_#{opt.upcase}"] ]}]
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,48 @@
1
+
2
+ require 'json'
3
+
4
+ class Cog
5
+ class Response
6
+ LOG_LEVELS = [ :debug, :info, :warn, :err, :error ]
7
+
8
+ attr_accessor :template, :content
9
+
10
+ def []=(key, value)
11
+ @content ||= {}
12
+ @content[key] = value
13
+ end
14
+
15
+ def send
16
+ return if content.nil?
17
+
18
+ write "COG_TEMPLATE: #{@template}" unless @template.nil?
19
+
20
+ case content.class
21
+ when String
22
+ write @content.join('').to_json
23
+ else
24
+ write "JSON\n" + @content.to_json
25
+ end
26
+ end
27
+
28
+ def write(message)
29
+ self.class.write(message)
30
+ end
31
+
32
+ def log(level, message)
33
+ self.class.log(level, message)
34
+ end
35
+
36
+ class << self
37
+ def write(message)
38
+ puts message
39
+ STDOUT.flush
40
+ end
41
+
42
+ def log(level, message)
43
+ level = LOG_LEVELS.include?(level) ? level : :info
44
+ write "COGCMD_#{level.to_s.upcase}: #{message}"
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,70 @@
1
+
2
+ require 'net/http'
3
+
4
+ class Cog
5
+ class Service
6
+ class << self
7
+ def headers
8
+ {
9
+ "Authorization" => "pipeline #{ENV['COG_SERVICE_TOKEN']}",
10
+ "Content-Type" => "application/json"
11
+ }
12
+ end
13
+
14
+ def uri_for(path=nil)
15
+ service_path =
16
+ if path.class == URI::HTTP
17
+ path
18
+ else
19
+ [ service_name, version, path ].compact.join('/')
20
+ end
21
+
22
+ URI(ENV['COG_SERVICES_ROOT'] + '/v1/services/' + service_path)
23
+ end
24
+
25
+ def service_name
26
+ get_const('SERVICE_NAME', self.to_s.split('::').last.downcase)
27
+ end
28
+
29
+ def version
30
+ get_const('VERSION')
31
+ end
32
+
33
+ def get(path=nil)
34
+ uri = uri_for(path)
35
+ res = Net::HTTP.start(uri.host, uri.port) do |http|
36
+ req = Net::HTTP::Get.new(uri)
37
+ headers.each { |k,v| req[k] = v }
38
+ http.request(req)
39
+ end
40
+
41
+ res.body
42
+ end
43
+
44
+ def post(path: nil, data: nil)
45
+ put_or_post(:post, path: path, data: data)
46
+ end
47
+
48
+ def put(path: nil, data: nil)
49
+ put_or_post(:put, path: path, data: data)
50
+ end
51
+
52
+ private
53
+
54
+ def get_const(name, default=nil)
55
+ self.const_defined?(name) ? self.const_get(name) : default
56
+ end
57
+
58
+ def put_or_post(type, path: nil, data: nil)
59
+ uri = uri_for(path)
60
+ http_class = (type == :post) ? Net::HTTP::Post : Net::HTTP::Put
61
+ req = http_class.new(uri, headers)
62
+ req.body = data
63
+
64
+ res = Net::HTTP.start(uri.host, uri.port) do |http|
65
+ http.request(req)
66
+ end
67
+ end
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,17 @@
1
+ class Cog
2
+ class Services
3
+ class Memory < Cog::Service
4
+ VERSION = "1.0.0"
5
+
6
+ class << self
7
+ def accum(key, value)
8
+ post(path: key, data: { "op" => "accum", "value" => value }.to_json)
9
+ end
10
+
11
+ def replace(key, value)
12
+ put(path: key, data: value.to_s)
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,7 @@
1
+ class Cog
2
+ class Services
3
+ class Metadata < Cog::Service
4
+ SERVICE_NAME = :meta
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,3 @@
1
+ class Cog
2
+ VERSION = "0.1.5"
3
+ end
data/lib/cog.rb ADDED
@@ -0,0 +1,19 @@
1
+
2
+ require_relative 'cog/bundle'
3
+ require_relative 'cog/config'
4
+ require_relative 'cog/command'
5
+ require_relative 'cog/request'
6
+ require_relative 'cog/response'
7
+ require_relative 'cog/version'
8
+
9
+ require_relative 'cog/service'
10
+ require_relative 'cog/services/memory'
11
+ require_relative 'cog/services/metadata'
12
+
13
+ class Cog
14
+ def self.bundle(name)
15
+ bundle = Cog::Bundle.new(name)
16
+ yield bundle if block_given?
17
+ bundle.run_command
18
+ end
19
+ end
metadata ADDED
@@ -0,0 +1,79 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cog-rb
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.5
5
+ platform: ruby
6
+ authors:
7
+ - Mark Imbriaco
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-06-10 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.11'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.11'
27
+ description:
28
+ email:
29
+ - mark@operable.io
30
+ executables:
31
+ - console
32
+ - setup
33
+ extensions: []
34
+ extra_rdoc_files: []
35
+ files:
36
+ - ".gitignore"
37
+ - CODE_OF_CONDUCT.md
38
+ - CONTRIBUTING.MD
39
+ - Gemfile
40
+ - LICENSE
41
+ - README.md
42
+ - Rakefile
43
+ - bin/console
44
+ - bin/setup
45
+ - cog-rb.gemspec
46
+ - lib/cog.rb
47
+ - lib/cog/bundle.rb
48
+ - lib/cog/command.rb
49
+ - lib/cog/config.rb
50
+ - lib/cog/request.rb
51
+ - lib/cog/response.rb
52
+ - lib/cog/service.rb
53
+ - lib/cog/services/memory.rb
54
+ - lib/cog/services/metadata.rb
55
+ - lib/cog/version.rb
56
+ homepage: https://github.com/cog-bundles/cog-rb
57
+ licenses: []
58
+ metadata: {}
59
+ post_install_message:
60
+ rdoc_options: []
61
+ require_paths:
62
+ - lib
63
+ required_ruby_version: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: '0'
68
+ required_rubygems_version: !ruby/object:Gem::Requirement
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ version: '0'
73
+ requirements: []
74
+ rubyforge_project:
75
+ rubygems_version: 2.4.5.1
76
+ signing_key:
77
+ specification_version: 4
78
+ summary: Cog command helper library
79
+ test_files: []