Module: Pastie

Defined in:
lib/pastie-api/paste.rb,
lib/pastie-api/pastie.rb,
lib/pastie-api/history.rb,
lib/pastie-api/request.rb

Defined Under Namespace

Classes: History, Paste, Request

Constant Summary collapse

BASE_URL =
'http://pastie.org'

Class Method Summary collapse

Class Method Details

.create(content, private = true, language = nil) ⇒ Object

Creates a new paste


13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/pastie-api/pastie.rb', line 13

def self.create(content, private=true, language=nil)
  params = {
    "paste[body]" => content.to_s,
    "paste[authorization]" => "burger",
    "paste[restricted]" => "1"
  }
  params["paste[restricted]"] = "0" unless private
  params["paste[parser_id]"] = parser_id(language)
  resp = Net::HTTP.post_form(URI.parse(BASE_URL + "/pastes"), params)
  if resp.kind_of?(Net::HTTPFound)
    paste_url = resp.body.scan(/<a href="(.*)">/)[0][0]
    Paste.new(Request.fetch(paste_url))
  else
    raise "Cannot create paste!"
  end
end

.get(id) ⇒ Object

Gets existing paste


5
6
7
8
9
10
# File 'lib/pastie-api/pastie.rb', line 5

def self.get(id)
  id = id.to_s
  path = id.match(/^[\d]{1,}$/) ? "/#{id}" : "/private/#{id}"
  html = Request.fetch(BASE_URL + path)
  return html.nil? ? nil : Paste.new(html)
end

.parser_id(language = nil) ⇒ Object


30
31
32
33
34
35
36
37
38
39
# File 'lib/pastie-api/pastie.rb', line 30

def self.parser_id(language=nil)
  return "6" if language.nil?  # Default to plain text
  if @parser.nil?
    @parser = {}
    %w{objective_c action_script ruby rails diff plain c css java java_script html erb bash sql php python n_a perl yaml c_sharp}.each_with_index do |lang, ii|
      @parser[lang] = (ii+1).to_s
    end
  end
  @parser[language.to_s] || @parser[:plain] 
end