Sha256: 58a9fd20c35c5df749faa42111e5506e83027ab4f6ba6f6a32c72a4b6104d729

Contents?: true

Size: 1.24 KB

Versions: 2

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

2 entries across 2 versions & 1 rubygems

Version Path
nest_connect-0.1.7 lib/nest_connect/api/authorize.rb
nest_connect-0.1.6 lib/nest_connect/api/authorize.rb