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 !
commit  edc1379638847f09d2637509e8ed29bb9ba7621c
tree    60a76aa7fd57152fb4347017958385e19158aa6f
parent  cdcb846dfba626604facfd1e69c368facabb02ed
gitnub / LighthouseController.rb
100644 76 lines (62 sloc) 2.327 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
require 'hpricot'
require 'open-uri'
class LighthouseController < OSX::NSObject
  ib_outlet :lighthouse_view
  ib_outlet :application_controller
  
  def awakeFromNib
    @project_url = "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.mainFrame.loadRequest(NSURLRequest.requestWithURL(NSURL.fileURLWithPath(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
    return true
  end
  
  def begin_loading_tickets
    tickets_url = "#{@project_url}/tickets.xml" % [@lighthouse[:account], @lighthouse[:project]]
    request = NSMutableURLRequest.objc_send(:requestWithURL, NSURL.URLWithString(tickets_url),
      :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")
    puts tickets[0].xpath("//body-html").to_s
  end
end