|
| 1 | +class AttendEase |
| 2 | + class Client |
| 3 | + # TODO add access_token=() and request_token=() methods that check whether the tokens are usable |
| 4 | + |
| 5 | + attr_reader :access_token, :request_token, :consumer, :format |
| 6 | + |
| 7 | + def initialize(options = {}) |
| 8 | + options = { |
| 9 | + :debug => false, |
| 10 | + :format => AttendEase::FORMAT_XML |
| 11 | + }.merge(options) |
| 12 | + |
| 13 | + # symbolize keys |
| 14 | + options.map do |k,v| |
| 15 | + options[k.to_sym] = v |
| 16 | + end |
| 17 | + raise AttendEase::ArgumentError, "OAuth Consumer Key and Secret required" if options[:consumer_key].nil? || options[:consumer_secret].nil? |
| 18 | + @consumer = OAuth::Consumer.new(options[:consumer_key], options[:consumer_secret], :site => AttendEase::API_SERVER, :authorize_url => AttendEase::AUTHORIZATION_URL) |
| 19 | + @debug = options[:debug] |
| 20 | + @format = options[:format] |
| 21 | + @app_id = options[:app_id] |
| 22 | + if options[:access_token] && options[:access_token_secret] |
| 23 | + @access_token = OAuth::AccessToken.new(@consumer, options[:access_token], options[:access_token_secret]) |
| 24 | + else |
| 25 | + @access_token = nil |
| 26 | + end |
| 27 | + if options[:request_token] && options[:request_token_secret] |
| 28 | + @request_token = OAuth::RequestToken.new(@consumer, options[:request_token], options[:request_token_secret]) |
| 29 | + else |
| 30 | + @request_token = nil |
| 31 | + end |
| 32 | + end |
| 33 | + |
| 34 | + # Obtain an <strong>new</strong> unauthorized OAuth Request token |
| 35 | + def get_request_token(force_token_regeneration = false) |
| 36 | + if force_token_regeneration || @request_token.nil? |
| 37 | + @request_token = consumer.get_request_token |
| 38 | + end |
| 39 | + @request_token |
| 40 | + end |
| 41 | + |
| 42 | + # Return the Fire Eagle authorization URL for your mobile application. At this URL, the User will be prompted for their request_token. |
| 43 | + def mobile_authorization_url |
| 44 | + raise AttendEase::ArgumentError, ":app_id required" if @app_id.nil? |
| 45 | + "#{AttendEase::MOBILE_AUTH_URL}#{@app_id}" |
| 46 | + end |
| 47 | + |
| 48 | + # The URL the user must access to authorize this token. get_request_token must be called first. For use by web-based and desktop-based applications. |
| 49 | + def authorization_url |
| 50 | + raise AttendEase::ArgumentError, "call #get_request_token first" if @request_token.nil? |
| 51 | + request_token.authorize_url |
| 52 | + end |
| 53 | + |
| 54 | + #Exchange an authorized OAuth Request token for an access token. For use by desktop-based and mobile applications. |
| 55 | + def convert_to_access_token |
| 56 | + raise AttendEase::ArgumentError, "call #get_request_token and have user authorize the token first" if @request_token.nil? |
| 57 | + @access_token = request_token.get_access_token |
| 58 | + end |
| 59 | + |
| 60 | + |
| 61 | + |
| 62 | + def events(params) |
| 63 | + raise AttendEase::ArgumentError, "OAuth Access Token Required" unless @access_token |
| 64 | + response = get(AttendEase::EVENTS_API_PATH + ".#{format}", :params => params) |
| 65 | + AttendEase::Response.new(response.body) |
| 66 | + end |
| 67 | + |
| 68 | + |
| 69 | + protected |
| 70 | + |
| 71 | + def get(url, options = {}) #:nodoc: |
| 72 | + request(:get, url, options) |
| 73 | + end |
| 74 | + |
| 75 | + def post(url, options = {}) #:nodoc: |
| 76 | + request(:post, url, options) |
| 77 | + end |
| 78 | + |
| 79 | + def request(method, url, options) #:nodoc: |
| 80 | + response = case method |
| 81 | + when :post |
| 82 | + access_token.request(:post, url, options[:params]) |
| 83 | + when :get |
| 84 | + qs = options[:params].collect { |k,v| "#{CGI.escape(k.to_s)}=#{CGI.escape(v.to_s)}" }.join("&") if options[:params] |
| 85 | + access_token.request(:get, "#{url}?#{qs}") |
| 86 | + else |
| 87 | + raise ArgumentError, "method #{method} not supported" |
| 88 | + end |
| 89 | + |
| 90 | + case response.code |
| 91 | + when '500'; then raise AttendEase::AttendEaseException, "Internal Server Error" |
| 92 | + when '400'; then raise AttendEase::AttendEaseException, "Method Not Implemented Yet" |
| 93 | + else response |
| 94 | + end |
| 95 | + end |
| 96 | + end |
| 97 | +end |
0 commit comments