Ruby Writing API Program

Source: Internet
Author: User

In fact, Ruby Write API program is very simple, if the API itself is relatively standard, rather than a mess.

class apidemo    #  Defining Interfaces   API_QERUY = [       :userAdd               #  User profile Add   ]    #  implement Ghost Method   def method_missing (method_name ,  *args, &block)       API_QERUY.each do |v|         if v.to_s == method_name.to_s           return api_request (V.to_s, args.first)          end      end      super   end    #  Returns the result parsing, here returns the XML, therefore uses Nokogiri this gem to parse the XML, is very convenient.   def api_request (OPERATION, ARGV)       query =  ""       argv.each do |a|        query +=  "&"  +  a.first.to_s +  "="  + a.last.to_s      end       uri = uri.escape ("version=1&type=xml&key=" +api_key + query)        @doc  = nokogiri::html (open (api_url + operation +  uri)       puts  @doc. to_s if rails.env.development? #   Development mode output results for easy commissioning       self    end  #  Get returned code  def code      if  @doc           @doc. At ("code") .content.to_i      end     end  #  determine if the return result is successful   def success?      if   @doc     &Nbsp;   self.code == 200      end     endend


How do I call it?

APIDEMO.NEW.USERADD (username: ' Moli '). Success?

If you want to add more APIs, you can

Api_qeruy

can be added in


Ruby Writing API Program

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.