Class: Flowdock::Flow

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

Defined Under Namespace

Classes: ApiError, InvalidParameterError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Flow

Required options keys: :api_token Optional keys: :external_user_name, :source, :project, :from => { :name, :address }, :reply_to



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

def initialize(options = {})
  @api_token = if options[:api_token].kind_of?(Array)
    options[:api_token].join(",")
  else
    options[:api_token].to_s
  end
  raise InvalidParameterError, "Flow must have :api_token attribute" if blank?(@api_token)

  @source = options[:source] || nil
  @project = options[:project] || nil
  @from = options[:from] || {}
  @reply_to = options[:reply_to] || nil
  @external_user_name = options[:external_user_name] || nil
end

Instance Attribute Details

#api_tokenObject (readonly)

Returns the value of attribute api_token.



13
14
15
# File 'lib/flowdock.rb', line 13

def api_token
  @api_token
end

#external_user_nameObject (readonly)

Returns the value of attribute external_user_name.



13
14
15
# File 'lib/flowdock.rb', line 13

def external_user_name
  @external_user_name
end

#fromObject (readonly)

Returns the value of attribute from.



13
14
15
# File 'lib/flowdock.rb', line 13

def from
  @from
end

#projectObject (readonly)

Returns the value of attribute project.



13
14
15
# File 'lib/flowdock.rb', line 13

def project
  @project
end

#sourceObject (readonly)

Returns the value of attribute source.



13
14
15
# File 'lib/flowdock.rb', line 13

def source
  @source
end

Instance Method Details

#push_to_chat(params) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/flowdock.rb', line 70

def push_to_chat(params)
  raise InvalidParameterError, "Message must have :content" if blank?(params[:content])

  @external_user_name = params[:external_user_name] unless blank?(params[:external_user_name])
  if blank?(@external_user_name) || @external_user_name.match(/^[\S]+$/).nil? || @external_user_name.length > 16
    raise InvalidParameterError, "Message must have :external_user_name that has no whitespace and maximum of 16 characters"
  end

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

  params = {
    :content => params[:content],
    :external_user_name => @external_user_name
  }
  params[:tags] = tags.join(",") if tags.size > 0

  # Send the request
  resp = self.class.post(get_flowdock_api_url("messages/chat"), :body => params)
  handle_response(resp)
  true
end

#push_to_team_inbox(params) ⇒ Object



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
65
66
67
68
# File 'lib/flowdock.rb', line 32

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

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

  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, "Message's :from attribute must have :address attribute" if blank?(from[:address])

  reply_to = (!blank?(params[:reply_to])) ? params[:reply_to] : @reply_to

  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_address => from[:address],
    :subject => params[:subject],
    :content => params[:content],
  }
  params[:from_name] = from[:name] unless blank?(from[:name])
  params[:reply_to] = reply_to unless blank?(reply_to)
  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("messages/team_inbox"), :body => params)
  handle_response(resp)
  true
end

#send_message(params) ⇒ Object

DEPRECATED: Please use useful instead.



94
95
96
97
# File 'lib/flowdock.rb', line 94

def send_message(params)
  warn "[DEPRECATION] `send_message` is deprecated.  Please use `push_to_team_inbox` instead."
  push_to_team_inbox(params)
end