Class: GContacts::Element

Inherits:
Object
  • Object
show all
Defined in:
lib/google-contacts/element.rb

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Element) initialize(entry = nil)

Creates a new element by parsing the returned entry from Google



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
# File 'lib/google-contacts/element.rb', line 10

def initialize(entry=nil)
  @data = {}
  return unless entry

  @id, @updated, @content, @title, @etag = entry["id"], Time.parse(entry["updated"]), entry["content"], entry["title"], entry["@gd:etag"]
  if entry["category"]
    @category = entry["category"]["@term"].split("#", 2).last
    @category_tag = entry["category"]["@label"] if entry["category"]["@label"]
  end

  # Parse out all the relevant data
  entry.each do |key, unparsed|
    if key =~ /^gd:/
      if unparsed.is_a?(Array)
        @data[key] = unparsed.map {|v| parse_element(v)}
      else
        @data[key] = [parse_element(unparsed)]
      end
    elsif key =~ /^batch:(.+)/
      @batch ||= {}

      if $1 == "interrupted"
        @batch["status"] = "interrupted"
        @batch["code"] = "400"
        @batch["reason"] = unparsed["@reason"]
        @batch["status"] = {"parsed" => unparsed["@parsed"].to_i, "success" => unparsed["@success"].to_i, "error" => unparsed["@error"].to_i, "unprocessed" => unparsed["@unprocessed"].to_i}
      elsif $1 == "id"
        @batch["status"] = unparsed
      elsif $1 == "status"
        if unparsed.is_a?(Hash)
          @batch["code"] = unparsed["@code"]
          @batch["reason"] = unparsed["@reason"]
        else
          @batch["code"] = unparsed.attributes["code"]
          @batch["reason"] = unparsed.attributes["reason"]
        end

      elsif $1 == "operation"
        @batch["operation"] = unparsed["@type"]
      end
    end
  end

  if entry["gContact:groupMembershipInfo"].is_a?(Hash)
    @modifier_flag = :delete if entry["gContact:groupMembershipInfo"]["@deleted"] == "true"
    @group_id = entry["gContact:groupMembershipInfo"]["@href"]
  end

  # Need to know where to send the update request
  if entry["link"].is_a?(Array)
    entry["link"].each do |link|
      if link["@rel"] == "edit"
        @edit_uri = URI(link["@href"])
        break
      end
    end
  end
end

Instance Attribute Details

- (Object) batch (readonly)

Returns the value of attribute batch



4
5
6
# File 'lib/google-contacts/element.rb', line 4

def batch
  @batch
end

- (Object) category

Returns the value of attribute category



3
4
5
# File 'lib/google-contacts/element.rb', line 3

def category
  @category
end

- (Object) content

Returns the value of attribute content



3
4
5
# File 'lib/google-contacts/element.rb', line 3

def content
  @content
end

- (Object) data

Returns the value of attribute data



3
4
5
# File 'lib/google-contacts/element.rb', line 3

def data
  @data
end

- (Object) edit_uri (readonly)

Returns the value of attribute edit_uri



4
5
6
# File 'lib/google-contacts/element.rb', line 4

def edit_uri
  @edit_uri
end

- (Object) etag

Returns the value of attribute etag



3
4
5
# File 'lib/google-contacts/element.rb', line 3

def etag
  @etag
end

- (Object) group_id

Returns the value of attribute group_id



3
4
5
# File 'lib/google-contacts/element.rb', line 3

def group_id
  @group_id
end

- (Object) id (readonly)

Returns the value of attribute id



4
5
6
# File 'lib/google-contacts/element.rb', line 4

def id
  @id
end

- (Object) modifier_flag (readonly)

Returns the value of attribute modifier_flag



4
5
6
# File 'lib/google-contacts/element.rb', line 4

def modifier_flag
  @modifier_flag
end

- (Object) title

Returns the value of attribute title



3
4
5
# File 'lib/google-contacts/element.rb', line 3

def title
  @title
end

- (Object) updated (readonly)

Returns the value of attribute updated



4
5
6
# File 'lib/google-contacts/element.rb', line 4

def updated
  @updated
end

Instance Method Details

- (Object) create

Flags the element for creation, must be passed through Client#batch for the change to take affect.



103
104
105
106
107
# File 'lib/google-contacts/element.rb', line 103

def create;
  unless @id
    @modifier_flag = :create
  end
end

- (Object) delete

Flags the element for deletion, must be passed through Client#batch for the change to take affect.



111
112
113
114
115
116
117
# File 'lib/google-contacts/element.rb', line 111

def delete;
  if @id
    @modifier_flag = :delete
  else
    @modifier_flag = nil
  end
end

- (Boolean) has_modifier?

Whether #create, #delete or #update have been called



129
# File 'lib/google-contacts/element.rb', line 129

def has_modifier?; !!@modifier_flag end

- (Object) inspect Also known as: to_s



131
132
133
# File 'lib/google-contacts/element.rb', line 131

def inspect
  "#<#{self.class.name} title: \"#{@title}\", updated: \"#{@updated}\">"
end

- (Object) to_xml(batch = false)

Converts the entry into XML to be sent to Google



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/google-contacts/element.rb', line 71

def to_xml(batch=false)
  xml = "<atom:entry xmlns:atom='http://www.w3.org/2005/Atom' xmlns:gd='http://schemas.google.com/g/2005'"
  xml << " gd:etag='#{@etag}'" if @etag
  xml << ">\n"

  if batch
    xml << "  <batch:id>#{@modifier_flag}</batch:id>\n"
    xml << "  <batch:operation type='#{@modifier_flag == :create ? "insert" : @modifier_flag}'/>\n"
  end

  # While /base/ is whats returned, /full/ is what it seems to actually want
  if @id
    xml << "  <id>#{@id.to_s.gsub("/base/", "/full/")}</id>\n"
  end

  unless @modifier_flag == :delete
    xml << "  <atom:category scheme='http://schemas.google.com/g/2005#kind' term='http://schemas.google.com/g/2008##{@category}'/>\n"
    xml << "  <updated>#{Time.now.utc.iso8601}</updated>\n"
    xml << "  <atom:content type='text'>#{@content}</atom:content>\n"
    xml << "  <atom:title>#{@title}</atom:title>\n"
    xml << "  <gContact:groupMembershipInfo deleted='false' href='#{@group_id}'/>\n" if @group_id

    @data.each do |key, parsed|
      xml << handle_data(key, parsed, 2)
    end
  end

  xml << "</atom:entry>\n"
end

- (Object) update

Flags the element to be updated, must be passed through Client#batch for the change to take affect.



121
122
123
124
125
# File 'lib/google-contacts/element.rb', line 121

def update;
  if @id
    @modifier_flag = :update
  end
end