Class: VastApi::Listings::Entry

Inherits:
Object
  • Object
show all
Defined in:
lib/vast_api/listings/entry.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(vast, doc) ⇒ Entry

Returns a new instance of Entry.



5
6
7
8
9
# File 'lib/vast_api/listings/entry.rb', line 5

def initialize(vast, doc)
  @doc = doc
  @attributes = {}
  @vast = vast
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args, &block) ⇒ Object (private)



136
137
138
# File 'lib/vast_api/listings/entry.rb', line 136

def method_missing(sym, *args, &block)
  self[sym.to_s]
end

Instance Attribute Details

#docObject (readonly)

Returns the value of attribute doc.



4
5
6
# File 'lib/vast_api/listings/entry.rb', line 4

def doc
  @doc
end

#vastObject (readonly)

Returns the value of attribute vast.



4
5
6
# File 'lib/vast_api/listings/entry.rb', line 4

def vast
  @vast
end

Instance Method Details

#[](var_name) ⇒ Object



11
12
13
14
15
16
17
18
19
20
# File 'lib/vast_api/listings/entry.rb', line 11

def [](var_name)
  @attributes[var_name] ||=
    if %W{id author link feeds updated published}.index(var_name)
      self.send(var_name)
    elsif %W{ title }.index(var_name)
      get_var('xmlns:'+var_name)
    else
      get_var("v:#{var_name}")
    end
end

#attributesObject



22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/vast_api/listings/entry.rb', line 22

def attributes
  vast_links
  @doc.children.each do |item|
    next if item.name == 'links'
    if %W{id author link feeds updated published}.index(item.name)
      self.send(var_name)
    else
      @attributes[item.name] ||= item.content
    end
  end
  @attributes
end

#authorObject



43
44
45
# File 'lib/vast_api/listings/entry.rb', line 43

def author
  @attributes['author'] ||= get_children(@doc.at_xpath('xmlns:author'))
end

#idObject



35
36
37
# File 'lib/vast_api/listings/entry.rb', line 35

def id
  @attributes['id'] ||= get_var('v:item_id')
end


39
40
41
# File 'lib/vast_api/listings/entry.rb', line 39

def link
  @attributes['link'] ||= get_var('xmlns:id')
end

#post_leads(params) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/vast_api/listings/entry.rb', line 83

def post_leads(params)
  url = "http://leads.vast.com/leads/-/#{@vast.category}?apikey=#{@vast.api_key}"
  entry = {
    "record_id" => "#{@vast.api_url}listings/#{self.id}/-/#{@vast.category}?apikey=#{@vast.api_key}",
    "adf" =>
    {
      "customer" =>
      {
        "comments" => params["comments"] || "",
        "contact"  =>
        {
          "phone"      => params["phone"] || "",
          "address"    =>
          {
            "street_1"   => params["street_1"] || "",
            "regioncode" => params["regioncode"] || "",
            "postalcode" => params["postalcode"] || "",
            "city"       => params["city"]} || "",
            "email"      => params["email"] || "",
            "name_last"  => params["name_last"] || "",
            "name_first" => params["name_first"] || ""
        }
      }
    }
  }
  request = Net::HTTP::Post.new(url)
  request.content_type = "application/atom+xml"
  request.body = entry.to_xml(:root => 'entry', :dasherize => false)
  uri = URI.parse(url)
  http = Net::HTTP.new(uri.host, uri.port)
  response = http.request(request)
  doc = Nokogiri::XML(response.body)
  {:status => doc.at_css("status")["code"], :message => doc.at_css('message').text}
end

#publishedObject



51
52
53
# File 'lib/vast_api/listings/entry.rb', line 51

def published
  @attributes["published"] ||= DateTime.strptime(@doc.at_xpath('xmlns:published').content)
end

#req_fieldsObject



69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/vast_api/listings/entry.rb', line 69

def req_fields
  res = Net::HTTP.get_response(URI.parse("http://leads.vast.com/leads/schema/#{self.id}/-/#{@vast.category}?apikey=#{@vast.api_key}"))
  req = {'name_first' => true, 'name_last' => true, 'email' => true}
  doc = Nokogiri::XML(res.body)
  %W{street city regioncode postalcode}.each do |a|
    if (doc.at_css(a) && doc.at_css(a).attributes['required'])
      req.merge!(a => true)
    else
      req.merge!(a => false)
    end
  end
  req
end

#updatedObject



47
48
49
# File 'lib/vast_api/listings/entry.rb', line 47

def updated
  @attributes["updated"] ||= DateTime.strptime(@doc.at_xpath('xmlns:updated').content)
end


55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/vast_api/listings/entry.rb', line 55

def vast_links
  if @attributes["vast_links"].nil?
    @attributes["vast_links"] = {}
    @doc.xpath('xmlns:link').each do |ln|
      attributes = ln.attributes
      @attributes["vast_links"][attributes['rel'].content] = {}
      attributes.each do |key, node|
        @attributes["vast_links"][attributes['rel'].content][key] = node.content
      end
    end
  end
  @attributes["vast_links"]
end