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



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

def cache
  Cache
end

#debug_stream(&block) ⇒ Object



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

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



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

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

#historyObject



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

def history
  @history ||= []
end

#leaveObject



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

def leave
  #say 'bai'
end

#paste(*args, &block) ⇒ Object



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

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



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

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"
         }


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

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



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

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

#upload(*args, &block) ⇒ Object

Raises:

  • (NotImplementedError)


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

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

#user_for(id) ⇒ Object



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

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

#usersObject



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

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

#yell(*args) ⇒ Object



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

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