Class: Toolshed::Client

Inherits:
Hash
  • Object
show all
Includes:
Singleton
Defined in:
lib/toolshed/client.rb

Overview

This is responsible for loading .toolshedrc file

Constant Summary collapse

GITHUB_BASE_API_URL =
'https://api.github.com/'
PIVOTAL_TRACKER_BASE_API_URL =
'https://www.pivotaltracker.com/services/v5/'

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Hash

#to_ostruct

Constructor Details

#initializeClient

Returns a new instance of Client.



19
20
21
22
# File 'lib/toolshed/client.rb', line 19

def initialize
  load_toolshedrc_configuration
  @struct = to_ostruct
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(*args) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/toolshed/client.rb', line 30

def method_missing(*args)
  begin
    if args.first.to_s.end_with?('=')
      val = args.last
      my_h = self
      args.each_with_index do |arg, index|
        arg = arg.to_s.gsub('=', '')
        next_arg_val = args[index + 1].to_s.gsub('=', '')
        my_h = my_h[arg] unless next_arg_val == val.to_s
        my_h[arg] = val if next_arg_val == val.to_s && !my_h[arg].nil?
        my_h.merge!(arg => val) if next_arg_val == val.to_s && my_h[arg].nil?
      end
      @struct = to_ostruct
    else
      struct.send(args.first)
    end
  rescue NoMethodError => e
    Toolshed.die(e.message)
  end
end

Instance Attribute Details

#structObject (readonly)

Returns the value of attribute struct.



17
18
19
# File 'lib/toolshed/client.rb', line 17

def struct
  @struct
end

Instance Method Details

#load_toolshedrc_configurationObject



24
25
26
27
28
# File 'lib/toolshed/client.rb', line 24

def load_toolshedrc_configuration
  toolshedrc_configurations = YAML.load(ERB.new(File.read(toolshedrc_path)).result)
  raise CorruptFileException, 'Toolshedrc file is not configured properly.' unless toolshedrc_configurations.is_a?(Hash)
  self.merge!(toolshedrc_configurations)
end

#toolshedrc_pathObject



51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/toolshed/client.rb', line 51

def toolshedrc_path
  @toolshedrc_path ||= begin
    dir = Dir.pwd
    while File.expand_path(dir) != '/'
      unless File.exist?("#{dir}/.toolshedrc")
        dir = File.join dir, '..'
        next
      end
      credentials_loaded_from = "#{dir}/.toolshedrc"
      break
    end
    credentials_loaded_from
  end
end