Sha256: 3ab1fdb9876b1e8eb060bc62dcc9244eb11500457d470e1c3df8c2c4c0a7936e

Contents?: true

Size: 1.24 KB

Versions: 5

Compression:

Stored size: 1.24 KB

Contents

module NestConnect
  class API
    class Authorize < API
      def initialize(auth_code:, client_id:, client_secret:, stdout: STDOUT)
        @auth_code = auth_code
        @client_id = client_id
        @client_secret = client_secret
        @stdout = stdout
      end

      def run
        response = connection.post do |request|
          request.url(url)
          request.headers.merge!(headers)
          request.body = body
        end

        if response.status == 200
          configuration.access_token = response.body['access_token']
        else
          stdout.write response.body
        end
      end

        private

          attr_reader :auth_code, :client_id, :client_secret, :stdout

          def url
            'oauth2/access_token'
          end

          def headers
            { 'Content-Type' => 'application/x-www-form-urlencoded' }
          end

          def body
            URI.encode_www_form(data)
          end

          def data
            {
              code: auth_code,
              client_id: client_id,
              client_secret: client_secret,
              grant_type: 'authorization_code'
            }
          end

          def api_endpoint
            'https://api.home.nest.com'
          end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
nest_connect-0.1.5 lib/nest_connect/api/authorize.rb
nest_connect-0.1.4 lib/nest_connect/api/authorize.rb
nest_connect-0.1.3 lib/nest_connect/api/authorize.rb
nest_connect-0.1.2 lib/nest_connect/api/authorize.rb
nest_connect-0.1.1 lib/nest_connect/api/authorize.rb