Created
November 1, 2013 18:07
-
-
Save revans/7269361 to your computer and use it in GitHub Desktop.
Get the latest releae
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'net/https' | |
require 'json' | |
require 'uri' | |
module MightyOctocat | |
class Releases | |
attr_reader :owner, :repo | |
# ==== Arguments | |
# | |
# * owner - String: the repo owner name | |
# * repo - String: the name of the repo | |
# | |
# ==== Returns | |
# | |
# * Hash - contains all the information for the latest release | |
# | |
def self.latest_for(owner, repo) | |
new(owner, repo).get_latest_release | |
end | |
# ==== Arguments | |
# | |
# * owner - String: the repo owner name | |
# * repo - String: the name of the repo | |
# | |
# ==== Returns | |
# | |
# * String - URL of the latest tarball | |
# | |
def self.latest_tarball(owner, repo) | |
new(owner, repo).get_latest_tarball | |
end | |
# | |
# ==== Arguments | |
# | |
# * owner - String: the repo owner name | |
# * repo - String: the name of the repo | |
# | |
def initialize(owner, repo) | |
@owner, @repo = owner, repo | |
end | |
# Gets the latest tarball | |
# | |
# ==== Returns | |
# | |
# * String that represents the URL of the tarball | |
# | |
def get_latest_tarball | |
get_releases.first["tarball_url"] | |
end | |
# Gets the latest release | |
# | |
# ==== Returns | |
# | |
# * Hash - contains all the information for the latest release | |
# | |
def get_latest_release | |
get_releases.first | |
end | |
# Gets the all the releases | |
# | |
# ==== Returns | |
# | |
# * Array of Hashes containing all the releases for a given repo | |
# | |
def get_all_releases | |
get_releases | |
end | |
private | |
def get_releases | |
parse(send_request.body) | |
end | |
def uri | |
@uri ||= URI.parse("https://api.github.com/repos/#{owner}/#{repo}/releases") | |
end | |
def parse(body) | |
::JSON.parse(body) | |
end | |
def send_request | |
@send_request ||= http.request(request) | |
end | |
def http | |
@http ||= begin | |
http = ::Net::HTTP.new(uri.host, uri.port) | |
http.use_ssl = true | |
http.verify_mode = ::OpenSSL::SSL::VERIFY_PEER | |
http | |
end | |
end | |
def request | |
@request ||= begin | |
request = ::Net::HTTP::Get.new(uri.path) | |
request["User-Agent"] = "The Mighty Octocat" | |
request["Accept"] = "application/vnd.github.manifold-preview" | |
request | |
end | |
end | |
end | |
end | |
## Usage | |
# | |
# Using class methods: | |
# | |
# latest_release = MightyOctocat::Releases.latest_for('twbs', 'bootstrap') | |
# puts latest_release.inspect | |
# | |
# latest_tarball = MightyOctocat::Releases.latest_tarball('twbs', 'bootstrap') | |
# puts latest_tarball | |
# | |
# Using instance methods | |
# | |
# releases = MightyOctocat::Releases.new('twbs', 'bootstrap') | |
# releases.get_all_releases | |
# releases.get_latest_release | |
# releases.get_latest_tarball |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment