Sha256: 0778daef67b11ae80fced3631ff428713c78d04352f10b28e5b9755975ac96d5

Contents?: true

Size: 1.58 KB

Versions: 1

Compression:

Stored size: 1.58 KB

Contents

require 'json'
require 'httparty'

module Githubrepo

  # no need for classes (will never create instance of self)... refactor and use just modules and methods
  # http://matt.aimonetti.net/posts/2012/07/30/ruby-class-module-mixins/
  include HTTParty

  def self.create(attributes)

    post = HTTParty.post(
        'https://api.github.com/user/repos',

        :headers => {
            # When API Keys are supported we will want to pass them via the headers
            #'Authorization' => "token #{token}",

            'User-Agent' => 'Githubrepo',
            'Content-Type' => 'application/json',
            'Accept' => 'application/json'
        },

        :basic_auth => {
            :username => attributes[:username],
            :password => attributes[:password]
        },

        :body => {
            'name' => attributes[:repository]

            # feature coming at a future date
            #'description' => description
        }.to_json
    )

    Githubrepo.parse_response_from(post)
  end

  # DRY this by moving to a Parse.response_from(post)
  def self.parse_response_from(post)
    attributes = post

    clone_url =
        if attributes['clone_url'] != nil
          attributes['clone_url']
        end

    message =
        if attributes['message'] != nil
          attributes['message']
        end

    error_message =
        if attributes['errors'] != nil
          attributes['errors'].first['message']
        end

    # messages to console
    puts clone_url if clone_url
    puts message.capitalize if message
    puts error_message.capitalize if error_message
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
githubrepo-0.1.0 lib/githubrepo.rb