Class: LocalUnfuddleNotebook::Notebook

Inherits:
RestClient::Resource
  • Object
show all
Defined in:
lib/local_unfuddle_notebook/notebook.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#last_updated_atObject

Returns the value of attribute last_updated_at.



32
33
34
# File 'lib/local_unfuddle_notebook/notebook.rb', line 32

def last_updated_at
  @last_updated_at
end

Class Method Details

.attributes_pathObject



7
8
9
# File 'lib/local_unfuddle_notebook/notebook.rb', line 7

def attributes_path
  ".local_unfuddle_notebook.yaml"
end

.localObject



11
12
13
# File 'lib/local_unfuddle_notebook/notebook.rb', line 11

def local
  with_attributes(saved_attributes)
end

.local_pages_pathObject



15
16
17
# File 'lib/local_unfuddle_notebook/notebook.rb', line 15

def local_pages_path
  "pages"
end

.saved_attributesObject



19
20
21
# File 'lib/local_unfuddle_notebook/notebook.rb', line 19

def saved_attributes
  YAML::load(Pow(attributes_path).read) if Pow(attributes_path).file?
end

.with_attributes(attributes) ⇒ Object



23
24
25
26
27
28
29
# File 'lib/local_unfuddle_notebook/notebook.rb', line 23

def with_attributes(attributes)
  attributes[:protocol] ||= 'http'
  url = "#{attributes[:protocol]}://#{attributes[:subdomain]}.unfuddle.com/api/v1/projects/#{attributes[:project_id]}/notebooks/#{attributes[:notebook_id]}"
  Notebook.new(url, attributes[:username], attributes[:password]).tap do |notebook|
    notebook.last_updated_at = attributes[:last_updated_at]
  end
end

Instance Method Details

#local_attributesObject



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/local_unfuddle_notebook/notebook.rb', line 34

def local_attributes
  {
    :subdomain => subdomain,
    :username => user,
    :password => password,
    :project_id => project_id,
    :notebook_id => notebook_id,
    :last_updated_at => last_updated_at,
    :protocol => protocol
  }
end

#local_pagesObject



46
47
48
49
50
51
52
53
# File 'lib/local_unfuddle_notebook/notebook.rb', line 46

def local_pages
  Pow(Notebook.local_pages_path).files.map do |file|
    Page.new(YAML.load(file.read)).tap do |page|
      page.notebook = self
      page.local_file = file
    end
  end
end

#notebook_idObject



55
56
57
# File 'lib/local_unfuddle_notebook/notebook.rb', line 55

def notebook_id
  url.match(%r{notebooks/(\d+)})[1].to_i
end

#project_idObject



59
60
61
# File 'lib/local_unfuddle_notebook/notebook.rb', line 59

def project_id
  url.match(%r{projects/(\d+)})[1].to_i
end

#protocolObject



63
64
65
# File 'lib/local_unfuddle_notebook/notebook.rb', line 63

def protocol
  url.match(%r{^(\w*)://})[1]
end

#pullObject



67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/local_unfuddle_notebook/notebook.rb', line 67

def pull
  if Pow(self.class.local_pages_path).exists?
    Pow(self.class.local_pages_path).delete!
  end

  remote_pages.each do |page|
    page.save
  end

  self.last_updated_at = Time.now
  self.update_attributes_file
end

#push(message) ⇒ Object



80
81
82
83
84
85
86
87
# File 'lib/local_unfuddle_notebook/notebook.rb', line 80

def push(message)
  local_pages.select{|page| page.changed?}.each do |page|
    page.message = message
    page.push
  end

  pull
end

#remote_pagesObject



89
90
91
92
93
94
95
96
# File 'lib/local_unfuddle_notebook/notebook.rb', line 89

def remote_pages
  Crack::XML.parse(self["pages/unique"].get)['pages'].map do |remote_page_hash|
    page_attributes = remote_page_hash.delete_if{|k, v| ! Page.attributes.include?(k.to_sym) }
    Page.new(page_attributes).tap do |page|
      page.notebook = self
    end
  end
end

#subdomainObject



98
99
100
# File 'lib/local_unfuddle_notebook/notebook.rb', line 98

def subdomain
  url.match(%r{http.*://(.*)\.unfuddle.com})[1]
end

#update_attributes_fileObject



102
103
104
# File 'lib/local_unfuddle_notebook/notebook.rb', line 102

def update_attributes_file
  Pow(self.class.attributes_path).write local_attributes.to_yaml
end

#url_with_basic_auth(suburl) ⇒ Object



106
107
108
# File 'lib/local_unfuddle_notebook/notebook.rb', line 106

def url_with_basic_auth(suburl)
  self[suburl].url.sub("#{protocol}://", "#{protocol}://#{user}:#{password}@")
end