Sha256: b2b10d445971f6a6697c8aae113dcb9f2e4165e8b34ac57848e06df75524f94d

Contents?: true

Size: 1.31 KB

Versions: 1

Compression:

Stored size: 1.31 KB

Contents

require 'net/http'

module Stocktwits
  module PlainUser
    def self.included(base)
      base.class_eval do

      end

      base.extend Stocktwits::PlainUser::ClassMethods
    end

    module ClassMethods
      def verify_credentials(login, password)
      
        response =  Stocktwits.net.start { |http|
                  request = Net::HTTP::Get.new(Stocktwits.base_url + "/users/show/#{login}.json")
                  http.request(request)
                }

        if response.code == '200'
          JSON.parse(response.body)
        else
          false
        end
      end

      def authenticate(login, password = nil)
        if stocktwits_hash = verify_credentials(login, password)
          user = identify_or_create_from_stocktwits_hash_and_password(stocktwits_hash, password)
          user
        else
          nil
        end
      end
      
      def identify_or_create_from_stocktwits_hash_and_password(stocktwits_hash, password)
        if user = User.find_by_stocktwits_id(stocktwits_hash['id'].to_s)
          user.login = stocktwits_hash['login']
          user.assign_stocktwits_attributes(stocktwits_hash)
          user.save
          user
        else
          user = User.new_from_stocktwits_hash(stocktwits_hash)
          user.save
          user
        end
      end
      
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
stocktwits-1.0.0 app/models/stocktwits/plain_user.rb