class HintMe::API::Client

Client for the DevHints API

Constants

RAW_REPO_URL

Base URL for the DevHints cheatsheets github repository

Public Instance Methods

search_cheatsheet_for_topic(topic) click to toggle source

searches and downloads the raw markdown representation of cheatsheet

@param topic [String] topic to fetch cheatsheet for

# File lib/hintme/api/client.rb, line 18
def search_cheatsheet_for_topic(topic)
  download_cheatsheet(topic) if cheatsheet_exists?(topic)
end

Private Instance Methods

build_topic_url(topic) click to toggle source

builds the full repository url for a specific cheatsheet

@param topic [String] topic for which the full cheatsheet url should be build

# File lib/hintme/api/client.rb, line 57
def build_topic_url(topic)
  URI("#{RAW_REPO_URL}#{topic}.md")
end
cheatsheet_exists?(topic) click to toggle source

checks if a cheatsheet for a certain topic exists

@param topic [String] @return [true, false]

# File lib/hintme/api/client.rb, line 29
def cheatsheet_exists?(topic)
  url = build_topic_url(topic)
  response = Net::HTTP.get_response(url)

  case response
  when Net::HTTPSuccess then true
  when Net::HTTPNotFound then false
  else
    handle_invalid_response(response)
  end
end
download_cheatsheet(topic) click to toggle source

downloads a cheatsheet for a given topic

@param topic [String] @return [String, nil] downloaded cheatsheet

# File lib/hintme/api/client.rb, line 46
def download_cheatsheet(topic)
  url = build_topic_url(topic)
  Net::HTTP.get(url)
           .force_encoding('utf-8')
end
handle_invalid_response(response) click to toggle source
# File lib/hintme/api/client.rb, line 61
def handle_invalid_response(response)
  raise StandartError, 'An network error occured.' \
    "Error response: #{response.value}"
end