btoone
4/3/2012 - 1:54 AM

Prototype for a gist library using httparty

Prototype for a gist library using httparty

:username: caspyin
:password: PASSWD
{
  "description": "the description for this gist",
  "public": true,
  "files": {
    "file1.txt": {
      "content": "String file contents"
    }
  }
}
require 'httparty'
require 'pp'
require 'json'

config = YAML.load_file('credentials.yml')


class GitHub
  include HTTParty
  base_uri 'https://api.github.com'
  
  def initialize(u, p)
    @auth = {username: u, password: p}
  end
  
  def users(options={})
    options.merge!({basic_auth: @auth})
    self.class.get('/users/caspyin', options)
  end
  
  def gists(options={})
    options.merge!({basic_auth: @auth})
    self.class.get('/gists', options)
  end
  
  
  def starred_gists(options={})
    options.merge!({basic_auth: @auth})
    self.class.get('/gists/starred', options)
  end
end

gist = {
  "description" => "Test",
  "public" => false,
  "files" => {
    "file1.txt" => {
      "content" => "API 3:34 PM"
    }
  }
}

options = {
  basic_auth: {
    username: config[:username],
    password: config[:password]
  },
  body: JSON.generate(gist)
}

github = GitHub.new(config[:username], config[:password])
puts "User Info"
pp github.users
puts "Gists"
pp github.gists
puts "Starred Gists"
pp github.starred_gists

# pp HTTParty.post('https://api.github.com/gists', options)

# cli
# httparty --user 'user':'passwd' "https://api.github.com/users/caspyin"
# httparty -v --action post --user "caspyin":"PASSWD" --header "ContentType:application/json" --data "{\"description\":\"Test\",\"public\":false,\"files\":{\"file1.txt\":{\"content\":\"Post via cmd line\"}}}" "https://api.github.com/gists"

# pp HTTParty.get('https://api.github.com/users/caspyin', {basic_auth:{username:config[:username], password:config[:password]}})
# response = HTTParty.get('https://api.github.com/users/caspyin', options)