net-github-upload 0.0.4 → 0.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.rdoc +1 -0
- data/Rakefile +2 -1
- data/lib/net/github-upload.rb +45 -37
- data/test/github_test.rb +5 -2
- metadata +18 -11
    
        data/README.rdoc
    CHANGED
    
    
    
        data/Rakefile
    CHANGED
    
    | @@ -7,7 +7,7 @@ require 'rake/packagetask' | |
| 7 7 | 
             
            require 'rake/gempackagetask'
         | 
| 8 8 | 
             
            require 'rake/rdoctask'
         | 
| 9 9 | 
             
            require 'rake/contrib/sshpublisher'
         | 
| 10 | 
            -
            require 'lib/net/github-upload'
         | 
| 10 | 
            +
            require './lib/net/github-upload'
         | 
| 11 11 |  | 
| 12 12 | 
             
            $version = Net::GitHub::Upload::VERSION
         | 
| 13 13 | 
             
            $readme = 'README.rdoc'
         | 
| @@ -51,6 +51,7 @@ spec = Gem::Specification.new do |s| | |
| 51 51 | 
             
              s.test_files = Dir["test/*_test.rb"]
         | 
| 52 52 | 
             
              s.add_dependency('nokogiri', '>=1.4.0')
         | 
| 53 53 | 
             
              s.add_dependency('faster_xml_simple')
         | 
| 54 | 
            +
              s.add_dependency('json')
         | 
| 54 55 | 
             
              s.add_dependency('httpclient')
         | 
| 55 56 | 
             
              s.files = %w(README.rdoc Rakefile) + Dir["{bin,test,lib}/**/*"]
         | 
| 56 57 | 
             
            end
         | 
    
        data/lib/net/github-upload.rb
    CHANGED
    
    | @@ -1,12 +1,14 @@ | |
| 1 | 
            +
            require 'tempfile'
         | 
| 1 2 | 
             
            require 'nokogiri'
         | 
| 2 3 | 
             
            require 'httpclient'
         | 
| 3 4 | 
             
            require 'stringio'
         | 
| 5 | 
            +
            require 'json'
         | 
| 4 6 | 
             
            require 'faster_xml_simple'
         | 
| 5 7 |  | 
| 6 8 | 
             
            module Net
         | 
| 7 9 | 
             
              module GitHub
         | 
| 8 10 | 
             
                class Upload
         | 
| 9 | 
            -
                  VERSION = '0.0. | 
| 11 | 
            +
                  VERSION = '0.0.5'
         | 
| 10 12 | 
             
                  def initialize params=nil
         | 
| 11 13 | 
             
                    @login = params[:login]
         | 
| 12 14 | 
             
                    @token = params[:token]
         | 
| @@ -40,18 +42,14 @@ module Net | |
| 40 42 | 
             
                    if info[:replace]
         | 
| 41 43 | 
             
                      list_files(info[:repos]).each { |obj|
         | 
| 42 44 | 
             
                        next unless obj[:name] == info[:name]
         | 
| 43 | 
            -
                         | 
| 44 | 
            -
                          "_method"      => "delete",
         | 
| 45 | 
            -
                          "login"        => @login,
         | 
| 46 | 
            -
                          "token"        => @token
         | 
| 47 | 
            -
                        })
         | 
| 45 | 
            +
                        delete info[:repos], obj[:id]
         | 
| 48 46 | 
             
                      }
         | 
| 49 47 | 
             
                    elsif list_files(info[:repos]).any?{|obj| obj[:name] == info[:name]}
         | 
| 50 48 | 
             
                      raise "file '#{info[:name]}' is already uploaded. please try different name"
         | 
| 51 49 | 
             
                    end
         | 
| 52 50 |  | 
| 53 51 | 
             
                    info[:content_type] ||= 'application/octet-stream'
         | 
