Class: GnipApi::Apis::PowerTrack::Rules

Inherits:
Object
  • Object
show all
Defined in:
lib/gnip_api/apis/power_track/rules.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ Rules

In order to do any operation, you need to specify:

  • label: the label of your stream

  • source: which data source to use (I think only twitter is available)


14
15
16
17
# File 'lib/gnip_api/apis/power_track/rules.rb', line 14

def initialize params={}
  @adapter = GnipApi::Adapter.new
  @label = params[:label] || GnipApi.config.label
end

Instance Attribute Details

#adapterObject (readonly)

Returns the value of attribute adapter.


9
10
11
# File 'lib/gnip_api/apis/power_track/rules.rb', line 9

def adapter
  @adapter
end

Instance Method Details

#construct_rules(rules) ⇒ Object

Parses an array of GnipApi::Apis::PowerTrack::Rule objects to the necesary JSON format for the endpoint


48
49
50
51
52
53
54
# File 'lib/gnip_api/apis/power_track/rules.rb', line 48

def construct_rules rules
  parsed_rules = {:rules => []}
  rules.each do |rule|
    parsed_rules[:rules] << rule.attributes
  end
  parsed_rules.to_json
end

#create(rules) ⇒ Object

Creates the specified rule. Parameters:

  • rules: GnipApi::Apis::PowerTrack::Rule object


28
29
30
31
32
33
34
# File 'lib/gnip_api/apis/power_track/rules.rb', line 28

def create rules
  raise GnipApi::Errors::PowerTrack::MissingRules.new if rules.nil? || rules.empty?
  request = create_post_request(construct_rules(rules))
  response = adapter.post(request)
  return true if response.nil?
  return GnipApi::JsonParser.new.parse(response)
end

#delete(rules) ⇒ Object

Deletes the specified rule. Parameters:

  • rules: GnipApi::Apis::PowerTrack::Rule object


38
39
40
41
42
43
44
# File 'lib/gnip_api/apis/power_track/rules.rb', line 38

def delete rules
  raise GnipApi::Errors::PowerTrack::MissingRules.new if rules.nil? || rules.empty?
  request = create_delete_request(construct_rules(rules))
  response = adapter.delete(request)
  return true if response.nil?
  return GnipApi::JsonParser.new.parse(response)
end

#listObject

Returns an array of defined rules


20
21
22
23
24
# File 'lib/gnip_api/apis/power_track/rules.rb', line 20

def list
  request = create_get_request
  rules = adapter.get(request)
  parse_rules(rules)
end

#parse_rules(data) ⇒ Object


56
57
58
59
# File 'lib/gnip_api/apis/power_track/rules.rb', line 56

def parse_rules data
  parsed_data = GnipApi::JsonParser.new.parse(data)
  parsed_data['rules'].map{|rule| GnipApi::Apis::PowerTrack::Rule.new(:value => rule['value'], :tag => rule['tag'], :id => rule['id'])}
end