forked from slack-ruby/slack-bot-on-rails
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
163 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# @cjsx React.DOM | ||
|
||
@Message = React.createClass | ||
displayName: 'Message' | ||
render: -> | ||
# let's use this add-on to set the main div's class names | ||
cx = React.addons.classSet | ||
|
||
# here we use the calculated classes | ||
<div className="message"> | ||
{@props.data.id}: {@props.data.message?.text} | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
# @cjsx React.DOM | ||
|
||
@MessagesSection = React.createClass | ||
# Display name used for debugging | ||
displayName: 'MessagesSection' | ||
|
||
# Invoked before the component is mounted and provides the initial | ||
# state for the render method. | ||
getInitialState: -> | ||
# We'll change it to true once we fetch data | ||
didFetchData: false | ||
# The messages JSON array used to display the messages in the view | ||
messages: [] | ||
|
||
# Invoked right after the component renders | ||
componentDidMount: -> | ||
# Lets fetch all the messages... | ||
@_fetchMessages() | ||
setInterval(@_fetchMessages, 1000); | ||
|
||
# AJAX call to our MessagesController | ||
_fetchMessages: ()-> | ||
$.ajax | ||
url: Routes.messages_path() | ||
.done @_fetchDataDone | ||
.fail @_fetchDataFail | ||
|
||
# If the AJAX call is successful... | ||
_fetchDataDone: (data, textStatus, jqXHR) -> | ||
# We change the state of the component. This will cause the component and | ||
# it's children to render again | ||
@setState | ||
didFetchData: true | ||
messages: data | ||
|
||
# If errors in AJAX call... | ||
_fetchDataFail: (xhr, status, err) => | ||
console.error @props.url, status, err.toString() | ||
|
||
# How the component is going to be rendered to the user depending on it's | ||
# props and state... | ||
render: -> | ||
# The collection of Message components we are going to display | ||
# using the messages stored in the component's state | ||
messagesNode = @state.messages.map (message) -> | ||
# Message component with a data property containing all the JSON | ||
# attributes we are going to use to display it to the user | ||
<Message key={message.id} data={message}/> | ||
|
||
# HTML displayed if no messages found in it's state | ||
noDataNode = | ||
<div className="warning"> | ||
<h4>No messages found...</h4> | ||
</div> | ||
|
||
# Here starts the render result | ||
<div> | ||
<div className="messages-wrapper"> | ||
{ | ||
# If there are messages render the messages... | ||
if @state.messages.length > 0 | ||
messagesNode | ||
# If has fetched data and no messages found, render the | ||
# warning message instead | ||
else if @state.didFetchData | ||
noDataNode | ||
} | ||
</div> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
class HomeController < ApplicationController | ||
def index | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
class MessagesController < ApplicationController | ||
def index | ||
data = (1..10).map do |i| | ||
{ id: i, message: Rails.cache.read(i) } | ||
end | ||
render json: data | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
%section | ||
%header | ||
%h1 Tattletale | ||
|
||
= react_component 'MessagesSection', {}, :div |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,12 @@ | ||
class Say < SlackRubyBot::Commands::Base | ||
@id = 0 | ||
|
||
def self.next_id | ||
@id = @id % 10 + 1 | ||
end | ||
|
||
command 'say' do |client, data, match| | ||
Rails.cache.write next_id, { text: match['expression'] } | ||
send_message client, data.channel, match['expression'] | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,56 +1,4 @@ | ||
Rails.application.routes.draw do | ||
# The priority is based upon order of creation: first created -> highest priority. | ||
# See how all your routes lay out with "rake routes". | ||
|
||
# You can have the root of your site routed with "root" | ||
# root 'welcome#index' | ||
|
||
# Example of regular route: | ||
# get 'products/:id' => 'catalog#view' | ||
|
||
# Example of named route that can be invoked with purchase_url(id: product.id) | ||
# get 'products/:id/purchase' => 'catalog#purchase', as: :purchase | ||
|
||
# Example resource route (maps HTTP verbs to controller actions automatically): | ||
# resources :products | ||
|
||
# Example resource route with options: | ||
# resources :products do | ||
# member do | ||
# get 'short' | ||
# post 'toggle' | ||
# end | ||
# | ||
# collection do | ||
# get 'sold' | ||
# end | ||
# end | ||
|
||
# Example resource route with sub-resources: | ||
# resources :products do | ||
# resources :comments, :sales | ||
# resource :seller | ||
# end | ||
|
||
# Example resource route with more complex sub-resources: | ||
# resources :products do | ||
# resources :comments | ||
# resources :sales do | ||
# get 'recent', on: :collection | ||
# end | ||
# end | ||
|
||
# Example resource route with concerns: | ||
# concern :toggleable do | ||
# post 'toggle' | ||
# end | ||
# resources :posts, concerns: :toggleable | ||
# resources :photos, concerns: :toggleable | ||
|
||
# Example resource route within a namespace: | ||
# namespace :admin do | ||
# # Directs /admin/products/* to Admin::ProductsController | ||
# # (app/controllers/admin/products_controller.rb) | ||
# resources :products | ||
# end | ||
root 'home#index' | ||
resources :messages, only: :index | ||
end |