| 54 | 
            -
                    stat = HTTPClient.post(" | 
| 52 | 
            +
                    stat = HTTPClient.post("https://github.com/#{info[:repos]}/downloads", {
         | 
| 55 53 | 
             
                      "file_size"    => info[:file] ? File.stat(info[:file]).size : info[:data].size,
         | 
| 56 54 | 
             
                      "content_type" => info[:content_type],
         | 
| 57 55 | 
             
                      "file_name"    => info[:name],
         | 
| @@ -64,32 +62,26 @@ module Net | |
| 64 62 | 
             
                      raise "Failed to post file info"
         | 
| 65 63 | 
             
                    end
         | 
| 66 64 |  | 
| 67 | 
            -
                    upload_info =  | 
| 65 | 
            +
                    upload_info = JSON.parse(stat.content)
         | 
| 68 66 | 
             
                    if info[:file]
         | 
| 69 67 | 
             
                      f = File.open(info[:file], 'rb')
         | 
| 70 | 
            -
                      stat = HTTPClient.post("http://github.s3.amazonaws.com/", [
         | 
| 71 | 
            -
                        ['Filename', info[:name]],
         | 
| 72 | 
            -
                        ['policy', upload_info['policy']],
         | 
| 73 | 
            -
                        ['success_action_status', 201],
         | 
| 74 | 
            -
                        ['key', upload_info['prefix'] + info[:name]],
         | 
| 75 | 
            -
                        ['AWSAccessKeyId', upload_info['accesskeyid']],
         | 
| 76 | 
            -
                        ['signature', upload_info['signature']],
         | 
| 77 | 
            -
                        ['acl', upload_info['acl']],
         | 
| 78 | 
            -
                        ['file', f]
         | 
| 79 | 
            -
                      ])
         | 
| 80 | 
            -
                      f.close
         | 
| 81 68 | 
             
                    else
         | 
| 82 | 
            -
                       | 
| 83 | 
            -
             | 
| 84 | 
            -
             | 
| 85 | 
            -
                        ['success_action_status', 201],
         | 
| 86 | 
            -
                        ['key', upload_info['prefix'] + info[:name]],
         | 
| 87 | 
            -
                        ['AWSAccessKeyId', upload_info['accesskeyid']],
         | 
| 88 | 
            -
                        ['signature', upload_info['signature']],
         | 
| 89 | 
            -
                        ['acl', upload_info['acl']],
         | 
| 90 | 
            -
                        ['file', StringIO.new(info[:data])]
         | 
| 91 | 
            -
                      ])
         | 
| 69 | 
            +
                      f = Tempfile.open('net-github-upload')
         | 
| 70 | 
            +
                      f << info[:data]
         | 
| 71 | 
            +
                      f.flush
         | 
| 92 72 | 
             
                    end
         | 
| 73 | 
            +
                    stat = HTTPClient.post("http://github.s3.amazonaws.com/", [
         | 
| 74 | 
            +
                      ['Filename', info[:name]],
         | 
| 75 | 
            +
                      ['policy', upload_info['policy']],
         | 
| 76 | 
            +
                      ['success_action_status', 201],
         | 
| 77 | 
            +
                      ['key', upload_info['path']],
         | 
| 78 | 
            +
                      ['AWSAccessKeyId', upload_info['accesskeyid']],
         | 
| 79 | 
            +
                      ['Content-Type', upload_info['content_type'] || 'application/octet-stream'],
         | 
| 80 | 
            +
                      ['signature', upload_info['signature']],
         | 
| 81 | 
            +
                      ['acl', upload_info['acl']],
         | 
| 82 | 
            +
                      ['file', f]
         | 
| 83 | 
            +
                    ])
         | 
| 84 | 
            +
                    f.close
         | 
| 93 85 |  | 
| 94 86 | 
             
                    if stat.code == 201
         | 
| 95 87 | 
             
                      return FasterXmlSimple.xml_in(stat.content)['PostResponse']['Location']
         | 
| @@ -102,8 +94,17 @@ module Net | |
| 102 94 | 
             
                     upload info.merge( :replace => true )
         | 
| 103 95 | 
             
                  end
         | 
| 104 96 |  | 
| 105 | 
            -
                   | 
| 97 | 
            +
                  def delete_all repos
         | 
| 98 | 
            +
                    unless repos
         | 
| 99 | 
            +
                      raise "required repository name"
         | 
| 100 | 
            +
                    end
         | 
| 101 | 
            +
                    repos = @login + '/' + repos unless repos.include? '/'
         | 
| 102 | 
            +
                    list_files(repos).each { |obj|
         | 
| 103 | 
            +
                      delete repos, obj[:id]
         | 
| 104 | 
            +
                    }
         | 
| 105 | 
            +
                  end
         | 
| 106 106 |  | 
| 107 | 
            +
                  private
         | 
| 107 108 |  | 
| 108 109 | 
             
                  def extract_error_message(stat)
         | 
| 109 110 | 
             
                    # @see http://docs.amazonwebservices.com/AmazonS3/2006-03-01/ErrorResponses.html
         | 
| @@ -113,21 +114,28 @@ module Net | |
| 113 114 | 
             
                    ''
         | 
| 114 115 | 
             
                  end
         | 
| 115 116 |  | 
| 117 | 
            +
                  def delete repos, id
         | 
| 118 | 
            +
                    HTTPClient.post("https://github.com/#{repos}/downloads/#{id.gsub( "download_", '')}", {
         | 
| 119 | 
            +
                      "_method"      => "delete",
         | 
| 120 | 
            +
                      "login"        => @login,
         | 
| 121 | 
            +
                      "token"        => @token
         | 
| 122 | 
            +
                    })
         | 
| 123 | 
            +
                  end
         | 
| 116 124 |  | 
| 117 125 | 
             
                  def list_files repos
         | 
| 118 126 | 
             
                    raise "required repository name" unless repos
         | 
| 119 | 
            -
                    res = HTTPClient.get_content(" | 
| 127 | 
            +
                    res = HTTPClient.get_content("https://github.com/#{repos}/downloads", {
         | 
| 120 128 | 
             
                      "login" => @login,
         | 
| 121 129 | 
             
                      "token" => @token
         | 
| 122 130 | 
             
                    })
         | 
| 123 | 
            -
                    Nokogiri::HTML(res).xpath('id(" | 
| 131 | 
            +
                    Nokogiri::HTML(res).xpath('id("manual_downloads")/li').map do |fileinfo|
         | 
| 124 132 | 
             
                      obj = {
         | 
| 125 | 
            -
                        : | 
| 126 | 
            -
                        : | 
| 127 | 
            -
                        : | 
| 128 | 
            -
                        : | 
| 133 | 
            +
                        :description => fileinfo.at_xpath('descendant::h4').text,
         | 
| 134 | 
            +
                        :date        => fileinfo.at_xpath('descendant::p/abbr').attribute('title').text,
         | 
| 135 | 
            +
                        :size        => fileinfo.at_xpath('descendant::p/strong').text,
         | 
| 136 | 
            +
                        :id          => /\d+$/.match(fileinfo.at_xpath('a').attribute('href').text)[0]
         | 
| 129 137 | 
             
                      }
         | 
| 130 | 
            -
                      anchor = fileinfo.at_xpath('descendant:: | 
| 138 | 
            +
                      anchor = fileinfo.at_xpath('descendant::h4/a')
         | 
| 131 139 | 
             
                      obj[:link] = anchor.attribute('href').text
         | 
| 132 140 | 
             
                      obj[:name] = anchor.text
         | 
| 133 141 | 
             
                      obj
         | 
    
        data/test/github_test.rb
    CHANGED
    
    | @@ -1,5 +1,5 @@ | |
| 1 1 | 
             
            require 'test/unit'
         | 
| 2 | 
            -
            require  | 
| 2 | 
            +
            require 'net/github-upload'
         | 
| 3 3 |  | 
| 4 4 | 
             
            class GitHubUploadTest < Test::Unit::TestCase
         | 
| 5 5 | 
             
            	def setup
         | 
| @@ -10,7 +10,8 @@ class GitHubUploadTest < Test::Unit::TestCase | |
| 10 10 | 
             
            			:token => token
         | 
| 11 11 | 
             
            		)
         | 
| 12 12 | 
             
            		@repos = 'ruby-net-github-upload'
         | 
| 13 | 
            -
             | 
| 13 | 
            +
                @gh.delete_all @repos
         | 
| 14 | 
            +
              end
         | 
| 14 15 |  | 
| 15 16 | 
             
            	def test_file_upload
         | 
| 16 17 | 
             
            		direct_link = nil
         | 
| @@ -18,6 +19,7 @@ class GitHubUploadTest < Test::Unit::TestCase | |
| 18 19 | 
             
            			direct_link = @gh.upload(
         | 
| 19 20 | 
             
            				:repos => @repos,
         | 
| 20 21 | 
             
            				:file  => 'test/test',
         | 
| 22 | 
            +
            				:content_type => 'text/plain',
         | 
| 21 23 | 
             
            				:description => "test file"
         | 
| 22 24 | 
             
            			)
         | 
| 23 25 | 
             
            		}
         | 
| @@ -28,6 +30,7 @@ class GitHubUploadTest < Test::Unit::TestCase | |
| 28 30 | 
             
            			direct_link = @gh.replace(
         | 
| 29 31 | 
             
            				:repos => @repos,
         | 
| 30 32 | 
             
            				:file  => 'test/test',
         | 
| 33 | 
            +
            				:content_type => 'text/plain',
         | 
| 31 34 | 
             
            				:description => "test file"
         | 
| 32 35 | 
             
            			)
         | 
| 33 36 | 
             
            		}
         | 
    
        metadata
    CHANGED
    
    | @@ -1,13 +1,12 @@ | |
| 1 1 | 
             
            --- !ruby/object:Gem::Specification 
         | 
| 2 2 | 
             
            name: net-github-upload
         | 
| 3 3 | 
             
            version: !ruby/object:Gem::Version 
         | 
| 4 | 
            -
              hash: 23
         | 
| 5 4 | 
             
              prerelease: false
         | 
| 6 5 | 
             
              segments: 
         | 
| 7 6 | 
             
              - 0
         | 
| 8 7 | 
             
              - 0
         | 
| 9 | 
            -
              -  | 
| 10 | 
            -
              version: 0.0. | 
| 8 | 
            +
              - 5
         | 
| 9 | 
            +
              version: 0.0.5
         | 
| 11 10 | 
             
            platform: ruby
         | 
| 12 11 | 
             
            authors: 
         | 
| 13 12 | 
             
            - Constellation
         | 
| @@ -15,7 +14,7 @@ autorequire: | |
| 15 14 | 
             
            bindir: bin
         | 
| 16 15 | 
             
            cert_chain: []
         | 
| 17 16 |  | 
| 18 | 
            -
            date: 2010- | 
| 17 | 
            +
            date: 2010-11-11 00:00:00 +09:00
         | 
| 19 18 | 
             
            default_executable: 
         | 
| 20 19 | 
             
            dependencies: 
         | 
| 21 20 | 
             
            - !ruby/object:Gem::Dependency 
         | 
| @@ -26,7 +25,6 @@ dependencies: | |
| 26 25 | 
             
                requirements: 
         | 
| 27 26 | 
             
                - - ">="
         | 
| 28 27 | 
             
                  - !ruby/object:Gem::Version 
         | 
| 29 | 
            -
                    hash: 7
         | 
| 30 28 | 
             
                    segments: 
         | 
| 31 29 | 
             
                    - 1
         | 
| 32 30 | 
             
                    - 4
         | 
| @@ -42,26 +40,37 @@ dependencies: | |
| 42 40 | 
             
                requirements: 
         | 
| 43 41 | 
             
                - - ">="
         | 
| 44 42 | 
             
                  - !ruby/object:Gem::Version 
         | 
| 45 | 
            -
                    hash: 3
         | 
| 46 43 | 
             
                    segments: 
         | 
| 47 44 | 
             
                    - 0
         | 
| 48 45 | 
             
                    version: "0"
         | 
| 49 46 | 
             
              type: :runtime
         | 
| 50 47 | 
             
              version_requirements: *id002
         | 
| 51 48 | 
             
            - !ruby/object:Gem::Dependency 
         | 
| 52 | 
            -
              name:  | 
| 49 | 
            +
              name: json
         | 
| 53 50 | 
             
              prerelease: false
         | 
| 54 51 | 
             
              requirement: &id003 !ruby/object:Gem::Requirement 
         | 
| 55 52 | 
             
                none: false
         | 
| 56 53 | 
             
                requirements: 
         | 
| 57 54 | 
             
                - - ">="
         | 
| 58 55 | 
             
                  - !ruby/object:Gem::Version 
         | 
| 59 | 
            -
                    hash: 3
         | 
| 60 56 | 
             
                    segments: 
         | 
| 61 57 | 
             
                    - 0
         | 
| 62 58 | 
             
                    version: "0"
         | 
| 63 59 | 
             
              type: :runtime
         | 
| 64 60 | 
             
              version_requirements: *id003
         | 
| 61 | 
            +
            - !ruby/object:Gem::Dependency 
         | 
| 62 | 
            +
              name: httpclient
         | 
| 63 | 
            +
              prerelease: false
         | 
| 64 | 
            +
              requirement: &id004 !ruby/object:Gem::Requirement 
         | 
| 65 | 
            +
                none: false
         | 
| 66 | 
            +
                requirements: 
         | 
| 67 | 
            +
                - - ">="
         | 
| 68 | 
            +
                  - !ruby/object:Gem::Version 
         | 
| 69 | 
            +
                    segments: 
         | 
| 70 | 
            +
                    - 0
         | 
| 71 | 
            +
                    version: "0"
         | 
| 72 | 
            +
              type: :runtime
         | 
| 73 | 
            +
              version_requirements: *id004
         | 
| 65 74 | 
             
            description: |
         | 
| 66 75 | 
             
              Ruby Net::GitHub::Upload is upload user agent for GitHub Downloads
         | 
| 67 76 |  | 
| @@ -75,8 +84,8 @@ extra_rdoc_files: | |
| 75 84 | 
             
            files: 
         | 
| 76 85 | 
             
            - README.rdoc
         | 
| 77 86 | 
             
            - Rakefile
         | 
| 78 | 
            -
            - test/test
         | 
| 79 87 | 
             
            - test/github_test.rb
         | 
| 88 | 
            +
            - test/test
         | 
| 80 89 | 
             
            - lib/net/github-upload.rb
         | 
| 81 90 | 
             
            has_rdoc: true
         | 
| 82 91 | 
             
            homepage: http://github.com/Constellation/ruby-net-github-upload
         | 
| @@ -97,7 +106,6 @@ required_ruby_version: !ruby/object:Gem::Requirement | |
| 97 106 | 
             
              requirements: 
         | 
| 98 107 | 
             
              - - ">="
         | 
| 99 108 | 
             
                - !ruby/object:Gem::Version 
         | 
| 100 | 
            -
                  hash: 3
         | 
| 101 109 | 
             
                  segments: 
         | 
| 102 110 | 
             
                  - 0
         | 
| 103 111 | 
             
                  version: "0"
         | 
| @@ -106,7 +114,6 @@ required_rubygems_version: !ruby/object:Gem::Requirement | |
| 106 114 | 
             
              requirements: 
         | 
| 107 115 | 
             
              - - ">="
         | 
| 108 116 | 
             
                - !ruby/object:Gem::Version 
         | 
| 109 | 
            -
                  hash: 3
         | 
| 110 117 | 
             
                  segments: 
         | 
| 111 118 | 
             
                  - 0
         | 
| 112 119 | 
             
                  version: "0"
         |