Class: PackageCloud::ConfigFile

Inherits:
Object
  • Object
show all
Defined in:
lib/package_cloud/config_file.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename = "~/.packagecloud", url = "https://packagecloud.io") ⇒ ConfigFile

Returns a new instance of ConfigFile.



10
11
12
13
14
# File 'lib/package_cloud/config_file.rb', line 10

def initialize(filename = "~/.packagecloud", url = "https://packagecloud.io")
  handle_locale
  @filename = File.expand_path(filename)
  @url = URI(url)
end

Instance Attribute Details

#tokenObject (readonly)

Returns the value of attribute token.



8
9
10
# File 'lib/package_cloud/config_file.rb', line 8

def token
  @token
end

Instance Method Details

#base_url(username = token, password = "") ⇒ Object



45
46
47
48
49
50
# File 'lib/package_cloud/config_file.rb', line 45

def base_url(username = token, password = "")
  u = url.dup
  u.user = CGI.escape(username)
  u.password = CGI.escape(password)
  u.to_s
end

#read_or_createObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/package_cloud/config_file.rb', line 16

def read_or_create
  if ENV["PACKAGECLOUD_TOKEN"]
    if ENV["PACKAGECLOUD_TOKEN"].length < 48
      puts "Found PACKAGECLOUD_TOKEN environment variable but is empty or too short! Visit https://packagecloud.io/api_token and confirm it is correct."
      exit!
    end
    @token = ENV["PACKAGECLOUD_TOKEN"]
    @url   = URI(ENV["PACKAGECLOUD_URL"]) if ENV["PACKAGECLOUD_URL"]
    output_host_and_token
  elsif File.exist?(@filename)
    attrs = JSON.parse(File.read(@filename))
    @token = attrs["token"] if attrs.has_key?("token")
    @url   = URI(attrs["url"]) if attrs.has_key?("url")
    fix_config_file!
    output_host_and_token
  else
    puts "No config file exists at #{@filename}. Login to create one."

    @token = 
    print "Got your token. Writing a config file to #{@filename}... "
    write
    puts "success!"
  end
end

#urlObject



41
42
43
# File 'lib/package_cloud/config_file.rb', line 41

def url
  @url ||= URI("https://packagecloud.io")
end