lib/base/api_client/client_secret.rb in base-api-client-0.2.3.beta vs lib/base/api_client/client_secret.rb in base-api-client-0.2.4.beta

- old
+ new

@@ -21,25 +21,29 @@ def initialize(args = nil) opts = args || load_file @client_id ||= opts['client_id'] @client_secret ||= opts['client_secret'] @code ||= opts['code'] + @expires_at ||= opts['expires_at'] @redirect_uri ||= opts['redirect_uri'] + @access_token ||= opts['access_token'] + @refresh_token ||= opts['refresh_token'] @search_client_id ||= opts['search_client_id'] @search_client_secret ||= opts['search_client_secret'] end def update!(args) - self.client_id = args[:client_id] if args[:client_id] - self.client_secret = args[:client_secret] if args[:client_secret] - self.code = args[:code] if args[:code] - self.expires_at = args[:expires_at] if args[:expires_at] - self.redirect_uri = args[:redirect_uri] if args[:redirect_uri] - self.access_token = args[:access_token] if args[:access_token] - self.refresh_token = args[:refresh_token] if args[:refresh_token] - self.search_client_id = args[:search_client_id] if args[:search_client_id] - self.search_client_secret = args[:search_client_secret] if args[:search_client_secret] + opts = Base::Normalization.recursive_hash_normalize_keys(args) + self.client_id = opts[:client_id] if opts[:client_id] + self.client_secret = opts[:client_secret] if opts[:client_secret] + self.code = opts[:code] if opts[:code] + self.expires_at = opts[:expires_at] if opts[:expires_at] + self.redirect_uri = opts[:redirect_uri] if opts[:redirect_uri] + self.access_token = opts[:access_token] if opts[:access_token] + self.refresh_token = opts[:refresh_token] if opts[:refresh_token] + self.search_client_id = opts[:search_client_id] if opts[:search_client_id] + self.search_client_secret = opts[:search_client_secret] if opts[:search_client_secret] end def to_json MultiJson.dump(to_hash) end @@ -79,20 +83,20 @@ uri.hostname, uri.port, use_ssl: uri.scheme == 'https') { |http| http.request(request) } end - def generate_code - system(command, [AUTH_URI, generate_authorize_parameters].join) + def generate_code_uri + [AUTH_URI, generate_authorize_parameters].join end def set_tokens! response = fetch_token @access_token = response['access_token'] @refresh_token = response['refresh_token'] @code = nil - @expires_at = to_expires_at(response['expires_in']) + send :expires_at= to_hash end private @@ -124,18 +128,10 @@ .inject([]) do |memo, arr| memo << arr.join('=') end.join('&') end - def command - if OS.linux? - 'sensible-browser' - elsif OS.mac? - 'open' - end - end - def grant_type @grant_type = if code && redirect_uri 'authorization_code' elsif refresh_token @@ -149,11 +145,11 @@ response = Net::HTTP.post_form(uri, request_parameters) JSON.parse response.body end - def to_expires_at(expires_in) - Time.now + expires_in + def expires_at=(expires_at = nil) + @expires_at = expires_at || Time.now + 3600 end end end end