Class: FBomb::Flowdock::Flow

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

Instance Method Summary collapse

Methods inherited from Object

#initialize, #to_s, #to_str

Constructor Details

This class inherits a constructor from FBomb::Flowdock::Object

Instance Method Details

#cacheObject



244
245
246
# File 'lib/fbomb/flowdock.rb', line 244

def cache
  Cache
end

#debug_stream(&block) ⇒ Object



200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
# File 'lib/fbomb/flowdock.rb', line 200

def debug_stream(&block)
  require "readline"

  while buf = Readline.readline("#{ organization }/#{ flow } > ", true)
    if buf.strip.downcase == 'exit'
      exit
    end

    flow = {
      :event       => 'message',
      :content     => buf,
      :tags        => [],
      :persist     => false,
      :id          => rand(99999),
      :uuid        => FBomb.uuid,
      :sent        => Time.now.to_i,
      :app         => 'fbomb',
      :attachments => [],
      :user        => rand(99999),
    }

    block.call(Map.for(flow)) if block
  end
end

#escape(string) ⇒ Object



69
70
71
# File 'lib/fbomb/flowdock.rb', line 69

def escape(string)
  string.to_s.gsub('+', '%2B')
end

#historyObject



225
226
227
# File 'lib/fbomb/flowdock.rb', line 225

def history
  @history ||= []
end

#leaveObject



131
132
133
# File 'lib/fbomb/flowdock.rb', line 131

def leave
  #say 'bai'
end

#paste(*args, &block) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/fbomb/flowdock.rb', line 96

def paste(*args, &block)
  options = Map.options_for!(args)
  tags = tags_for(options[:tags], options[:tag])

  content =
    case
      when args.size == 1
        if args.first.is_a?(String)
          args.first
        else
          Coerce.array(args.first).join("\n")
        end
      else
        Coerce.list_of_strings(args).join("\n")
    end

  msg = {:content => Util.indent(escape(content), 4), :tags => tags}

  if FBomb.debug
    puts("PASTE\n")
    puts(msg.to_yaml)
    puts
  else
    client.push_to_chat(msg)
  end
end

#speak(*args, &block) ⇒ Object Also known as: say



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/fbomb/flowdock.rb', line 77

def speak(*args, &block)
  options = Map.options_for!(args)
  tags = tags_for(options[:tags], options[:tag])

  content = escape(Coerce.list_of_strings(args).join(' '))

  msg = {:content => content, :tags => tags}

  if FBomb.debug
    puts("SPEAK\n")
    puts(msg.to_yaml)
    puts
  else
    client.push_to_chat(msg)
  end
end

#stream(&block) ⇒ Object

"tags"=>[],
"uuid"=>nil,
"persist"=>false,
"id"=>194342,
"flow"=>"c6dbc029-2173-4fb6-a423-32293c373106",
"content"=>{"last_activity"=>1399656686378,
"sent"=>1399657205286,
"app"=>nil,
"attachments"=>[],
"user"=>"76002"}

 "tags"=>[],
  "uuid"=>"Ry2GHegX445OAxV_",
   "id"=>2427,
    "flow"=>"affcb403-0c5f-4a2c-89a6-a809a887e281",
     "content"=>".peeps",
      "sent"=>1399838702954,
       "app"=>"chat",
        "attachments"=>[],
         "user"=>"77414"
         }


161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/fbomb/flowdock.rb', line 161

def stream(&block)
  return debug_stream(&block) if FBomb.debug

  http = EM::HttpRequest.new(
    "https://stream.flowdock.com/flows/#{ organization }/#{ flow }",
    :keepalive => true, :connect_timeout => 0, :inactivity_timeout => 0)

  EventMachine.run do
    s = http.get(:head => { 'Authorization' => [token, ''], 'accept' => 'application/json'})

    buffer = ""
    s.stream do |chunk|
      buffer << chunk
      while line = buffer.slice!(/.+\r\n/)
        begin
          flow = JSON.parse(line)

          unless flow['event'] == 'activity.user'
            history.push(flow)

            while history.size > 1024
              history.shift
            end
          end

          if flow['external_user_name'] == client.external_user_name
            next
          end

          block.call(Map.for(flow)) if block
        rescue Object => e
          warn("#{ e.message }(#{ e.class })#{ Array(e.backtrace).join(10.chr) }")
          # FIXME 
        end
      end
    end
  end
end

#tags_for(*tags) ⇒ Object



73
74
75
# File 'lib/fbomb/flowdock.rb', line 73

def tags_for(*tags)
  Coerce.list_of_strings(*tags).map{|tag| "##{ tag }".gsub(/^[#]+/, '#')}
end

#upload(*args, &block) ⇒ Object

Raises:

  • (NotImplementedError)


123
124
125
# File 'lib/fbomb/flowdock.rb', line 123

def upload(*args, &block)
  raise NotImplementedError
end

#user_for(id) ⇒ Object



240
241
242
# File 'lib/fbomb/flowdock.rb', line 240

def user_for(id)
  users.detect{|user| user.id.to_s == id.to_s || user.email.to_s == id.to_s}
end

#usersObject



127
128
129
# File 'lib/fbomb/flowdock.rb', line 127

def users(*args, &block)
  []
end

#yell(*args) ⇒ Object



63
64
65
66
67
# File 'lib/fbomb/flowdock.rb', line 63

def yell(*args)
  string = args.join(' ')
  prefix = ["FUCKING", "OH YEAH!?", "SRSLY"].sort_by{ rand }.first
  paste([prefix, string].join('  '))
end