Class: Gnip::GnipRules::Rules
- Inherits:
-
Object
- Object
- Gnip::GnipRules::Rules
- Includes:
- HTTParty
- Defined in:
- lib/gnip/gnip-rules/rules.rb
Instance Attribute Summary collapse
-
#rules_url ⇒ Object
readonly
Returns the value of attribute rules_url.
-
#version ⇒ Object
readonly
Returns the value of attribute version.
Instance Method Summary collapse
-
#add(rules) ⇒ Object
Add rules to PowerTrack rules rules should be an hash in the format [{“value”: “rule1”, “tag”: “tag1”, “value”:“rule2”]}“.
-
#delete_all! ⇒ Object
delete all rules from PowerTrack support.gnip.com/apis/powertrack/api_reference.html#DeleteRules Request Body Size Limit 1 MB (~5000 rules).
-
#initialize(client, replay = false) ⇒ Rules
constructor
A new instance of Rules.
-
#list ⇒ Object
Get the full list of rules.
-
#list_by_tag(tag) ⇒ Object
Get the full list of rules by tag.
-
#remove(rules) ⇒ Object
Remove rules from PowerTrack rules rules should be an hash in the format [{“value”: “rule1”, “tag”: “tag1”, “value”:“rule2”]}“.
Constructor Details
#initialize(client, replay = false) ⇒ Rules
Returns a new instance of Rules.
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/gnip/gnip-rules/rules.rb', line 10 def initialize(client, replay = false) @version = client.power_track_version case version when '1.0' @rules_url = "https://api.gnip.com:443/accounts/#{client.account}/publishers/#{client.publisher}/#{replay ? 'replay' : 'streams'}/track/#{client.label}/rules.json" when '2.0' if replay @rules_url = "https://gnip-api.twitter.com/rules/powertrack-replay/accounts/#{client.account}/publishers/#{client.publisher}/#{client.replay_label}.json" else @rules_url = "https://gnip-api.twitter.com/rules/powertrack/accounts/#{client.account}/publishers/#{client.publisher}/#{client.label}.json" end else raise Exception, "version #{version} is not supported from this gem." end @auth = { username: client.username, password: client.password } end |
Instance Attribute Details
#rules_url ⇒ Object (readonly)
Returns the value of attribute rules_url.
8 9 10 |
# File 'lib/gnip/gnip-rules/rules.rb', line 8 def rules_url @rules_url end |
#version ⇒ Object (readonly)
Returns the value of attribute version.
8 9 10 |
# File 'lib/gnip/gnip-rules/rules.rb', line 8 def version @version end |
Instance Method Details
#add(rules) ⇒ Object
Add rules to PowerTrack rules rules should be an hash in the format [{“value”: “rule1”, “tag”: “tag1”, “value”:“rule2”]}“
29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/gnip/gnip-rules/rules.rb', line 29 def add(rules) response = self.class.post(rules_url, basic_auth: @auth, body: rules.to_json, headers: { 'Content-Type' => 'application/json', 'Accept' => 'application/json' }, type: :json) parsed_response = safe_parsed_response(response.parsed_response) if parsed_response.present? && parsed_response['error'].present? { status: :error, code: response.response.code, error: parsed_response['error']['message'] } else { status: :success, code: 200, response: parsed_response } end rescue Exception => e { status: :error, code: 500, error: e. } end |
#delete_all! ⇒ Object
delete all rules from PowerTrack support.gnip.com/apis/powertrack/api_reference.html#DeleteRules Request Body Size Limit 1 MB (~5000 rules)
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/gnip/gnip-rules/rules.rb', line 85 def delete_all! retry_times = 0 begin rules_list = list (rules_list[:rules] || []).in_groups_of(2, false).each do |group_of_rules| remove("rules": group_of_rules) end sleep 0.05 rules_list = list if !(rules_list[:rules] || []).size.zero? delete_all! else { status: :success, code: 200, rules: [] } end rescue Exception => e retry_times += 1 if retry_times <= 3 retry else { status: :error, code: 500, error: e. } end end end |
#list ⇒ Object
Get the full list of rules
56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/gnip/gnip-rules/rules.rb', line 56 def list response = self.class.get(rules_url, basic_auth: @auth, headers: { 'Content-Type' => 'application/json', 'Accept' => 'application/json' }, type: :json) parsed_response = safe_parsed_response(response.parsed_response) if parsed_response.present? && parsed_response['error'].present? { status: :error, code: response.response.code, error: parsed_response['error']['message'] } else { status: :success, code: 200, rules: parsed_response['rules'] } end rescue Exception => e { status: :error, code: 500, error: e. } end |
#list_by_tag(tag) ⇒ Object
Get the full list of rules by tag
69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/gnip/gnip-rules/rules.rb', line 69 def list_by_tag(tag) response = self.class.get(rules_url, basic_auth: @auth) parsed_response = safe_parsed_response(response.parsed_response) if parsed_response.present? && parsed_response['error'].present? { status: :error, code: response.response.code, error: parsed_response['error']['message'] } else rules = parsed_response['rules'] { status: :success, code: 200, rules: rules.select { |rule| rule['tag'] == tag } } end rescue Exception => e { status: :error, code: 500, error: e. } end |
#remove(rules) ⇒ Object
Remove rules from PowerTrack rules rules should be an hash in the format [{“value”: “rule1”, “tag”: “tag1”, “value”:“rule2”]}“
43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/gnip/gnip-rules/rules.rb', line 43 def remove(rules) response = self.class.post(rules_url, query: { _method: 'delete' }, basic_auth: @auth, body: rules.to_json) parsed_response = safe_parsed_response(response.parsed_response) if parsed_response.present? && parsed_response['error'].present? { status: :error, code: response.response.code, error: parsed_response['error']['message'] } else { status: :success, code: 200, response: parsed_response } end rescue Exception => e { status: :error, code: 500, error: e. } end |