public
Description: Standard authentication stack for Rails using Twitter to log in.
Homepage:
Clone URL: git://github.com/mbleigh/twitter-auth.git
Click here to lend your support to: twitter-auth and make a donation at www.pledgie.com !
100644 41 lines (37 sloc) 1.147 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
module TwitterAuth
  module Dispatcher
    module Shared
      def post!(status)
        self.post('/statuses/update.json', :status => status)
      end
 
      def append_extension_to(path)
        path, query_string = *(path.split("?"))
        path << '.json' unless path.match(/\.(:?xml|json)\z/i)
        "#{path}#{"?#{query_string}" if query_string}"
      end
 
      def handle_response(response)
        case response
        when Net::HTTPOK
          begin
            JSON.parse(response.body)
          rescue JSON::ParserError
            response.body
          end
        when Net::HTTPUnauthorized
          raise TwitterAuth::Dispatcher::Unauthorized, 'The credentials provided did not authorize the user.'
        else
          message = begin
            JSON.parse(response.body)['error']
          rescue JSON::ParserError
            if match = response.body.match(/<error>(.*)<\/error>/)
              match[1]
            else
              'An error occurred processing your Twitter request.'
            end
          end
 
          raise TwitterAuth::Dispatcher::Error, message
        end
      end
    end
  end
end