Module: Voog::Dtk

Defined in:
lib/voog/dtk/watcher.rb,
lib/voog/dtk.rb,
lib/voog/dtk/version.rb,
lib/voog/dtk/notifier.rb,
lib/voog/dtk/filemanager.rb

Overview

def run_on_modifications(paths)

    @filemanager.upload_files paths
    @filemanager.notifier.newline
  rescue => e
    @filemanager.notifier.newline
    Voog::Dtk.handle_exception e, @debug, @filemanager.notifier
  end
end

end

Defined Under Namespace

Classes: FileManager, Notifier, Watcher

Constant Summary collapse

CONFIG_FILENAME =
'.voog'
VERSION =
'0.5.9'

Class Method Summary collapse

Class Method Details

.config_exists?(filename = CONFIG_FILENAME) ⇒ Boolean

Returns:

  • (Boolean)


11
12
13
# File 'lib/voog/dtk.rb', line 11

def config_exists?(filename=CONFIG_FILENAME)
  filename && !filename.empty? && File.exist?(filename)
end

.global_config_exists?(filename = CONFIG_FILENAME) ⇒ Boolean

Returns:

  • (Boolean)


15
16
17
# File 'lib/voog/dtk.rb', line 15

def global_config_exists?(filename=CONFIG_FILENAME)
  filename && !filename.empty? && File.exist?([ENV['HOME'], filename].join('/'))
end

.handle_exception(exception, debug, notifier = nil) ⇒ Object



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/voog/dtk.rb', line 133

def handle_exception(exception, debug, notifier=nil)
  error_msg = if is_api_error?(exception)
    if exception.respond_to?(:response) && exception.response
      if exception.response.fetch(:headers, {}).fetch(:content_type, '') =~ /application\/json/
        body = JSON.parse(exception.response.fetch(:body))
        "#{body.fetch('message', '')} #{("Errors: " + body.fetch('errors', '').inspect) if body.fetch('errors', nil)}".red
      else
        exception.response.fetch(:body)
      end + "(error code #{exception.response[:status]})".red
    else
      "#{exception}"
    end
  else
    "#{exception}"
  end

  if notifier
    notifier.newline
    notifier.error error_msg
    notifier.newline
  else
    puts error_msg
  end
  print_debug_info(exception) if debug
rescue => e
  handle_exception e, debug, notifier
end

.is_api_error?(exception) ⇒ Boolean

Returns:

  • (Boolean)


114
115
116
117
118
119
120
121
122
# File 'lib/voog/dtk.rb', line 114

def is_api_error?(exception)
  [
    Faraday::ClientError,
    Faraday::ConnectionFailed,
    Faraday::ParsingError,
    Faraday::TimeoutError,
    Faraday::ResourceNotFound
  ].include? exception.class
end


124
125
126
127
128
129
130
131
# File 'lib/voog/dtk.rb', line 124

def print_debug_info(exception)
  puts
  puts "Exception: #{exception.class}"
  if is_api_error?(exception) && exception.respond_to?(:response) && exception.response
    pp exception.response
  end
  puts exception.backtrace
end

.read_config(block = nil, file = CONFIG_FILENAME) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/voog/dtk.rb', line 19

def read_config(block = nil, file = CONFIG_FILENAME)
  config = {
    :host => nil,
    :api_token => nil,
    :overwrite => nil,
    :protocol => 'http'
  }
  local_config = config_exists?(file) ? ParseConfig.new(File.expand_path(file)).params : {}
  global_config = global_config_exists?(file) ? ParseConfig.new(File.expand_path([ENV['HOME'], file].join('/'))).params : {}

  options = local_config.merge(global_config)

  unless options.empty?
    @block = case block
    when nil
      options.keys.first
    when :all
      options.keys
    else
      if options.key?(block)
        block
      else
        fail "Site '#{block}' not found in the configuration file!".red
      end
    end

    if @block.is_a?(Array) && @block.size
      config = []
      @block.each do |site|
        config << {
          name: site,
          host: options[site].fetch('host'),
          api_token: options[site].fetch('api_token'),
          overwrite: options[site].fetch('overwrite'),
          protocol: options[site].fetch('protocol')
        }
      end
    else
      config[:host] = options[@block].fetch("host")
      config[:api_token] = options[@block].fetch("api_token")
      config[:overwrite] = options[@block].fetch("overwrite", false) == 'true' ? true : false
      config[:protocol] = options[@block].fetch("protocol", 'http') == "https" ? "https" : "http"
    end
  end
  config
end

.write_config(opts) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/voog/dtk.rb', line 66

def write_config(opts)
  block = opts.fetch(:block, '')
  host = opts.fetch(:host, '')
  api_token = opts.fetch(:api_token, '')
  silent = opts.fetch(:silent, false)
  overwrite = opts.fetch(:overwrite, '')
  protocol = opts.fetch(:protocol, '')

  @file = if config_exists?
    CONFIG_FILENAME
  elsif global_config_exists?
    [ENV['HOME'], CONFIG_FILENAME].join('/')
  else
    File.new(CONFIG_FILENAME, 'w+')
    CONFIG_FILENAME
  end

  options = ParseConfig.new(File.expand_path(@file))

  if options.params.key?(block)
    puts "Writing new configuration options to existing config block.".white unless silent
    options.params[block]['host'] = host unless host.empty?
    options.params[block]['api_token'] = api_token unless api_token.empty?
    options.params[block]['overwrite'] = overwrite if (overwrite == true || overwrite == false)
    options.params[block]['protocol'] = (protocol == 'https' ? 'https' : 'http') unless protocol.empty?
  else
    puts "Writing configuration options to new config block.".white unless silent
    options.params[block] = {
      'host' => host,
      'api_token' => api_token,
      'overwrite' => (overwrite == true ? true : false),
      'protocol' => (protocol == 'https' ? 'https' : 'http')
    }
  end

  File.open(@file, 'w+') do |file|
    file.truncate(0)
    file << "\n"
    options.params.each do |param|
      file << "[#{param[0]}]\n"
      param[1].keys.each do |key|
        file << "  #{key}=#{param[1][key]}\n"
      end
      file << "\n"
    end
  end
end