Module: Murmurs

Includes:
Git
Defined in:
lib/murmurs.rb,
lib/murmurs/git.rb

Defined Under Namespace

Modules: Git Classes: Error, HookExistsError, InvalidMurmursURLError, UnexpectedResponseError

Instance Method Summary collapse

Methods included from Git

#git_commits, #git_hooks_dir, #install_git_hook

Instance Method Details

#extract_project_info(url) ⇒ Object



32
33
34
35
36
37
38
# File 'lib/murmurs.rb', line 32

def extract_project_info(url)
  if url =~ /\/projects\/([^\/]+)\//
    "#{$1}@#{URI(url).host}"
  else
    url
  end
end

#http_post(url, body, options = {}) ⇒ Object



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
# File 'lib/murmurs.rb', line 56

def http_post(url, body, options={})
  uri = URI.parse(url)
  http = Net::HTTP.new(uri.host, uri.port)
  if uri.scheme == 'https'
    http.use_ssl = true
    if options[:skip_ssl_verify]
      http.verify_mode = OpenSSL::SSL::VERIFY_NONE
    end
  end

  request = Net::HTTP::Post.new(uri.request_uri)
  request.body = body

  request['Content-Type'] = 'application/json'
  request['Content-Length'] = body.bytesize


  if options[:access_key_id]
    ApiAuth.sign!(request, options[:access_key_id], options[:access_secret_key])
  end

  response = http.request(request)

  if response.code.to_i > 300
    raise UnexpectedResponseError, <<-ERROR
\nRequest URL: #{url}
Response: #{response.code} #{response.message}
Response Headers: #{response.to_hash.inspect}\nResponse Body: #{response.body}"
ERROR
  end
end

#log(level, m) ⇒ Object



50
51
52
53
54
# File 'lib/murmurs.rb', line 50

def log(level, m)
  if level == :info
    puts m
  end
end

#murmur(url, msg, options = {}) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/murmurs.rb', line 15

def murmur(url, msg, options={})
  if msg.nil? || msg.empty?
    log(options[:log_level], "Nothing to murmur.")
    return
  end

  log(options[:log_level], "murmur => #{extract_project_info(url)}")
  if options[:git]
    Array(git_commits(msg, options[:git_branch])).each do |msg|
      murmur(url, msg, options.merge(:git => false, :log_level => :error))
    end
  else
    validate_murmurs_url!(url)
    http_post(url, "{\"murmur\":{\"body\":#{msg.inspect}}}", options)
  end
end

#truncate(m, t = 15) ⇒ Object



46
47
48
# File 'lib/murmurs.rb', line 46

def truncate(m, t=15)
  m.size > t ? "#{m[0..t]}..." : m
end

#validate_murmurs_url!(url) ⇒ Object



40
41
42
43
44
# File 'lib/murmurs.rb', line 40

def validate_murmurs_url!(url)
  if url.to_s !~ /\Ahttps?\:\/\/.+\/murmurs/
    raise InvalidMurmursURLError, "Invalid murmurs URL: #{url.inspect}"
  end
end