Class: MechanicalGithub::Session

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSession

set up an agent witht the provided credentials, and log in to github with it.



6
7
8
9
# File 'lib/mechanical_github/session.rb', line 6

def initialize()
  @agent = WWW::Mechanize.new
  @logged_in = false
end

Instance Attribute Details

#agentObject (readonly)

Returns the value of attribute agent.



3
4
5
# File 'lib/mechanical_github/session.rb', line 3

def agent
  @agent
end

#logged_inObject (readonly)

Returns the value of attribute logged_in.



3
4
5
# File 'lib/mechanical_github/session.rb', line 3

def logged_in
  @logged_in
end

#usernameObject (readonly)

Returns the value of attribute username.



3
4
5
# File 'lib/mechanical_github/session.rb', line 3

def username
  @username
end

Instance Method Details

#create_repository(name, description, homepage) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/mechanical_github/session.rb', line 26

def create_repository(name, description, homepage)
  return unless @logged_in
  newpage = @agent.get('http://github.com/repositories/new')
  # create the repo
  newform  = newpage.forms[2]
  #p form
  newform['repository[name]']    = name
  newform['repository[description]'] = description
  newform['repository[homepage]'] = homepage
  newform['repository[public]'] = true #always want it to be public

  finishpage  = @agent.submit(newform)
  
  # TODO - error handle here - regex for error or something. rtn the new repo if OK, nil if not.
 
  Repository.new(self, name, @username, description, homepage)
end

#find_repository(repository_name, username = @username) ⇒ Object



44
45
46
47
48
49
50
51
# File 'lib/mechanical_github/session.rb', line 44

def find_repository(repository_name, username=@username)
  return unless username
  repopage = @agent.get(Repository.url_for(username, repository_name))
  description = repopage.search("//span[@id='repository_description']").inner_html
  homepage = repopage.search("//span[@id='repository_homepage']").inner_html
  # because the fields on a repo are currently immutable, we have to create a new repo and return
  Repository.new(self, repository_name, username, description, homepage)
end

#login(username, password) ⇒ Object

will need to do this to access private repos or be able to create stuff.



12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/mechanical_github/session.rb', line 12

def (username, password)
  @username = username
  
  loginpage  = @agent.get('https://github.com/login')
  loginform  = loginpage.forms[0]

  loginform['login']    = username
  loginform['password'] = password
  loginform['remember_me'] = '1' # make the session 'stick' a little longer, hopefully.
  loginpage  = @agent.submit(loginform)
  @logged_in = true
end

#send_message(to, subject, body) ⇒ Object



53
54
55
56
57
58
59
60
61
# File 'lib/mechanical_github/session.rb', line 53

def send_message(to, subject, body)
  return unless @logged_in
  newpage = @agent.get('http://github.com/inbox/new')
  newform = newpage.forms[2]
  newform['message[to]']      = to
  newform['message[subject]'] = subject
  newform['message[body]']    = body
  @agent.submit(newform)
end