Consuming non-REST APIs in Rails with ActiveResource

前端 未结 3 535
眼角桃花
眼角桃花 2021-02-04 20:48

I\'m writing a client that consumes a non-REST API (i.e. GET site.com/gettreasurehunts), which requires that I specify all parameters (even the resource ID) in the request\'s HT

3条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-02-04 21:43

    Simple answer, don't. I had a similar problem with ActiveResource, didn't like HTTParty's api (too many class methods), so I rolled my own. Try it out, it's called Wrest. It has partial support for Curl and deserialisation via REXML, LibXML, Nokogiri and JDom out of the box. You can trivially write your own deserialiser too.

    Here's an example for the Delicious api:

    class Delicious
      def initialize(options)
        @uri = "https://api.del.icio.us/v1/posts".to_uri(options)
      end
    
      def bookmarks(parameters = {})
        @uri['/get'].get(parameters)
      end
    
      def recent(parameters = {})
        @uri['/recent'].get(parameters)
      end
    
      def bookmark(parameters)
        @uri['/add'].post_form(parameters)
      end
    
      def delete(parameters)
        @uri['/delete'].delete(parameters)
      end
    end
    
    account = Delicious.new :username => 'kaiwren', :password => 'fupupp1es'
    account.bookmark(
        :url => 'http://blog.sidu.in/search/label/ruby',
        :description => 'The Ruby related posts on my blog!',
        :extended => "All posts tagged with 'ruby'",
        :tags => 'ruby hacking'
      )
    

提交回复
热议问题