I am new to ruby and rails and i need to create an sms application that uses a rest api. (own api and not something like clickatell or so which have gems). I've gone through a lot of material and having confused myself entirely am posting on this forum. 开发者_运维技巧Basically i need to be able to enter a number and type a message and click on send and the message should go to the number. I have an API key. and the API Url. I am quite confused about where to write the post methods etc/where to do routing - which im guessing in config and other details. Any help or direction would be appreciated.
require 'open-uri'
class SmsMessage
attr_reader :url, :number, :message, request_id
API_ID = 'xxxxxxx'
API_USERNAME = 'xxxxxx'
API_PASSWORD = 'xxxxxx'
def initialize(number, message)
@number = number
@message = message
end
def deliver
@url = "http://api.clickatell.com/http/sendmsg?user=#{API_USERNAME}&password=#{API_PASSWORD}&api_id=#{API_ID}&to=#{@number}&text=#{CGI::escape(@message)}"
@request_id = open(@url)
end
def self.deliver(phone, msg)
SmsMessage.new(phone, msg).deliver
end
end
Still you need to learn some basic rails tutorials.
Learn rails first, and then do that application.
Don't try to do both at the same time.
I am quite confused about where to write the post methods etc/where to do routing - which im guessing in config and other details. Any help or direction would be appreciated.
So routing details go in config/routing, and here's a wonderful guide on Rails routing.
Rails is an MVC architecture and if you look in the app/ directory you'll see subdirectories for each of these things that are called "models", "controllers" and "views" appropriately.
Your RESTful interfaces will live in the controllers code and here's a fantastic guide on Rails controllers
As everyone else has said you really, really must study the guides and tutorials that are easily found on the web. I'd strongly suggest you build a toy application with the simplest behavior in order to learn.
All of the Rails guides found at the linked site are really great places to start. I'd suggest focusing on the MVC entries at the top of the list, then dig into the others as you need them.
FINALLY, please don't take this the wrong way but you'll really want to do your homework more carefully before asking questions here--this is a fantastic resource and generally you'll get a rapid and useful response--but people here get rather prickly if you don't come to the table with basic understanding of what you need and how things work.
Good luck and I hope this helps...
精彩评论