Ruby on Sinatra Using The Twitter API
|
local variable
|
local_variable
|
|
instance variable
|
@instance_variable
|
|
class variable
|
@@class_variable
|
|
global variable
|
$global_variable
|
|
String
|
"Sequence of characters." or 'Sequence of characters.' |
|
Symbol (identifier)
|
:a_symbol, :another_symbol, :name |
|
Integer
|
42, 100, 1002, -256 |
|
Float (floating point)
|
1.1, 3.1415926, 5.5, -128.6 |
|
Numeric Methods
|
+, -, *, /, % |
|
Comparison Operators
|
==, !=, >, >=, <, <= |
|
Return Value
|
Last line of a ruby method/block/expression in IRB
|
|
Boolean
|
true or false |
nil |
counts as false in Ruby, everything counts as true
|
|
Array (0 indexed)
|
letters = ["a", "b", "c"] |
letters[2] |
#returns "c"
|
letters[1] = "monkey" |
["a", "monkey", "c"] |
|
ERB
|
Embedded Ruby for making dynamic HTML templates
|
erb :hello |
Sinatra looks in "views" folder for hello.erb file
|
|
Show @tweets
|
<% if @tweets %> <% @tweets.each do |tweet| %> <p><%= tweet %></p> <% end %> <% end %>
|
|
Instance Variables
|
Any instance variables you declare in your routes, will be accessible in these erb files
|
seen_it = "seen again" |
Save string to variable
|
num = 3 |
Save integer to variable
|
other_num = 5 |
Save another integer to another variable
|
answer = (num + other_num) * 6 |
Save result of adding/multiplying.
|
dog_speak = "woof!" |
puts "Dogs say #{dog_speak}" |
|
or
|
puts "Dogs say " + dog_speak |
|
if/else
|
if input == admin_password |
admin_logged_in = true |
|
else |
|
puts "Sorry, wrong password." |
|
end |
split |
Breaks a string into an array.
|
join |
Takes an array and joins into a string.
|
upcase |
Makes characters capital letters.
|
reverse |
Reverse characters in a string.
|
sample |
Randomly choose element.
|
map |
Do the same thing to every element.
|
|
Cmd+Click (Mac) or Ctrl+Click
|
Open links in new tabs.
|
|
ruby-doc.org ("The Ruby Docs")
|
Ruby coding best practices.
|
|
Stack Overflow
|
Oops. Research your error.
|
|
Guides and blog posts
|
Has someone done this before?
|
|
Sinatra
|
Framework for building a web server written in Ruby
|
|
Routing
|
Action taken in response to request
|
|
Path
|
text and slashes after domain
|
|
Sinatra looks for
get '/hello' |
|
get '/hello' do |
|
#code to return a response to the browser |
|
end |
|
|
String Response
|
get '/hello' |
"Hello, friendly web traveler!" |
|
end |
|
|
HTML File Response
|
get '/hello' do |
erb :hello |
|
end |
|
|
Get tweets route
|
get '/' do |
@tweets = $client.user_timeline.map { |tweet| tweet.text } |
|
erb :index |
|
end |
|
Redirect '/' |
Get server to run code in get '/' route
|
push |
Add to end of array:
letters << "elephant" or letters.push("elephant") |
<< |
Shovel operator. Push to end of array.
|
unshift |
Add element to beginning of array:
letters.unshift("zebra") |
pop |
Remove from end of array.
letters.pop |
shift |
Remove from start of array.
letters.shift |
.method(arguments) |
Call a method with arguments. Some methods require no arguments.
|
letters.pop |
Method without arguments.
|
|
Instantiate a Hash (hash rockets)
|
person = {:name => "Lauren", :age => 89, :sex => "female"} |
|
Instantiate a Hash (shortcut)
|
person = {name: "Lauren", age: 89, sex: "female"} |
|
Access value by key.
|
person[:name] # returns "Lauren" |
|
Update value by key.
|
person[:age] = 12 |
|
Insert key, value pair.
|
person[:gender] = "cis female" |
|
Remove value by key.
|
person.delete(:sex) # returns "female" |
|
Greeting Method Accepts Name
|
def create_greeting(name) |
return "Welcome, #{name}" |
|
end |
|
|
Call Greeting Method
|
create_greeting("Lauren") |
#returns 'Welcome, Lauren" |
|
|
Upcase On Call To Greeting Method
|
create_greeting("Lauren").upcase |
|
Arguments
|
Input given to methods.
|
|
Parameters
|
Define what can be passed to a method.
|
|
Return value
|
Output from a function
|
|
Implicit Return
|
Returning last line of method without explicit "return"
|
|
Ruby gems
|
Download, install, and use ruby software packages on your system
|
$ ruby -v |
Get Ruby Version
|
$ irb |
Load up interactive Ruby
|
$ gem install sinatra |
Install sinatra gem
|
$ gem install twitter |
Install twitter gem
|
$ ruby server.rb |
Tell Ruby interpreter to run code.
|
|
Ctrl+C
|
Stop server from running.
|
|
Server
|
has IP address, processes requests made by the (client) browser
|
|
HTTP
|
protocol enabling communication between clients and servers
|
|
HTTP Verbs
|
GET, POST, PUT, PATCH, DELETE |
|
GET
|
Request Tweets(data) from Twitter API
|
|
POST
|
Submit Tweet(data) to Twitter API
|
|
https:
|
scheme
|
|
twitter.com
|
domain
|
|
lauren_n_roth/status/699996428429561856
|
path
|
|
Params hash
|
params[:tweet] |
|
times
|
10.times do |
puts "Hello!" |
|
end |
|
|
times loop with counter
|
10.times do |counter| |
puts "#{counter}" |
|
end |
|
each |
letters = ["a", "b", "c"] |
letters.each do |letter| |
|
puts "Letter #{letter}" |
|
end |
|
map |
capital_letters = letters.map do |letter| |
letter.upcase |
|
end |
|
|
block style
|
letters.each { |letter| puts "Upcase #{letter.upcase}!" } |
|
elegant map method
|
capitals = letters.map { |letter| letter.upcase } |
|
Download files
|
bit.ly/dbc-twitter-sinatra
|
|
Sign Up To Use Twitter API
|
apps.twitter.com
|
|
Create New App
|
1. Create new App 2. Create Access Tokens 3. Ensure permissions are read/write
|
|
Set up Twitter $client global variable
|
require twitter |
$client = Twitter::REST::Client.new do |config| |
|
config.consumer_key = "YOUR_CONSUMER_KEY" config.consumer_secret = "YOUR_CONSUMER_SECRET" config.access_token = "YOUR_ACCESS_TOKEN" config.access_token_secret = "YOUR_ACCESS_SECRET" |
|
end |
|
|
Get user timeline
|
$client.user_timeline |
|
Send tweet
|
$client.update("omg I am tweeting from the command line. So cool!") |
No comments yet.
Cheatography n. The art of mapping complex subjects into clear, instantly navigable reference guides.