Permalink
Browse files

init

  • Loading branch information...
0 parents commit 1320dac1e88298f7e80e203c2618571258b062e4 rochefort committed Jan 29, 2012
Showing with 208 additions and 0 deletions.
  1. +18 −0 .gitignore
  2. +4 −0 Gemfile
  3. +22 −0 LICENSE
  4. +29 −0 README.md
  5. +2 −0 Rakefile
  6. +4 −0 bin/gist
  7. +16 −0 gist.gemspec
  8. +5 −0 lib/gist.rb
  9. +84 −0 lib/gist/main.rb
  10. +21 −0 lib/gist/my_util.rb
  11. +3 −0 lib/gist/version.rb
@@ -0,0 +1,18 @@
+*.gem
+*.rbc
+.bundle
+.config
+.yardoc
+.DS_Store
+Gemfile.lock
+InstalledFiles
+_yardoc
+coverage
+doc/
+lib/bundler/man
+pkg
+rdoc
+spec/reports
+test/tmp
+test/version_tmp
+tmp
@@ -0,0 +1,4 @@
+source 'https://rubygems.org'
+
+# Specify your gem's dependencies in gist.gemspec
+gemspec
22 LICENSE
@@ -0,0 +1,22 @@
+Copyright (c) 2012 rochefort
+
+MIT License
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,29 @@
+# Gist
+
+TODO: Write a gem description
+
+## Installation
+
+Add this line to your application's Gemfile:
+
+ gem 'gist'
+
+And then execute:
+
+ $ bundle
+
+Or install it yourself as:
+
+ $ gem install gist
+
+## Usage
+
+TODO: Write usage instructions here
+
+## Contributing
+
+1. Fork it
+2. Create your feature branch (`git checkout -b my-new-feature`)
+3. Commit your changes (`git commit -am 'Added some feature'`)
+4. Push to the branch (`git push origin my-new-feature`)
+5. Create new Pull Request
@@ -0,0 +1,2 @@
+#!/usr/bin/env rake
+require "bundler/gem_tasks"
@@ -0,0 +1,4 @@
+#!/usr/bin/env ruby
+$:.unshift(File.dirname(__FILE__) + '/../lib')
+require 'gist'
+Gist::Main.start
@@ -0,0 +1,16 @@
+# -*- encoding: utf-8 -*-
+require File.expand_path('../lib/gist/version', __FILE__)
+
+Gem::Specification.new do |gem|
+ gem.authors = ["rochefort"]
+ gem.description = %q{TODO: Write a gem description}
+ gem.summary = %q{TODO: Write a gem summary}
+ gem.homepage = ""
+
+ gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
+ gem.files = `git ls-files`.split("\n")
+ gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
+ gem.name = "gist"
+ gem.require_paths = ["lib"]
+ gem.version = Gist::VERSION
+end
@@ -0,0 +1,5 @@
+require 'gist/main'
+require 'gist/my_util'
+require 'gist/version'
+module Gist
+end
@@ -0,0 +1,84 @@
+require 'rubygems'
+require 'thor'
+require 'net/http'
+require 'json'
+require 'gist/my_util'
+
+module Gist
+ class RedirectError < StandardError; end
+ API_URL = 'https://api.github.com'
+
+ class Main < Thor
+ include MyUtil
+
+ desc 'list', 'List your gists'
+ def list
+ setup
+ write_list_header
+ @data.each_with_index do |list, i|
+ puts "%4s. %10s %s" % [i+1, list['id'], list['description']]
+ end
+ write_footer
+ end
+
+ desc 'show [ID/NUMBER]', 'Show your raw gist by id or the listing number'
+ def show(arg)
+ raise ArgumentError "#{arg} is not numerical" unless numeric?(arg)
+ setup
+ content = []
+ # IDで検索
+ @data.each_with_index do |list, i|
+ content = get_content(i) and break if list['id'] == arg
+ end
+
+ # 番号で検索
+ if content.empty? and number_of?(arg.to_i)
+ content = get_content(arg.to_i-1)
+ end
+ puts content
+ end
+
+ private
+ def get_content(n)
+ @data[n]['files'].values.inject([]) do |content, file|
+ content << ''
+ content << "file: #{file['filename']}"
+ content << '-'*100
+ content << get_body(file['raw_url'])
+ content << ''
+ end
+ end
+
+ def number_of?(n)
+ n > 0 and @data.size >= n
+ end
+
+ def setup
+ @user = ENV['GIST_USER'] || gitconfig_user
+ raise ArgumentError, "Set ENV['GIST_USER'] or 'git config user.name <your_name>'" if @user.empty?
+ @data = JSON.parse(get_list)
+ # debug
+ #cache_file = "#{ENV['HOME']}/.gist/data.txt"
+ #@data = JSON.parse(File.open(cache_file).read) if File.exist?(cache_file)
+ end
+
+ def write_list_header
+ puts
+ puts ' No. ID Description'
+ puts '-'*120
+ end
+
+ def write_footer
+ puts
+ end
+
+ # return blank if there is nothing
+ def gitconfig_user
+ `git config --get user.name`.chomp
+ end
+
+ def get_list
+ get_body(API_URL + "/users/#{@user}/gists")
+ end
+ end
+end
@@ -0,0 +1,21 @@
+module MyUtil
+
+ def numeric?(str)
+ str.to_i.to_s == str
+ end
+
+ def get_body(url, limit=2)
+ raise RedirectError, 'HTTP redirect too deep' if limit.zero?
+
+ uri = URI(url)
+ https = Net::HTTP.new(uri.host, uri.port)
+ https.use_ssl = true if uri.port == 443
+ res = https.get(uri.path)
+ case res
+ when Net::HTTPSuccess
+ res.body
+ when Net::HTTPFound # 302
+ get_body(res['location'], limit-1)
+ end
+ end
+end
@@ -0,0 +1,3 @@
+module Gist
+ VERSION = "0.0.1"
+end

0 comments on commit 1320dac

Please sign in to comment.