Sha256: 5eda407609ffc77bf3b00be58f07f0eb2e4b4755ef4de17c419813988f5dc20b

Contents?: true

Size: 1.34 KB

Versions: 10

Compression:

Stored size: 1.34 KB

Contents

require 'net/http'
require 'uri'

module Shelr
  class Publisher

    def publish(id)
      uri = URI.parse(Shelr::API_URL + '/records')
      params = { 'record' =>  prepare(id) }
      params.merge!({'api_key' => Shelr.api_key}) if api_key
      res = Net::HTTP.post_form(uri, params)
      res = JSON.parse(res.body)
      if res['ok']
        puts res['message']
        puts Shelr::API_URL + '/records/' + res['id']
      else
        puts res['message']
      end
    end

    def api_key
      unless Shelr.api_key
        print 'Paste your API KEY [or Enter to publish as Anonymous]: '
        key = STDIN.gets.strip
        Shelr.api_key = key unless key.empty?
      end
      Shelr.api_key
    end

    def prepare(id)
      puts
      puts 'Your record will be published under terms'
      puts 'of Creative Commons Attribution-ShareAlike 3.0 Unported'
      puts 'See http://creativecommons.org/licenses/by-sa/3.0/ for details.'
      puts

      out = {}
      ['meta', 'timing', 'typescript'].each do |file|
        out[file] = File.read(File.join(Shelr.data_dir(id), file))
      end

      meta = JSON.parse(out.delete('meta'))
      meta.each { |k,v| out[k] = v }
      print 'Description: '
      out['description'] = STDIN.gets.strip
      print 'Tags (ex: howto, linux): '
      out['tags'] = STDIN.gets.strip
      return out.to_json
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
shelr-0.11.8 lib/shelr/publisher.rb
shelr-0.11.7 lib/shelr/publisher.rb
shelr-0.11.6 lib/shelr/publisher.rb
shelr-0.11.5 lib/shelr/publisher.rb
shelr-0.11.4 lib/shelr/publisher.rb
shelr-0.11.3 lib/shelr/publisher.rb
shelr-0.11.2 lib/shelr/publisher.rb
shelr-0.11.1 lib/shelr/publisher.rb
shelr-0.11.0 lib/shelr/publisher.rb
shelr-0.10.0 lib/shelr/publisher.rb