public
Description: A Gitk-like application written in RubyCocoa that looks like it belongs on a Mac. See the wiki for downloads and screenshots.
Homepage: http://alternateidea.com
Clone URL: git://github.com/Caged/gitnub.git
Click here to lend your support to: gitnub and make a donation at www.pledgie.com !
Caged (author)
Sun Apr 27 23:05:49 -0700 2008
commit  3b4bb60ea7d45efa5659bfdb51761ab6c9cbf8fc
tree    3eceb17277c2050c640eed3e0533f40eb22528e9
parent  58057aee4abc5c0e9557397bb791faac2de03ace
gitnub / LighthouseController.rb
100644 82 lines (67 sloc) 2.659 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
class LighthouseController < OSX::NSObject
  ib_outlet :lighthouse_view
  ib_outlet :application_controller
  
  def awakeFromNib
    @project_url_template = "http://%s.lighthouseapp.com/projects/%i"
    @repo = @application_controller.repo
    unless @repo.nil?
      return unless setup_api_credentials
      setup_lighthouse_view
      #begin_loading_tickets
    end
  end
  
  
  # NSURLConnection delegate methods
  def connection_didReceiveResponse(connection, response)
    @data.length = 0
  end
  
  def connection_didReceiveData(connection, data)
    @data.appendData(data)
  end
  
  def connectionDidFinishLoading(connection)
    error = NSError.errorWithDomain_code_userInfo("Invalid XML", NSXMLParserErrorDomain, nil)
    data = NSXMLDocument.alloc.initWithData_options_error(@data, NSXMLDocumentValidate, error)
    
    #raise error.description unless error.description.blank?
    
    update_main_document(data)
  end
  
  
  private
  
  def setup_lighthouse_view
    #lighthouse_view = File.join(NSBundle.mainBundle.bundlePath, "Contents", "Resources", "lighthouse.html")
    lighthouse_view = "#{@project_url}/tickets"
    @lighthouse_view.mainFrame.loadRequest(NSURLRequest.requestWithURL(NSURL.URLWithString(lighthouse_view)))
  end
  
  def setup_api_credentials
    @lighthouse = {
      :key => @repo.config["lighthouse.key"],
      :project => @repo.config["lighthouse.project"],
      :account => @repo.config["lighthouse.account"]
    }
    if @lighthouse[:key].nil? || @lighthouse[:project].nil? || @lighthouse[:account].nil?
      return false
    end
    @project_url = "#{@project_url_template}" % [@lighthouse[:account], @lighthouse[:project]]
    return true
  end
  
  def begin_loading_tickets
    request = NSMutableURLRequest.objc_send(:requestWithURL, NSURL.URLWithString("#{@project_url}/tickets.xml"),
      :cachePolicy, NSURLRequestUseProtocolCachePolicy,
      :timeoutInterval, 60.0)
    
    request.setValue_forHTTPHeaderField(@lighthouse[:key], "X-LighthouseToken")
    connection = NSURLConnection.alloc.initWithRequest_delegate(request, self)
    if connection
      @data = NSMutableData.data
    else
      puts "Connection couldn't be established"
    end
  end
  
  def update_main_document(data)
    error = nil
    tickets = data.xpath("//ticket")
    doc = @lighthouse_view.mainFrame.DOMDocument
    ticket_list = doc.getElementById("ticket-list")
   
    tickets.each do |ticket|
      item = doc.createElement('li')
      item.setInnerHTML(%(<a href="#{@project_url}/tickets/#{ticket[:number].stringValue}">#{ticket[:title].stringValue}</a>))
      ticket_list.appendChild(item)
    end
  end
end