Class: Oos4ruby::Site

Inherits:
Bean::BeanClass show all
Includes:
Bean
Defined in:
lib/oos4ruby/site.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Bean

append_features

Methods inherited from Bean::BeanClass

#author, #contains?, #delete!, #to_s, #to_xml, #update!

Constructor Details

#initialize(xml, auth, slug = nil) ⇒ Site

Returns a new instance of Site.



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/oos4ruby/site.rb', line 22

def initialize(xml, auth, slug = nil)
  @xml = xml
  @auth = auth
  if slug
    @slug = slug
    @tags_scheme = "#{TAGS_URL}/#{@slug}"
    @lists_scheme = "#{LISTS_URL}/#{@slug}"
  end
  @categories_added = []
  @categories_removed = []
end

Instance Attribute Details

#locality=(value) ⇒ Object (writeonly)

Sets the attribute locality

Parameters:

  • value

    the value to set the attribute locality to.



20
21
22
# File 'lib/oos4ruby/site.rb', line 20

def locality=(value)
  @locality = value
end

#review_content=(value) ⇒ Object

Sets the attribute review_content

Parameters:

  • value

    the value to set the attribute review_content to.



20
21
22
# File 'lib/oos4ruby/site.rb', line 20

def review_content=(value)
  @review_content = value
end

#review_title=(value) ⇒ Object

Sets the attribute review_title

Parameters:

  • value

    the value to set the attribute review_title to.



20
21
22
# File 'lib/oos4ruby/site.rb', line 20

def review_title=(value)
  @review_title = value
end

#telephone=(value) ⇒ Object (writeonly)

Sets the attribute telephone

Parameters:

  • value

    the value to set the attribute telephone to.



20
21
22
# File 'lib/oos4ruby/site.rb', line 20

def telephone=(value)
  @telephone = value
end

#url=(value) ⇒ Object (writeonly)

Sets the attribute url

Parameters:

  • value

    the value to set the attribute url to.



20
21
22
# File 'lib/oos4ruby/site.rb', line 20

def url=(value)
  @url = value
end

#user_address=(value) ⇒ Object (writeonly)

Sets the attribute user_address

Parameters:

  • value

    the value to set the attribute user_address to.



20
21
22
# File 'lib/oos4ruby/site.rb', line 20

def user_address=(value)
  @user_address = value
end

Class Method Details

.dump!(opts, slug) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/oos4ruby/site.rb', line 54

def Site.dump!(opts, slug)
  require 'date'
  
  entry = create_entry    
  add_element(entry, 'author/name', slug)
  
  updated = DateTime::now.strftime("%Y-%m-%dT%H:%M:%S%z").sub(/(..)$/, ':\1')
  
  add_element entry, 'updated', updated
  add_element entry, 'id', make_id

  {:id => 'oos:id', :title => 'title', :user_address => 'oos:useraddress',
      :url => 'oos:url', :telephone => 'oos:telephone', :review_title => 'summary'}.each do |k, v|
    add_element entry, v, opts.delete(k) if opts.include?(k)
  end

  add_element entry, 'summary', opts.delete(:summary) if opts.include?(:summary) and REXML::XPath.first(entry, 'summary', XmlNamespaces).nil?
  
  if opts.include?(:privacy)
    privacy = opts.delete(:privacy)
    if privacy.is_a?REXML::Element
      entry.add_element privacy
    elsif privacy.is_a?String         
      add_category entry, privacy, PRIVACY_URL
    end
  end
    
  if opts.include?(:review_content) || opts.include?(:content)
    content = opts.delete(:review_content) || opts.delete(:content)
    attrs = {}
    text = content
    if content.respond_to?(:each_pair)
      text = content.delete(:text)
      content.each_pair {|key, value| attrs[key.to_s] = value}        
    end
    add_element entry, 'content', text, attrs
  end
  
  if opts.include?(:locality)
    locality = opts.delete(:locality)
    name = locality
    local_slug = nil
    attrs = {}
    if locality.respond_to?(:each_pair)
      name = locality[:name]
      local_slug = locality[:slug]
    end
    attrs['slug'] = local_slug if local_slug
    add_element entry, 'oos:locality', name, attrs
  end
  
  if opts.include?(:country)
    country = opts.delete(:country)
    name = country.delete(:name)
    attrs = {}
    country.each_pair {|key, value| attrs[key.to_s] = value}
    add_element entry, 'oos:country', name, attrs
  end
  
  opts.delete(:tags).each { |tag| add_category entry, tag, "#{TAGS_URL}/#{slug}"} if opts.include(:tags)
  opts.delete(:lists).each { |list| add_category entry, list, "#{LISTS_URL}/#{slug}"} if opts.include?(:lists)
  
  entry
