how to make rails 3 do something in some time - ruby-on-rails-3

I am using the gem: scottmotte / digitalocean
which seams to be the best. Please inform me on this.
I can create a server using
Digitalocean::Droplet.create({:name => droplet_name, :size_id => size_id, :image_id => image_id, :region_id => region_id)
But I would like to check after some time if everything is working fine since creating a droplet takes at least 1 minute.
How can I ask rails to do an API call in some given time, and then, doing something depending on the result? Is there any convention and pattern?
thank you very much,

What you need is a scheduler for your tasks. Use something like resque for this.

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.

How to pull a Rails 3 Twitter feed without a gem

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

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

rails3, params[:id] encryption

I trying to prevent url hacking, I passing an id to the url that the forms need, it works fine but if the user changes that value on the url it will send values to the wrong table.
<%= link_to '+ New Event',
{:controller =>'events', :action =>
'new', :company_id => company.id} %>
On the php world I used to encrypt that id ...how can I do this on rails3 or is there a better way ??
needless to say I sort of new to rails and I know a little bit of php
any help or suggestions will be greatly appreciated.
Even though this is an older question, it's a very worthwhile question. It is absolutely worthwhile to conceal the ID in the URL for, among other things, prevention of information disclosure.
For example, an application has a robust security model allowing users to only view resources to which they have rights. However, why should a user be able to look at the value of the ID in the URL and use it to deduce how many resources there are or, as the original questioner suggests, start trying to poke around with forced browsing.
The solution to this in rails turns out to be pretty simple. What I find works best is overriding to_param in the models, usually via a module in the lib directory and a before_filter in the application controller that decrypts the IDs.
For a walkthrough, have a look at this: http://www.youtube.com/watch?v=UW_s9ejrCsI
Rather than trying to encrypt or hide your company.id value, ask yourself what exactly it is that you want to prevent users from doing.
If you just want to prevent users from creating events associated with non-existant companies (by setting the id to a really high value for instance), then a simple
validates_presence_of :company
On the Event model would be fine.
If you only want users to be able to create events associated with companies that they work for, or have access for in some way, then you should create custom validations to verify that.
F

How can I prevent access by a certain IP in Rails?

I would like to shut down people running screen scrapers against our site and mining the data. Or at least slow them down big time.
My idea is to write every IP address into a memory object and count how many requests they make per minute, then put them in some "no access" list if they exceed some number that I set.
I'm just looking for some community validation on whether this is a sound approach for a Rails application. Thanks for any help you can offer.
I'm not sure it's a good idea to protect your site from these IPs at the application level. I would personally investigate if that would be possible to do that at the network level, for example in your firewall / router. If you have a Cisco router, check out the "rate-limit" command.
I don't know guys, I came up with a pretty nice way to do this in the app. I just want to punish bad behavior in one app anyway. What I ended up doing was:
def log_ip
# Initialize
if IPLOG.include?(request.ip)
IPLOG[request.ip][:count]+=1
else
IPLOG[request.ip] = {:time => Time.now, :count => 1}
end
# Reset the time if necessary
IPLOG[request.ip][:time] = Time.now if IPLOG[request.ip][:time] < 1.minute.ago
if IPLOG[request.ip][:count] > REQUESTS_PER_MINUTE_UNTIL_BLACKLIST
Blacklist.create(:ip_address => request.ip, :count => IPLOG[request.ip][:count])
end
if Blacklist.where(:ip_address => request.ip).first
IPLOG.delete(request.ip)
redirect_to blocked_path
end
end
I'm sure I can tighten this up so I'm not doing the db hit every time, but it appears to be working pretty well. It caught the GoogleBot last night. Plus, there's opportunity for whitelisting IP addresses in case a bunch of people are coming in through a known proxy.