Class: Googletastic::Row

Inherits:
Base
  • Object
show all
Defined in:
lib/googletastic/row.rb

Constant Summary

Constants included from Mixins::Namespaces

Mixins::Namespaces::NAMESPACES

Instance Attribute Summary collapse

Attributes inherited from Base

#acl, #attachment_path, #created_at, #etag, #head, #id, #keep_raw, #raw, #response, #synced_with, #updated_at

Attributes included from Mixins::Attributes

#attributes

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#initialize, #to_xml

Methods included from Mixins::Pagination

included

Methods included from Mixins::Finders

included

Methods included from Mixins::Parsing

included

Methods included from Mixins::Requesting

included

Methods included from Mixins::Attributes

#attribute_names, #has_attribute?, #inspect

Methods included from Mixins::Namespaces

included

Constructor Details

This class inherits a constructor from Googletastic::Base

Instance Attribute Details

#dataObject

Returns the value of attribute data.



3
4
5
# File 'lib/googletastic/row.rb', line 3

def data
  @data
end

#spreadsheet_idObject

Returns the value of attribute spreadsheet_id.



3
4
5
# File 'lib/googletastic/row.rb', line 3

def spreadsheet_id
  @spreadsheet_id
end

#titleObject

Returns the value of attribute title.



3
4
5
# File 'lib/googletastic/row.rb', line 3

def title
  @title
end

#worksheet_idObject

Returns the value of attribute worksheet_id.



3
4
5
# File 'lib/googletastic/row.rb', line 3

def worksheet_id
  @worksheet_id
end

Class Method Details

.build_url(options) ⇒ Object



36
37
38
39
40
# File 'lib/googletastic/row.rb', line 36

def build_url(options)
  raise "You must specify a spreadsheet key 'key' and a 'worksheet_id' for a list of rows" unless (options[:key] and options[:worksheet_id])
  options[:url] ||= index_url(File.join(options[:key], options[:worksheet_id]))
  super(options)
end

.client_classObject



20
21
22
# File 'lib/googletastic/row.rb', line 20

def client_class
  "Spreadsheets"
end

.get_url(path, id) ⇒ Object



32
33
34
# File 'lib/googletastic/row.rb', line 32

def get_url(path, id)
  "http://spreadsheets.google.com/feeds/list/#{path}/private/full/#{id}"
end

.index_url(path) ⇒ Object



24
25
26
# File 'lib/googletastic/row.rb', line 24

def index_url(path)
  "http://spreadsheets.google.com/feeds/list/#{path}/private/full"
end

.marshall(record) ⇒ Object

for save and update



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/googletastic/row.rb', line 77

def marshall(record)
  Nokogiri::XML::Builder.new { |xml| 
    attributes = nil
    if record.id
      attributes = {"etag" => record.etag}.merge(ns_xml("atom", "gsx"))
    else
      attributes = ns_xml("atom", "gsx")
    end
    xml.entry(attributes) {
      if record.id # PUT = update
        xml.id_ {
          xml.text record.get_url
        }
        #xml.updated {
        #  xml.text record.updated.to_s
        #}
      end
      if record.title
        xml.title(:type => "text") {
          xml.text record.title
        }
      end
      record.data.each do |k,v|
        xml["gsx"].send(k) {
          xml.text v
        }
      end
    }
  }.to_xml
end

.show_url(path, id) ⇒ Object



28
29
30
# File 'lib/googletastic/row.rb', line 28

def show_url(path, id)
  "http://spreadsheets.google.com/feeds/list/#{path}/private/full/#{id}"
end

.unmarshall(xml) ⇒ Object



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
# File 'lib/googletastic/row.rb', line 42

def unmarshall(xml)
  head    = unmarshall_head(xml)
  records = xml.xpath("//atom:entry", ns_tag("atom")).collect do |record|
    etag              = record["etag"].to_s
    id                = record.xpath("atom:id", ns_tag("atom")).first.text
    ids               = id =~ /http:\/\/spreadsheets\.google\.com\/feeds\/list\/([^\/]+)\/([^\/]+)\/([^\/]+)/
    spreadsheet_id    = $1
    worksheet_id      = $2
    id                = $3
    title             = record.xpath("atom:title", ns_tag("atom")).first.text
    content           = record.xpath("atom:content", ns_tag("atom")).first.text
    created_at        = record.xpath("atom:published", ns_tag("atom")).text
    updated_at        = record.xpath("atom:updated", ns_tag("atom")).text
    data              = {}
    
    record.xpath("gsx:*", ns_tag('gsx')).each do |node|
      next unless node.elem?
      data[node.name] = node.text
    end
    
    Googletastic::Row.new(
      :id => id,
      :etag => etag,
      :title => title,
      :spreadsheet_id => spreadsheet_id,
      :worksheet_id => worksheet_id,
      :data => data,
      :updated_at => DateTime.parse(updated_at),
      :head => head
    )
  end
  records
end

Instance Method Details

#edit_urlObject



13
14
15
# File 'lib/googletastic/row.rb', line 13

def edit_url
  "http://spreadsheets.google.com/feeds/list/#{self.spreadsheet_id}/#{self.worksheet_id}/private/full/#{self.id}"
end

#get_urlObject



5
6
7
# File 'lib/googletastic/row.rb', line 5

def get_url
  self.class.get_url(File.join(self.spreadsheet_id, self.worksheet_id), self.id)
end

#index_urlObject



9
10
11
# File 'lib/googletastic/row.rb', line 9

def index_url
  "http://spreadsheets.google.com/feeds/list/#{self.spreadsheet_id}/#{self.worksheet_id}/private/full"
end