end

.find_by_user(auth, slug, opts = {}) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/oos4ruby/site.rb', line 41

def Site.find_by_user( auth, slug, opts = {} )
  raise Oos4ruby::UnknownUser if slug.nil?
  
  uri = SITES_URL + "/#{slug}"
  uri << "?page=#{opts[:page].to_i}" if opts[:page]

  getter = HTTPInvoker.new uri, auth
  
  worked = getter.get
  
  Sites.new(Feed.read(getter.body), auth, slug) if worked
end

Instance Method Details

#add_list(list) ⇒ Object



156
157
158
159
# File 'lib/oos4ruby/site.rb', line 156

def add_list(list)
  list = Category.new(list, @lists_scheme )
  @categories_added << list
end

#add_tag(tag) ⇒ Object



151
152
153
154
# File 'lib/oos4ruby/site.rb', line 151

def add_tag(tag)
  tag = Category.new(tag, @tags_scheme )
  @categories_added << tag
end

#country=(name, slug) ⇒ Object



34
35
36
37
38
39
# File 'lib/oos4ruby/site.rb', line 34

def country=(name, slug)
  @country = REXML::Element.new('oos:country')
  @country.add_namespace('oos', OosNamespace)
  @country.add_text name 
  @country.add_attribute 'slug', slug
end

#latitudeObject



141
142
143
144
# File 'lib/oos4ruby/site.rb', line 141

def latitude
  @xml.first('./georss:where/gml:Point/gml:pos').text.
    split[0] if @xml.first('./georss:where')
end

#listsObject



130
131
132
133
134
135
136
137
138
139
# File 'lib/oos4ruby/site.rb', line 130

def lists
  return @lists if @lists
  categories = @xml.match("./atom:category[@scheme=\"#{@lists_scheme}\"]")
  if categories
    @lists = categories.map { |cat|
      Category.new cat
    }
  end
  @lists
end

#longitudeObject



146
147
148
149
# File 'lib/oos4ruby/site.rb', line 146

def longitude
  @xml.first('./georss:where/gml:Point/gml:pos').text.
    split[1] if @xml.first('./georss:where')
end

#multimediaObject



179
180
181
182
183
184
185
186
187
# File 'lib/oos4ruby/site.rb', line 179

def multimedia    
  href = @xml.first('./atom:source/app:collection').attributes['href']
  
  getter = HTTPInvoker.new href, @auth
  worked = getter.get
  
  raise getter.response.message unless worked
  Medias.new(Feed.read(getter.body), @auth, @slug)
end

#privacyObject



171
172
173
# File 'lib/oos4ruby/site.rb', line 171

def privacy
  @privacy ||= @xml.category({:scheme => PRIVACY_URL}).attributes['term']
end

#privacy=(privacy) ⇒ Object



175
176
177
# File 'lib/oos4ruby/site.rb', line 175

def privacy=(privacy)    
  @privacy_updated = privacy
end

#remove_list(list) ⇒ Object



166
167
168
169
# File 'lib/oos4ruby/site.rb', line 166

def remove_list(list)
  list = Category.new(list, @lists_scheme )
  @categories_removed << list
end

#remove_tag(tag) ⇒ Object



161
162
163
164
# File 'lib/oos4ruby/site.rb', line 161

def remove_tag(tag)
  tag = Category.new(tag, @tags_scheme )
  @categories_removed << tag
end

#tagsObject



119
120
121
122
123
124
125
126
127
128
# File 'lib/oos4ruby/site.rb', line 119

def tags
  return @tags if @tags
  categories = @xml.match("./atom:category[@scheme=\"#{@tags_scheme}\"]")
  if categories
    @tags = categories.map { |cat|
      Category.new cat
    }
  end
  @tags
end