class TdBerlinTest::StackexchangeApiFetcher

Constants

DefaultQueryURI

Attributes

language_data[RW]
result[RW]

Public Class Methods

new(yaml_file) click to toggle source
# File lib/stackoverflow_api_fetcher.rb, line 12
def initialize(yaml_file)
  @language_data = parse_yaml_file(yaml_file)
end

Public Instance Methods

fetch_stackoverflow(language) click to toggle source
# File lib/stackoverflow_api_fetcher.rb, line 32
def fetch_stackoverflow(language)
  url = DefaultQueryURI + "?order=desc&sort=popular&inname=#{language}&site=stackoverflow"
  open(url).read
end
get_tag_usage_count_from_json(json, language) click to toggle source
# File lib/stackoverflow_api_fetcher.rb, line 37
def get_tag_usage_count_from_json(json, language)
  parsed = JSON.parse(json)

  parsed['items'].each do |tag|
    if tag['name'] == language
      return tag['count']
    end
  end
end
output_result(hash) click to toggle source
# File lib/stackoverflow_api_fetcher.rb, line 51
def output_result(hash)
  ap @result, :indent => -2
end
parse_yaml_file(yaml_file) click to toggle source
# File lib/stackoverflow_api_fetcher.rb, line 16
def parse_yaml_file(yaml_file)
  YAML.load_file(yaml_file)
end
run() click to toggle source
# File lib/stackoverflow_api_fetcher.rb, line 20
def run
  result_hash = {}
  @language_data[0]['langs'].each do |language|
    json = fetch_stackoverflow(language)
    tag_usage_count = get_tag_usage_count_from_json(json, language)
    result_hash[language] = tag_usage_count
  end

  @result = sort_result_by_tag_usage_count(result_hash)
  output_result(@result)
end
sort_result_by_tag_usage_count(hash) click to toggle source
# File lib/stackoverflow_api_fetcher.rb, line 47
def sort_result_by_tag_usage_count(hash)
  temp = hash.sort_by { |language, tag_usage_count| tag_usage_count }.reverse.to_h
end