Class: Flowdock::Flow

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helpers

#blank?, #handle_response

Constructor Details

#initialize(options = {}) ⇒ Flow

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



35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/flowdock.rb', line 35

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.



31
32
33
# File 'lib/flowdock.rb', line 31

def api_token
  @api_token
end

#external_user_nameObject (readonly)

Returns the value of attribute external_user_name.



31
32
33
# File 'lib/flowdock.rb', line 31

def external_user_name
  @external_user_name
end

#fromObject (readonly)

Returns the value of attribute from.



31
32
33
# File 'lib/flowdock.rb', line 31

def from
  @from
end

#projectObject (readonly)

Returns the value of attribute project.



31
32
33
# File 'lib/flowdock.rb', line 31

def project
  @project
end

#sourceObject (readonly)

Returns the value of attribute source.



31
32
33
# File 'lib/flowdock.rb', line 31

def source
  @source
end

Instance Method Details

#push_to_chat(params) ⇒ Object



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

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) }
  thread_id = params[:thread_id]
  message_id = params[:message_id] || params[:message]

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

  # 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



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

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.



116
117
118
119
# File 'lib/flowdock.rb', line 116

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