Class: Tweethook::Search

Inherits:
Object
  • Object
show all
Defined in:
lib/tweethook/search.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ Search

Returns a new instance of Search.



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/tweethook/search.rb', line 25

def initialize(args)
  # change string keys to symbols
  if args.any? { |arg| arg.is_a?(String)  }
    args = args.inject({}) { |output,array| key,value = array; output[key.to_sym] = value;output  }
  end    
  # setup instance variable for every arg
  args.each { |key,value| instance_variable_set("@#{key}",value)  }
  
  @format ||= 'json'
  
  change_active_to_boolean
end

Instance Attribute Details

#activeObject

Returns the value of attribute active.



23
24
25
# File 'lib/tweethook/search.rb', line 23

def active
  @active
end

#idObject

Returns the value of attribute id.



23
24
25
# File 'lib/tweethook/search.rb', line 23

def id
  @id
end

#searchObject

Returns the value of attribute search.



23
24
25
# File 'lib/tweethook/search.rb', line 23

def search
  @search
end

#webhookObject

Returns the value of attribute webhook.



23
24
25
# File 'lib/tweethook/search.rb', line 23

def webhook
  @webhook
end

Class Method Details

.create(search, args) ⇒ Object

create a new search at Tweethook Tweethook::Search.create(‘monkey’, :webhook => ‘where.com/callback’ )



5
6
7
8
9
# File 'lib/tweethook/search.rb', line 5

def self.create(search,args)
  args.merge!({:search => search})
  search = new(args)
  search.save
end

.find(id) ⇒ Object



11
12
13
14
15
# File 'lib/tweethook/search.rb', line 11

def self.find(id)
  response = Tweethook.get('/info.json', :id => id)
  return nil if response.nil?
  new(response.first)
end

.listObject



17
18
19
20
21
# File 'lib/tweethook/search.rb', line 17

def self.list
  response = Tweethook.get('/list.json')
  return [] if response.nil?
  response.map { |hash| Tweethook::Search.new(hash)  }    
end

Instance Method Details

#change_active_to_booleanObject



38
39
40
41
42
43
# File 'lib/tweethook/search.rb', line 38

def change_active_to_boolean
  # @active defaults to true
  @active = true if @active.nil?      
  # change @active to boolean value if it's an integer
  @active = !@active.to_i.zero? if @active.is_a?(String)
end

#destroyObject



78
79
80
81
82
# File 'lib/tweethook/search.rb', line 78

def destroy
  response = Tweethook.post('/destroy.json',:id => @id)
  return false if response.nil?
  self.id = nil if response.first['id'].eql?(@id)    
end

#modifyObject



53
54
55
56
57
# File 'lib/tweethook/search.rb', line 53

def modify
  response = Tweethook.post('/modify.json', :id => @id, :search => @search, :webhook => @webhook, :active => @active)
  return nil if response.nil?
  self
end

#new_record?Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/tweethook/search.rb', line 59

def new_record?
  not @id.nil?
end

#saveObject



45
46
47
48
49
50
51
# File 'lib/tweethook/search.rb', line 45

def save
  return modify unless new_record?
  response = Tweethook.post('/create.json',:search => @search, :webhook => @webhook, :active => @active)
  return nil if response.nil?
  @id = response.first['id']
  self
end

#startObject Also known as: resume



63
64
65
66
67
# File 'lib/tweethook/search.rb', line 63

def start
  response = Tweethook.post('/start.json', :id => @id)
  return false if response.nil?
  @active = true
end

#stopObject Also known as: pause



70
71
72
73
74
# File 'lib/tweethook/search.rb', line 70

def stop
  response = Tweethook.post('/stop.json', :id => @id)
  return false if response.nil?
  @active = false
end