Class: GoogleBase::Adapter

Inherits:
DataMapper::Adapters::AbstractAdapter
  • Object
show all
Defined in:
lib/googlebase/adapter.rb

Constant Summary collapse

XML_ATTRIBUTES =
{
  :xmlns => 'http://www.w3.org/2005/Atom',
  'xmlns:g' => 'http://base.google.com/ns/1.0',
  'xmlns:gd' => 'http://schemas.google.com/g/2005'
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#dry_runObject

Returns the value of attribute dry_run.



21
22
23
# File 'lib/googlebase/adapter.rb', line 21

def dry_run
  @dry_run
end

#gbObject (readonly)

Returns the value of attribute gb.



20
21
22
# File 'lib/googlebase/adapter.rb', line 20

def gb
  @gb
end

Instance Method Details

#build_xml(resource) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/googlebase/adapter.rb', line 115

def build_xml(resource)
  builder = Nokogiri::XML::Builder.new(:encoding => 'UTF-8') do |xml|

    xml.entry(XML_ATTRIBUTES) do
      resource.model.properties.each do |property|
        value = property.get(resource)
        next if value.blank?

        if to_xml = property.options[:to_xml]
          to_xml.call(xml, value)
        elsif not property.options.has_key?(:to_xml)
          xml.send "#{property.field}_", value
        end
      end
    end

  end

  builder.to_xml
end

#create(resources) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/googlebase/adapter.rb', line 64

def create(resources)
  result = 0

  resources.each do |resource|
    xml = build_xml(resource)
    url = "http://www.google.com/base/feeds/items"
    url << "?dry-run=true" if @dry_run

    response = @gb.post(url, xml)

    result += 1 if response.status_code == 201
  end

  result
end

#delete(resources) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/googlebase/adapter.rb', line 96

def delete(resources)
  result = 0

  resources.each do |resource|
    url = resource.key.first
    url << "?dry-run=true" if @dry_run

    response = @gb.delete(url)

    result += 1 if response.status_code == 200
  end

  result
end

#read(query) ⇒ Object



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
# File 'lib/googlebase/adapter.rb', line 23

def read(query)
  records = []

  operands = query.conditions.operands

  if read_one?(operands)

    response = @gb.get(operands.first.value)
    xml = Nokogiri::XML.parse(response.body)

    each_record(xml, query.fields) do |record|
      records << record
    end

  elsif read_all?(operands)

    start    = query.offset + 1
    per_page = query.limit || 250

    url = "http://www.google.com/base/feeds/items?start-index=#{start}&max-results=#{per_page}"

    while url
      response = @gb.get(url)
      xml = Nokogiri::XML.parse(response.body).at('./xmlns:feed')

      each_record(xml, query.fields) do |record|
        records << record
      end

      break if query.limit && query.limit >= records.length
      url = xml.at("./xmlns:link[@rel='next']/@href")
    end

  else
    raise NotImplementedError
    # TODO implement query conditions
  end

  records
end

#tokenObject



111
112
113
# File 'lib/googlebase/adapter.rb', line 111

def token
  @gb.auth_handler.token
end

#update(attributes, resources) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/googlebase/adapter.rb', line 80

def update(attributes, resources)
  result = 0

  resources.each do |resource|
    xml = build_xml(resource)
    url = resource.key.first
    url << "?dry-run=true" if @dry_run

    response = @gb.put(url, xml)

    result += 1 if response.status_code == 200
  end

  result
end