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

Classes: Config, Github

Constant Summary collapse

HOME_RC_FILE =
'~/.anything-hubrc'
VERSION =
"0.0.1"

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#optionsObject

Returns the value of attribute options.



10
11
12
# File 'lib/anything-hub.rb', line 10

def options
  @options
end

Instance Method Details

#app_dirObject



32
33
34
35
36
# File 'lib/anything-hub.rb', line 32

def app_dir
  dir = File.expand_path('~/.anything-hub')
  Dir.mkdir(dir) unless Dir.exists? dir
  @app_dir ||= dir
end

#cacheObject



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

#commandsObject



17
18
19
# File 'lib/anything-hub/command_set.rb', line 17

def commands
  @commands ||= []
end

#configObject



18
19
20
# File 'lib/anything-hub/config.rb', line 18

def config
  @config ||= AnythingHub::Config.new
end

#configure {|config| ... } ⇒ Object

Yields:



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 options[: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

#githubObject



23
24
25
# File 'lib/anything-hub/github.rb', line 23

def github
  @github ||= Github.new(config., token)
end

#initObject



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.options = opts

  if (results = command(input))
    cache.write(input, results.to_json)
    system("#{options[:s]} #{_anything_(results).gsub(/.*\[(.*)\]/, '\1')}")
  end
end

#tokenObject



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.}' \
      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