Class: Blekko::Slashtag

Inherits:
Object
  • Object
show all
Defined in:
lib/blekko-search/slashtag.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(blekko, name, args = {}) ⇒ Slashtag

Returns a new instance of Slashtag.



6
7
8
9
10
11
12
13
14
# File 'lib/blekko-search/slashtag.rb', line 6

def initialize(blekko, name, args={})
  args = { eager_load: true }.merge(args)
  @blekko = blekko
  @name = name
  @urls = *args[:urls]
  if args[:eager_load] && urls.empty?
    self.urls = saved_urls
  end
end

Instance Attribute Details

#blekkoObject

Returns the value of attribute blekko.



4
5
6
# File 'lib/blekko-search/slashtag.rb', line 4

def blekko
  @blekko
end

#nameObject

Returns the value of attribute name.



4
5
6
# File 'lib/blekko-search/slashtag.rb', line 4

def name
  @name
end

#urlsObject

Returns the value of attribute urls.



4
5
6
# File 'lib/blekko-search/slashtag.rb', line 4

def urls
  @urls
end

Instance Method Details

#create!Object



45
46
47
# File 'lib/blekko-search/slashtag.rb', line 45

def create!
  blekko.request(save_url("create"))
end

#delete!Object



65
66
67
68
# File 'lib/blekko-search/slashtag.rb', line 65

def delete!
  return ArgumentError, "This is not implemented by blekko yet"
  blekko.reqeust(delete_url)
end

#delete_urlObject



61
62
63
# File 'lib/blekko-search/slashtag.rb', line 61

def delete_url
  blekko.protocol + blekko.host + "/tag/delete?submit=1&name=#{name}&auth=#{blekko.api_key}"
end

#remove_url(target_urls) ⇒ Object



57
58
59
# File 'lib/blekko-search/slashtag.rb', line 57

def remove_url(target_urls)
  blekko.protocol + blekko.host + "/tag/edit?submit=1&type=del&name=#{name}&urls=#{target_urls.join("%0A")}&auth=#{blekko.api_key}"
end

#remove_urls!(*target_urls) ⇒ Object



39
40
41
42
# File 'lib/blekko-search/slashtag.rb', line 39

def remove_urls!(*target_urls)
  blekko.request(remove_url(target_urls))
  true
end

#save!Object



28
29
30
31
32
33
34
35
36
37
# File 'lib/blekko-search/slashtag.rb', line 28

def save!
  begin
    if create!.read =~ /already exists/
      update!
    end
    true
  rescue
    false      
  end
end

#save_url(method, target_urls = urls) ⇒ Object



53
54
55
# File 'lib/blekko-search/slashtag.rb', line 53

def save_url(method, target_urls=urls)
  blekko.protocol + blekko.host + "/tag/add?name=#{name}&submit=#{method}&urls=#{urls.join("%0A")}&auth=#{blekko.api_key}"
end

#saved_urlsObject



20
21
22
23
24
25
26
# File 'lib/blekko-search/slashtag.rb', line 20

def saved_urls
  url = blekko.protocol + blekko.host + "/tag/view?name=" + CGI.escape(name) + "&format=text&auth=#{blekko.api_key}"
  lines = open(url, blekko.headers).collect { |line| line.strip }
  unless lines.first.scan(" ").any?
    lines.collect { |line| line }
  end
end

#update!Object



49
50
51
# File 'lib/blekko-search/slashtag.rb', line 49

def update!
  blekko.request(save_url("update"))      
end