Class: Mygist::GistApi

Inherits:
Object
  • Object
show all
Defined in:
lib/mygist.rb

Defined Under Namespace

Classes: NotLoggedIn

Instance Method Summary collapse

Constructor Details

#initialize(username = nil, password = nil) ⇒ GistApi

Returns a new instance of GistApi.



10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/mygist.rb', line 10

def initialize(username = nil, password = nil)
  @username = username
  @password = password    
  @login_page = "https://gist.github.com/login?return_to=gist"
  @logout_page = "http://gist.github.com/logout"
  @session_page = "https://gist.github.com/session"
  @post_page = "http://gist.github.com"
  @list_page = "http://gist.github.com/gists"
  @mine_page = "http://gist.github.com/mine"
  @agent = WWW::Mechanize.new
  @is_logged = false
end

Instance Method Details

#config!(config_file = "~/.gist") ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/mygist.rb', line 23

def config!(config_file = "~/.gist")
  config_file = File.expand_path(config_file) if config_file =~ /~/
  cfg = YAML::load(File.read(config_file))
  @username = cfg["username"]
  @password = cfg["password"]
  @login_page ||= cfg["login_page"]
  @logout_page ||= cfg["logout_page"]
  @session_page ||= cfg["session_page"]
  @post_page ||= cfg["post_page"]
  @list_page ||= cfg["list_page"]
  @mine_page ||= cfg["mine_page"]
end

#create(snippet, file_name = nil, type = nil) ⇒ Object

Raises:



50
51
52
53
54
55
56
57
58
59
# File 'lib/mygist.rb', line 50

def create(snippet, file_name = nil, type = nil)
  raise NotLoggedIn unless @is_logged
  page = @agent.get(@post_page)
  gist_form = page.forms.first
  gist_form.field("file_ext[gistfile1]").value = type if type
  gist_form.field("file_name[gistfile1]").value = file_name if file_name
  gist_form.field("file_contents[gistfile1]").value = snippet if snippet
  gist_page = @agent.submit(gist_form)
  return $1 if gist_page.uri.to_s =~ /\/(\d+)$/
end

#delete!(gist_id) ⇒ Object

Raises:



76
77
78
79
80
# File 'lib/mygist.rb', line 76

def delete!(gist_id)
  raise NotLoggedIn unless @is_logged
  @agent.get("#{@post_page}/delete/#{gist_id}")
  nil
end

#listObject



61
62
63
64
# File 'lib/mygist.rb', line 61

def list
  page = @agent.get(@is_logged ? @mine_page : @list_page)
  page.links.select { |l| l.text =~ /^gist/ }.map { |l| l.uri.to_s.gsub("\/", "") }
end

#login!Object



36
37
38
39
40
41
42
43
# File 'lib/mygist.rb', line 36

def login!
  page = @agent.get(@login_page)
   = page.forms.first
  . = @username
  .password = @password
  @login_page = @agent.submit()
  @is_logged = (@login_page.uri.to_s != @session_page)
end

#logout!Object



45
46
47
48
# File 'lib/mygist.rb', line 45

def logout!
  @agent.get(@logout_page)
  nil
end

#show(gist_id, format = ".txt") ⇒ Object



66
67
68
69
70
71
72
73
74
# File 'lib/mygist.rb', line 66

def show(gist_id, format = ".txt")
  case format
  when ".js"
    "<script src='#{@post_page}/#{gist_id}.js'></script>"
  else
    page = @agent.get("#{@post_page}/#{gist_id}.txt")
    page.content
  end
end

#update!(gist_id, snippet, file_name = nil, type = nil) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/mygist.rb', line 82

def update!(gist_id, snippet, file_name = nil, type = nil)
  page = @agent.get("#{@list_page}/#{gist_id}/edit")
  update_form = page.forms.first

  file_content_name   = page.search("//textarea[@class='file-contents']").first.attributes['name']
  file_extension_name = page.search("//div[@class='gist-lang']").first.search("//select").first.attributes['name']
  file_name_name      = page.search("//input[@class='gist-name-textbox']").first.attributes['name']

  update_form.field(file_content_name).value = snippet

  unless file_name.nil?
    update_form.field(file_name_name).value = file_name
  else
    unless type.nil?
      update_form.field(file_extension_name).value = type
      update_form.field(file_name_name).value = ''
    end
  end

  @agent.submit(update_form)
  nil
end