Class: Repos::StackOverflow

Inherits:
Object
  • Object
show all
Defined in:
lib/repocrawler/crawler.rb

Instance Method Summary collapse

Constructor Details

#initialize(gem_name, stackoverflow_token) ⇒ StackOverflow

Returns a new instance of StackOverflow.



378
379
380
# File 'lib/repocrawler/crawler.rb', line 378

def initialize(gem_name, stackoverflow_token)
  @STACKOVERFLOW_API = "https://api.stackexchange.com/2.2/search/advanced?order=desc&sort=creation&q=#{gem_name}&site=stackoverflow&key=#{stackoverflow_token}"
end

Instance Method Details

#get_questionsObject

get questions from stackexchange



383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
# File 'lib/repocrawler/crawler.rb', line 383

def get_questions

  stop_words = []
  File.open(File.expand_path("../../public/stop_words.txt",  File.dirname(__FILE__)), "r") do |f|
    f.each_line do |line|
      stop_words << line.gsub(/\n/,"")
    end
  end

  questions = []
  fetch_questions = HTTParty.get(@STACKOVERFLOW_API)
  fetch_questions['items'].each do |q|
    #don't store stop words
    good_words = []
    q['title'].split(' ').map do |word|
      if !stop_words.include?(word.downcase)
        good_words << word
      end
    end

    questions << {
      'creation_date' => q['creation_date'],
      'title' => good_words,
      'views' => q['view_count']
    }
  end

  questions_word_count = Hash.new(0)
  questions.each do |question|
    question['title'].each do |word|
      questions_word_count[word] += 1
    end
  end

  questions_word_count = questions_word_count.sort_by { |word, freq| freq }.reverse!
  [questions, questions_word_count]
end