lib/createsend/createsend.rb in createsend-3.0.0 vs lib/createsend/createsend.rb in createsend-3.1.0

- old
+ new

@@ -28,12 +28,18 @@ class Unauthorized < CreateSendError; end # Raised for HTTP response code of 404 class NotFound < ClientError; end # Raised for HTTP response code of 401, specifically when an OAuth token + # in invalid (Code: 120, Message: 'Invalid OAuth Token') + class InvalidOAuthToken < Unauthorized; end + # Raised for HTTP response code of 401, specifically when an OAuth token # has expired (Code: 121, Message: 'Expired OAuth Token') class ExpiredOAuthToken < Unauthorized; end + # Raised for HTTP response code of 401, specifically when an OAuth token + # has been revoked (Code: 122, Message: 'Revoked OAuth Token') + class RevokedOAuthToken < Unauthorized; end # Provides high level CreateSend functionality/data you'll probably need. class CreateSend include HTTParty attr_reader :auth_details @@ -47,11 +53,11 @@ qs << "&state=#{CGI.escape(state.to_s)}" if state "#{@@oauth_base_uri}?#{qs}" end # Exchange a provided OAuth code for an OAuth access token, 'expires in' - # value and refresh token. + # value, and refresh token. def self.exchange_token(client_id, client_secret, redirect_uri, code) body = "grant_type=authorization_code" body << "&client_id=#{CGI.escape(client_id.to_s)}" body << "&client_secret=#{CGI.escape(client_secret.to_s)}" body << "&redirect_uri=#{CGI.escape(redirect_uri.to_s)}" @@ -65,29 +71,31 @@ end r = Hashie::Mash.new(response) [r.access_token, r.expires_in, r.refresh_token] end + # Refresh an OAuth access token, given an OAuth refresh token. + # Returns a new access token, 'expires in' value, and refresh token. + def self.refresh_access_token(refresh_token) + options = { + :body => "grant_type=refresh_token&refresh_token=#{refresh_token}" } + response = HTTParty.post(@@oauth_token_uri, options) + if response.has_key? 'error' and response.has_key? 'error_description' + err = "Error refreshing access token: " + err << "#{response['error']} - #{response['error_description']}" + raise err + end + r = Hashie::Mash.new(response) + [r.access_token, r.expires_in, r.refresh_token] + end + def initialize(*args) if args.size > 0 auth args.first # Expect auth details as first argument end end - # Deals with an unfortunate situation where responses aren't valid json. - class Parser::DealWithCreateSendInvalidJson < HTTParty::Parser - # The createsend API returns an ID as a string when a 201 Created - # response is returned. Unfortunately this is invalid json. - def parse - begin - super - rescue MultiJson::DecodeError => e - body[1..-2] # Strip surrounding quotes and return as is. - end - end - end - parser Parser::DealWithCreateSendInvalidJson @@base_uri = "https://api.createsend.com/api/v3" @@oauth_base_uri = "https://api.createsend.com/oauth" @@oauth_token_uri = "#{@@oauth_base_uri}/token" headers({ 'User-Agent' => "createsend-ruby-#{VERSION}", @@ -106,18 +114,16 @@ not @auth_details.has_key? :refresh_token or not @auth_details[:refresh_token] raise '@auth_details[:refresh_token] does not contain a refresh token.' end - options = { - :body => "grant_type=refresh_token&refresh_token=#{@auth_details[:refresh_token]}" } - response = HTTParty.post(@@oauth_token_uri, options) - r = Hashie::Mash.new(response) + access_token, expires_in, refresh_token = + self.class.refresh_access_token @auth_details[:refresh_token] auth({ - :access_token => r.access_token, - :refresh_token => r.refresh_token}) - [r.access_token, r.expires_in, r.refresh_token] + :access_token => access_token, + :refresh_token => refresh_token}) + [access_token, expires_in, refresh_token] end # Gets your CreateSend API key, given your site url, username and password. def apikey(site_url, username, password) site_url = CGI.escape(site_url) @@ -225,13 +231,19 @@ case response.code when 400 raise BadRequest.new(Hashie::Mash.new response) when 401 data = Hashie::Mash.new(response) - if data.Code == 121 - raise ExpiredOAuthToken.new(data) + case data.Code + when 120 + raise InvalidOAuthToken.new data + when 121 + raise ExpiredOAuthToken.new data + when 122 + raise RevokedOAuthToken.new data + else + raise Unauthorized.new data end - raise Unauthorized.new(data) when 404 raise NotFound.new when 400...500 raise ClientError.new when 500...600 \ No newline at end of file