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
# File 'lib/package_cloud/config_file.rb', line 10

def initialize(filename = "~/.packagecloud", url = "https://packagecloud.io")
  @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



32
33
34
35
36
37
# File 'lib/package_cloud/config_file.rb', line 32

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



15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/package_cloud/config_file.rb', line 15

def read_or_create
  if 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")
  else
    puts "No config file exists at #{@filename}. Login to create one."

    @token = 
    write
  end
end

#urlObject



28
29
30
# File 'lib/package_cloud/config_file.rb', line 28

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