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



236
237
238
# File 'lib/fbomb/flowdock.rb', line 236

def cache
  Cache
end

#debug_stream(&block) ⇒ Object



192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'lib/fbomb/flowdock.rb', line 192

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



61
62
63
# File 'lib/fbomb/flowdock.rb', line 61

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

#historyObject



217
218
219
# File 'lib/fbomb/flowdock.rb', line 217

def history
  @history ||= []
end

#leaveObject



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

def leave
  say 'bai'
end

#paste(*args, &block) ⇒ 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/fbomb/flowdock.rb', line 88

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



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/fbomb/flowdock.rb', line 69

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


153
154
155
156
157
158
159
160
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
# File 'lib/fbomb/flowdock.rb', line 153

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



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

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

#upload(*args, &block) ⇒ Object

Raises:

  • (NotImplementedError)


115
116
117
# File 'lib/fbomb/flowdock.rb', line 115

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

#user_for(id) ⇒ Object



232
233
234
# File 'lib/fbomb/flowdock.rb', line 232

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

#usersObject



119
120
121
# File 'lib/fbomb/flowdock.rb', line 119

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