Module: Litc

Defined in:
lib/litc.rb

Constant Summary collapse

ITEMS =

These are the items (for method missing)…

‘ami-id’ => :ami_id ‘block-device-mapping/ami’ => :block_device_mapping_ami

%w(
  ami-id
  ami-launch-index
  ami-manifest-path
  block-device-mapping/
  block-device-mapping/ami
  block-device-mapping/ephemeral0
  block-device-mapping/swap
  block-device-mapping/root
  hostname
  instance-id
  instance-type
  local-hostname
  local-ipv4
  placement/
  placement/availability-zone
  public-hostname
  public-ipv4
  public-keys/
  reservation-id
  security-groups
  user-data
)
API_VERSION =

Version of the API to use, any one of:

1.0
2007-01-19
2007-03-01
2009-08-15
latest
"latest"

Class Method Summary collapse

Class Method Details

.get(item) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/litc.rb', line 53

def self.get(item)
  item = urlize(item)

  if item != 'user-data'
    item = "meta-data/#{item}"
  end

  response = Net::HTTP.get_response(URI.parse("http://169.254.169.254/#{API_VERSION}/#{item}"))
  
  case response
  when Net::HTTPSuccess then response.body.strip
  else nil
  end
end

.method_missing(meth, *args, &block) ⇒ Object



68
69
70
71
72
73
74
# File 'lib/litc.rb', line 68

def self.method_missing(meth, *args, &block)
  if ITEMS.include?(urlize(meth))
    get(meth)
  else
    raise(NoMethodError, "undefined method '#{meth}' for Litc")      
  end
end

.urlize(item) ⇒ Object



42
43
44
45
46
47
48
49
50
51
# File 'lib/litc.rb', line 42

def self.urlize item
  item = item.to_s.gsub(/_/, '-')
  if item =~ /(placement).+/ or item =~ /(block-device-mapping).+/
    item.gsub!(/(#{$1})-(.*)$/,'\\1/\\2')
  end
  if item =~ /block-device-mapping$/ or item =~ /placement$/ or item =~ /public-keys$/
    item << "/"
  end
  item
end