Sha256: 05866162a070f8eaf730bd7807b24193bc992ea22a32691003c874ecfc71cec1

Contents?: true

Size: 1.26 KB

Versions: 1

Compression:

Stored size: 1.26 KB

Contents

require 'oj'

module LearnWeb
  class Client
    class Me
      attr_accessor :response, :id, :first_name, :last_name, :full_name,
                    :username, :email, :github_gravatar, :github_uid, :data,
                    :silent_output

      def initialize(response, silent_output: false)
        @response      = response
        @silent_output = silent_output

        parse!
      end

      def parse!
        if response.status == 200
          self.data = Oj.load(response.body, symbol_keys: true)

          populate_attributes!
        elsif silent_output == false
          case response.status
          when 401
            puts "It seems your OAuth token is incorrect. Please re-run config with: learn reset"
            exit
          when 500
            puts "Something went wrong. Please try again."
            exit
          else
            puts "Something went wrong. Please try again."
            exit
          end
        end

        self
      end

      private

      def populate_attributes!
        data.each do |attribute, value|
          if !self.respond_to?(attribute)
            class << self
              attr_accessor attribute
            end
          end

          self.send("#{attribute}=", value)
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
learn-web-0.0.12 lib/learn_web/client/me.rb