Class: Sem4r::Profile

Inherits:
Object
  • Object
show all
Defined in:
lib/sem4r/profile.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Attribute Details

#profileObject (readonly)

Returns the value of attribute profile.



29
30
31
# File 'lib/sem4r/profile.rb', line 29

def profile
  @profile
end

Class Method Details

.configure(config_file = nil, password_file = nil) ⇒ Object

force config paths and the config file



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/sem4r/profile.rb', line 112

def configure(config_file = nil, password_file = nil)
  config = Profile.search_config
  unless config_file.nil?
    config_file = File.expand_path(config_file)
    unless File.exists?(config_file)
      raise "#{config_file} not exists"
    end
    config.config_file = config_file
  end
  unless password_file.nil?
    password_file = File.expand_path(password_file)
    config.password_file = password_file
  end
  config
end

.load_config(config, profile) ⇒ Object

load the configuration



130
131
132
133
134
135
136
137
138
# File 'lib/sem4r/profile.rb', line 130

def load_config(config, profile)
  unless config.config_file
    raise Sem4rError, "config file 'sem4r' not found"
  end
  # puts "Loaded profiles from #{config_file}"
  yaml = YAML::load(File.open(config.config_file))
  config = yaml['google_adwords'][profile.to_s]
  config || {}
end

.profiles(profile_file = nil) ⇒ Object

Return list of profile contained into config file



92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/sem4r/profile.rb', line 92

def profiles(profile_file = nil)
  unless profile_file
    config = search_config
    unless config
      raise Sem4rError, "config file 'sem4r' not found"
    end
    profile_file = config.config_file
  end
  # puts "Loaded profiles from #{profile_file}"
  yaml = YAML::load(File.open(profile_file))
  profiles = yaml['google_adwords']
  profiles
end

.save_passwords(key, options, config) ⇒ Object



152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/sem4r/profile.rb', line 152

def save_passwords(key, options, config)
  if config.password_file.nil?
    raise "no password file defined"
  end
  # TODO: check permission password file
  puts "save password in #{config.password_file} (security warning!)"
  passwords = {}
  if File.exists?(config.password_file)
    passwords = YAML.load(File.open(config.password_file))
  end
  passwords[options["email"]] ||= {}
  passwords[options["email"]][key] = options[key]
  File.open(config.password_file, "w") do |f|
    f.write(passwords.to_yaml)
  end
end

.search_configOpenStruct

Search configuration file in standard locations

Returns:

  • (OpenStruct)

    with :config_dir :config_file :password_file



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/sem4r/profile.rb', line 41

def search_config
  config_filename = "sem4r.yml"
  password_filename = "sem4r_passwords.yml"

  #
  # try current directory
  #
  # return File.expand_path(config_filename) if File.exists?( config_filename)

  #
  # try ~/.sem4r/sem4r.yml
  #
  # require 'etc'
  # homedir = Etc.getpwuid.dir
  homedir = ENV['HOME']
  config_filepath = File.join(homedir, ".sem4r", config_filename)
  if File.exists?(config_filepath)
    return OpenStruct.new(
        :config_dir => File.join(homedir, ".sem4r"),
        :config_file => config_filepath,
        :password_file => File.join(homedir, ".sem4r", password_filename))
  end

  #
  # try <home_sem4r>/config/sem4r
  #
  config_filepath = File.expand_path(File.join(File.dirname(__FILE__), "..", "..", "config", config_filename))
  if File.exists?(config_filepath)
    return OpenStruct.new(
        :config_dir => nil,
        :config_file => config_filepath,
        :password_file => nil)
  end

  config_filepath = File.expand_path(File.join(File.dirname(__FILE__), "..", "..", "config", "sem4r.example.yml"))
  if File.exists?(config_filepath)
    return OpenStruct.new(
        :config_dir => nil,
        :config_file => config_filepath,
        :password_file => nil)
  end

  OpenStruct.new(
      :config_dir => nil,
      :config_file => nil,
      :password_file => nil)
end

.try_to_find_in_password_file(key, options, config) ⇒ Object



140
141
142
143
144
145
146
147
148
149
150
# File 'lib/sem4r/profile.rb', line 140

def try_to_find_in_password_file(key, options, config)
  return unless config.password_file
  return unless File.exists?(config.password_file)
  passwords = YAML.load(File.open(config.password_file))
  t = passwords[options["email"]]
  if t and t[key]
    options[key] = t[key]
  else
    nil
  end
end