How to pull a Rails 3 Twitter feed without a gem - ruby-on-rails-3

So I am ruby/rails novice and have been looking at: https://dev.twitter.com/docs/api/1.1/get/statuses/user_timeline This is the link to the twitter docs for using the 1.1 api. I am clear that I need a twitter app which I have setup, and that I need to set global vars for the key and secret, but then what.
Should I create a controller method in my Pages_controller (the model for static pages)?
How should I code this method to grab my global vars and then create an instance var that includes my tweets?
thanks for the help

You say no gem but much easier if you just use the gem Twitter, put the config in a module if you want. It will look something like this
twitter_client = Twitter::Client.new(
:consumer_key => ENV["CONSUMER_KEY"],
:consumer_secret => ENV["CONSUMER_SECRET"],
:oauth_token => ENV["OAUTH_TOKEN"],
:oauth_token_secret => ENV["OAUTH_SECRET"],
)
In your controller you can then retrieve the tweets with something like
twitter_client.user_timeline('twitter_handle').map(&:attrs)
Any further help just ask

Related

How do I connect to the Bigcommerce API and then process the data using PHP

I am new to how APIs work, I would like to get a specific product from my BigCommerce store using the BigCommerce API. I have read the API documentation, googled quite alot, but to be honest, I am still a little confused. How do I create a connection to the BC API and once I create the connection how to I get the product I want from my store. I really appreciate any guidance on this.
You must first download this and unpack it on your server (local or web): https://github.com/bigcommerce/bigcommerce-api-php
Once inside the "src" folder, you can create a PHP file that will both authorize your connection and run your script.
require "vendor/autoload.php";
use Bigcommerce\Api\Client as Bigcommerce;
Bigcommerce::configure(array(
'store_url' => 'STORE_URL_HERE',
'username' => 'USERNAME_IN_BACKEND',
'api_key' => 'API_KEY'
));
Bigcommerce::verifyPeer(false);
Create some sort of check that you connected, I think BigCommerce uses a "getTimeStamp" as an example to output if you've correctly connected or not.
That should help you at least connect using PHP, let me know if you need more help.

Creating and authenticating user on post ("sign up"). Warden + Sinatra

TL;DR: How can I create a sign up feature with sinatra and warden?
I am trying to make a simple authentication system for a Sinatra app, and I found that warden is probably the best bet. I have found numerous examples on how to use it. I started working from the examples by sklise.
I quickly run into the problem of signing up. See that it is possible to create a new user with something like
post '/auth/signup' do
u = User.new(:username => params[:username], :password => params[:password])
u.save
But then what? How can I authenticate / sign this brand new user in? I cannot seem to find any single reference to how a sign up feature should be built with sinatra + warden. As a matter of fact, I can't seem to find anything for warden at all. None of the examples on the Warden wiki has a sign up feature. Do anyone have a solution for this?
Thanks
Not exactly a warden aficionado, but this looks like where you want to look:
https://github.com/hassox/warden/blob/906edf86c6c31be917a921097031b89361d022e8/lib/warden/strategies/base.rb#L116
From your example I think you'd want to do something like this:
post '/auth/signup' do
u = User.new(:username => params[:username], :password => params[:password])
u.save
env['warden'].success!(u)
...

How do I utilize user input without putting info into a Model?

This is an incredibly newbish question, but I can't seem to find the answer.
I'm building an app that utilizes external APIs heavily, and I'm fairly new to Rails, so it's still a little rough to get around. I can't, for the life of me, figure out how to accept user input and execute a function in my app without writing to a model.
For example, I just want to let a user type in a Twitter username and have it display on the page. I know how to make a form to cache the search in a model, but I can't figure out how to just... make a function happen on a page. I've been breaking my brains on this for several days now.
Please help? :/
You don't need a model to use Rails, but if you don't need ActiveRecord at all, you might benefit from a lighter framework like Sinatra. That doesn't answer your question, but it's worth thinking about if you really have no database requirement for your application.
It sounds like you're just trying to access non-resourceful user input, which is accessible in the controller via the params hash. So, assuming you have set up a valid route for the form action, you use your controller to extract GET or POST parameters.
For example:
# You define a non-resourceful route in routes.rb for your form action.
get 'twitternames/show'
# Form action directs user to GET the following route after filling in the form.
http://example.com/twitternames/show?user=foo
# The controller action extracts the data.
def show
#user = params[:user]
# render the view unless you tell rails to do something else
end
# show.html.erb
<%= #user %>
Creating the right route is the key. Once you've defined a route that can break a URL into the proper segments, the rest will fall into place.

Hide id from url in rails

I am new to rails. I have developed an application on rails recently. The application is pretty big and it's running fine. Currently i have url like this.
http://192.168.99.220/user/13/domainUsers
I want it to be like the below one (without any id)
http://192.168.99.220/user/domainUsers
My routes are like this.
match 'user/:id/domainUsers', :to => 'domains#manageDomain_2', :as => :manageDomain2
I have tried to rewrite the url using "to_param". As my application is too big and it has lots of functionalities, i am using parameters other than the "id" to find users informations frequently, so i am not being able to use the "to_param" method. Is there any other way to hide "id" from url.
Please help
Thanks in advance.
The easiest way to do this is with a gem called friendly_id.
Here is a tutorial that explains it quite well:
http://railscasts.com/episodes/314-pretty-urls-with-friendlyid

How to make Rails 3 DELETE resource action accept POST

I'm running into a dilemma here. The Firefox Add-Ons SDK only allows PUT and POST requests via its API. I am using the following route in Rails: "resource :users"
I can easily override the PUT to be:
"match 'users/:id' => 'users#update', :via => [:post, :put]
However, I can't do this again for DELETE as I also need to :post to the same URL again. Do I need to create a new route all-together and abandon the resourceful way of doing things? Is there a way to have POST behave like a DELETE on the Firefox side instead?
There is currently no way to do this in the high-level Request api, however you could implement something using the lower level xhr api:
https://addons.mozilla.org/en-US/developers/docs/sdk/1.2/packages/api-utils/docs/xhr.html
This provides access to Firefox's underlying XmlHttpRequest implementation:
https://developer.mozilla.org/En/Using_XMLHttpRequest