Module: AnythingHub
- Extended by:
- AnythingHub
- Included in:
- AnythingHub
- Defined in:
- lib/anything-hub.rb,
lib/anything-hub/config.rb,
lib/anything-hub/github.rb,
lib/anything-hub/version.rb,
lib/anything-hub/command_set.rb
Defined Under Namespace
Constant Summary collapse
- HOME_RC_FILE =
'~/.anything-hubrc'
- VERSION =
"0.0.1"
Instance Attribute Summary collapse
-
#options ⇒ Object
Returns the value of attribute options.
Instance Method Summary collapse
- #app_dir ⇒ Object
- #cache ⇒ Object
- #command(name, &block) ⇒ Object
- #command_set(&block) ⇒ Object
- #commands ⇒ Object
- #config ⇒ Object
- #configure {|config| ... } ⇒ Object
- #fetch_from_api_or_cache(cache_key, &api) ⇒ Object
- #github ⇒ Object
- #init ⇒ Object
- #run(input, opts = {}) ⇒ Object
- #token ⇒ Object
Instance Attribute Details
#options ⇒ Object
Returns the value of attribute options.
10 11 12 |
# File 'lib/anything-hub.rb', line 10 def @options end |
Instance Method Details
#app_dir ⇒ Object
32 33 34 35 36 |
# File 'lib/anything-hub.rb', line 32 def app_dir dir = File.('~/.anything-hub') Dir.mkdir(dir) unless Dir.exists? dir @app_dir ||= dir end |
#cache ⇒ Object
51 52 53 |
# File 'lib/anything-hub.rb', line 51 def cache @cache ||= ActiveSupport::Cache::FileStore.new(File.join(app_dir, 'cache')) end |
#command(name, &block) ⇒ Object
8 9 10 11 12 13 14 15 |
# File 'lib/anything-hub/command_set.rb', line 8 def command(name, &block) if block commands << {:pattern => /^#{name}(:.+)?/, :block => block} else cmd = commands.detect { |c| c[:pattern] =~ name } cmd[:block].call(name) end end |
#command_set(&block) ⇒ Object
4 5 6 |
# File 'lib/anything-hub/command_set.rb', line 4 def command_set(&block) class_eval(&block) end |
#commands ⇒ Object
17 18 19 |
# File 'lib/anything-hub/command_set.rb', line 17 def commands @commands ||= [] end |
#config ⇒ Object
18 19 20 |
# File 'lib/anything-hub/config.rb', line 18 def config @config ||= AnythingHub::Config.new end |
#configure {|config| ... } ⇒ Object
14 15 16 |
# File 'lib/anything-hub/config.rb', line 14 def configure yield config end |
#fetch_from_api_or_cache(cache_key, &api) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/anything-hub/command_set.rb', line 21 def fetch_from_api_or_cache(cache_key, &api) unless [:cache] == "no" cache_data = JSON.parse(cache.read(cache_key)) rescue nil end if cache_data cache_data else response = api.call cache.write cache_key, response.to_json response end end |
#github ⇒ Object
23 24 25 |
# File 'lib/anything-hub/github.rb', line 23 def github @github ||= Github.new(config.login, token) end |
#init ⇒ Object
24 25 26 27 28 29 30 |
# File 'lib/anything-hub.rb', line 24 def init require "anything-hub/config" load HOME_RC_FILE require "anything-hub/github" require "anything-hub/command_set" require "anything-hub/commands" end |
#run(input, opts = {}) ⇒ Object
14 15 16 17 18 19 20 21 22 |
# File 'lib/anything-hub.rb', line 14 def run(input, opts = {}) init self. = opts if (results = command(input)) cache.write(input, results.to_json) system("#{[:s]} #{_anything_(results).gsub(/.*\[(.*)\]/, '\1')}") end end |
#token ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/anything-hub.rb', line 38 def token unless (_token = cache.read('authorizations:token') || config.token) res = `curl \ --data '{"scopes":["repo"]}' \ --request POST -u '#{config.login}' \ https://api.github.com/authorizations` _token = (JSON.parse(res) rescue nil).try(:[], 'token') raise StandardError, 'invalid password' if _token.blank? cache.write 'authorizations:token', _token end @token ||= _token end |