Class: Flowdock::Flow

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/flowdock.rb

Defined Under Namespace

Classes: ApiError, InvalidParameterError

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Flow

Required options keys: :api_token, :source, :from => { :name, :address }



13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/flowdock.rb', line 13

def initialize(options = {})
  @api_token = options[:api_token]
  raise InvalidParameterError, "Flow must have :api_token attribute" if blank?(@api_token)
  
  @source = options[:source]
  raise InvalidParameterError, "Flow must have valid :source attribute, only alphanumeric characters and underscores can be used" if blank?(@source) || !@source.match(/^[a-z0-9\-_ ]+$/i)

  @project = options[:project]
  raise InvalidParameterError, "Optional attribute :project can only contain alphanumeric characters and underscores" if !blank?(@project) && !@project.match(/^[a-z0-9\-_ ]+$/i)

  @from = options[:from] || {}
end

Instance Method Details

#send_message(params) ⇒ Object



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

def send_message(params)
  raise InvalidParameterError, "Message must have both :subject and :content" if blank?(params[:subject]) || blank?(params[:content])
  
  from = (params[:from].kind_of?(Hash)) ? params[:from] : @from
  raise InvalidParameterError, "Flow's :from attribute must have :address attribute" if blank?(from[:address])

  tags = (params[:tags].kind_of?(Array)) ? params[:tags] : []
  tags.reject! { |tag| !tag.kind_of?(String) || blank?(tag) }
  
  link = (!blank?(params[:link])) ? params[:link] : nil

  params = {
    :source => @source,
    :format => 'html', # currently only supported format
    :from_name => from[:name],
    :from_address => from[:address],
    :subject => params[:subject],
    :content => params[:content],
  }
  params[:tags] = tags.join(",") if tags.size > 0
  params[:project] = @project unless blank?(@project)
  params[:link] = link unless blank?(link)

  # Send the request
  resp = self.class.post(get_flowdock_api_url, :body => params)
  raise ApiError, (resp.code == 500 ? "Flowdock API returned error: #{resp.body}" : "HTTP Error #{resp.code}") unless resp.code == 200
  true
end