gnip-client 0.2.2 → 0.2.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 7823da744cc5f7813dc5d7ae1189a3f5ef4f46a5
4
- data.tar.gz: aa5ffac45a636bfcc599812e4d0d7305bd888a50
3
+ metadata.gz: 3e5af33e34017bd7f0dc599a4c4e9e4d1af97322
4
+ data.tar.gz: b9b6a7c5f9875acca7a789cff3a713229aac83e7
5
5
  SHA512:
6
- metadata.gz: 6d6f31dd21ddd20f4df463ef236ce4a390e876363b2e821fb32bf83f251a8b91b86704bb7b3f32bfcba1a9a160717bbdc584952bef03875a669de62da4faa61e
7
- data.tar.gz: 77055fa6b3ad251bf514c918ddfb60e73f1e11bc8307e5a65c7ff772c87c52cfc178c69d543ca989d2465b8d7a3159c2bf08e10e67d0ca360ab53a5b8f35aee5
6
+ metadata.gz: b480d84ed09647a2fc7f79f7d8a3edbf36ac69b940aaff4740f77ccc9340be37980229098fb2b9eb10f3014bf5104447423d477ff73af0dccbfccaacf328300f
7
+ data.tar.gz: 40dc53a68f67f0fcdb3530e51f57716b679b549f342974910f761e4551a85bb459ba48e8a2a9bd86e4ad1afd9ec66b6f721ad826c07d1238c3a3e695d5b9b8c6
data/.gitignore CHANGED
@@ -7,3 +7,4 @@
7
7
  /pkg/
8
8
  /spec/reports/
9
9
  /tmp/
10
+ .idea/
@@ -4,10 +4,22 @@ module Gnip
4
4
 
5
5
  include HTTParty
6
6
 
7
- attr_reader :rules_url
7
+ attr_reader :rules_url, :version
8
8
 
9
9
  def initialize(client, replay=false)
10
- @rules_url = "https://api.gnip.com:443/accounts/#{client.account}/publishers/#{client.publisher}/#{replay ? "replay" : "streams"}/track/#{client.label}/rules.json"
10
+ @version = client.power_track_version
11
+ case self.version
12
+ when '1.0'
13
+ @rules_url = "https://api.gnip.com:443/accounts/#{client.account}/publishers/#{client.publisher}/#{replay ? "replay" : "streams"}/track/#{client.label}/rules.json"
14
+ when '2.0'
15
+ if replay
16
+ @rules_url = "https://gnip-api.twitter.com/rules/powertrack-replay/accounts/#{client.account}/publishers/#{client.publisher}/#{client.replay_label}.json"
17
+ else
18
+ @rules_url = "https://gnip-api.twitter.com/rules/powertrack/accounts/#{client.account}/publishers/#{client.publisher}/#{client.label}.json"
19
+ end
20
+ else
21
+ raise Exception.new("version #{self.version} is not supported from this gem.")
22
+ end
11
23
  @auth = { username: client.username, password: client.password }
12
24
  end
13
25
 
@@ -15,11 +27,12 @@ module Gnip
15
27
  #rules should be an hash in the format {"rules": [{"value": "rule1", "tag": "tag1"}, {"value":"rule2"}]}"
16
28
  def add(rules)
17
29
  begin
18
- response = self.class.post(self.rules_url, basic_auth: @auth, body: rules.to_json)
19
- if response.parsed_response.present? && response.parsed_response["error"].present?
20
- { status: :error, code: response.response.code, error: response.parsed_response["error"]["message"] }
30
+ response = self.class.post( self.rules_url, basic_auth: @auth, body: rules.to_json, headers: { 'Content-Type' => 'application/json', 'Accept' => 'application/json' }, type: :json )
31
+ parsed_response = safe_parsed_response(response.parsed_response)
32
+ if parsed_response.present? && parsed_response["error"].present?
33
+ { status: :error, code: response.response.code, error: parsed_response["error"]["message"] }
21
34
  else
22
- { status: :success, code: 200 }
35
+ { status: :success, code: 200, response: parsed_response }
23
36
  end
24
37
  rescue Exception => e
25
38
  { status: :error, code: 500, error: e.message }
@@ -31,11 +44,12 @@ module Gnip
31
44
  #rules should be an hash in the format {"rules": [{"value": "rule1", "tag": "tag1"}, {"value":"rule2"}]}"
32
45
  def remove(rules)
33
46
  begin
34
- response = self.class.delete(self.rules_url, basic_auth: @auth, body: rules.to_json)
35
- if response.parsed_response.present? && response.parsed_response["error"].present?
36
- { status: :error, code: response.response.code, error: response.parsed_response["error"]["message"] }
47
+ response = self.class.post(self.rules_url, query: { _method: 'delete'}, basic_auth: @auth, body: rules.to_json)
48
+ parsed_response = safe_parsed_response(response.parsed_response)
49
+ if parsed_response.present? && parsed_response["error"].present?
50
+ { status: :error, code: response.response.code, error: parsed_response["error"]["message"] }
37
51
  else
38
- { status: :success, code: 200 }
52
+ { status: :success, code: 200, response: parsed_response }
39
53
  end
40
54
  rescue Exception => e
41
55
  { status: :error, code: 500, error: e.message }
@@ -46,11 +60,12 @@ module Gnip
46
60
  #Get the full list of rules
47
61
  def list
48
62
  begin
49
- response = self.class.get(self.rules_url, basic_auth: @auth)
50
- if response.parsed_response.present? && response.parsed_response["error"].present?
51
- { status: :error, code: response.response.code, error: response.parsed_response["error"]["message"] }
63
+ response = self.class.get(self.rules_url, basic_auth: @auth, headers: { 'Content-Type' => 'application/json', 'Accept' => 'application/json' }, type: :json )
64
+ parsed_response = safe_parsed_response(response.parsed_response)
65
+ if parsed_response.present? && parsed_response["error"].present?
66
+ { status: :error, code: response.response.code, error: parsed_response["error"]["message"] }
52
67
  else
53
- { status: :success, code: 200, rules: response.parsed_response["rules"] }
68
+ { status: :success, code: 200, rules: parsed_response["rules"] }
54
69
  end
55
70
  rescue Exception => e
56
71
  { status: :error, code: 500, error: e.message }
@@ -60,11 +75,12 @@ module Gnip
60
75
  #Get the full list of rules by tag
61
76
  def list_by_tag(tag)
62
77
  begin
63
- response = self.class.get(self.rules_url, basic_auth: @auth)
64
- if response.parsed_response.present? && response.parsed_response["error"].present?
65
- { status: :error, code: response.response.code, error: response.parsed_response["error"]["message"] }
78
+ response = self.class.get(self.rules_url, basic_auth: @auth)
79
+ parsed_response = safe_parsed_response(response.parsed_response)
80
+ if parsed_response.present? && parsed_response["error"].present?
81
+ { status: :error, code: response.response.code, error: parsed_response["error"]["message"] }
66
82
  else
67
- rules = response.parsed_response["rules"]
83
+ rules = parsed_response["rules"]
68
84
  { status: :success, code: 200, rules: rules.select{ |rule| rule["tag"] == tag } }
69
85
  end
70
86
  rescue Exception => e
@@ -101,6 +117,12 @@ module Gnip
101
117
 
102
118
  end
103
119
 
120
+ private
121
+ def safe_parsed_response(parsed_response)
122
+ ret = parsed_response.present? ? (parsed_response.is_a?(String) ? JSON.parse(parsed_response).with_indifferent_access : parsed_response) : nil
123
+ ret
124
+ end
125
+
104
126
  end
105
127
  end
106
128
  end
@@ -3,13 +3,20 @@ module Gnip
3
3
  class Replay < Stream
4
4
 
5
5
  def initialize(client)
6
- super
7
- @url = "https://stream.gnip.com:443/accounts/#{client.account}/publishers/#{client.publisher}/replay/track/#{client.label}.json"
6
+ super #version is setted in the super
7
+ case self.version
8
+ when '1.0'
9
+ @url = "https://stream.gnip.com:443/accounts/#{client.account}/publishers/#{client.publisher}/replay/track/#{client.replay_label}.json"
10
+ when '2.0'
11
+ @url = "https://gnip-stream.gnip.com/replay/powertrack/accounts/#{client.account}/publishers/#{client.publisher}/#{client.replay_label}.json"
12
+ else
13
+ raise Exception.new("version #{self.version} is not supported from this gem.")
14
+ end
8
15
  end
9
16
 
10
17
  def configure_handlers
11
18
  self.on_error { |error| @error_handler.attempt_to_reconnect("Gnip Connection Error. Reason was: #{error.inspect}") }
12
- self.on_connection_close { puts 'done' }
19
+ self.on_connection_close { puts 'Gnip::GnipStream::Replay -> Connection closed' }
13
20
  end
14
21
 
15
22
  def consume(options={}, &block)
@@ -7,10 +7,18 @@ module Gnip
7
7
 
8
8
  EventMachine.threadpool_size = 5
9
9
 
10
- attr_accessor :url, :backfill_client
10
+ attr_accessor :url, :backfill_client, :version
11
11
 
12
- def initialize(client)
13
- @url = "https://stream.gnip.com:443/accounts/#{client.account}/publishers/#{client.publisher}/streams/track/#{client.label}.json"
12
+ def initialize(client)
13
+ self.version = client.power_track_version
14
+ case self.version
15
+ when '1.0'
16
+ @url = "https://stream.gnip.com:443/accounts/#{client.account}/publishers/#{client.publisher}/streams/track/#{client.label}.json"
17
+ when '2.0'
18
+ @url = "https://gnip-stream.twitter.com/stream/powertrack/accounts/#{client.account}/publishers/#{client.publisher}/#{client.label}.json"
19
+ else
20
+ raise Exception.new("version #{self.version} is not supported from this gem.")
21
+ end
14
22
  @backfill_client = client.backfill_client
15
23
  @processor = JsonDataBuffer.new("\r\n", Regexp.new(/^\{.*\}\r\n/))
16
24
  @headers = {'authorization' => [client.username, client.password], 'accept-encoding' => 'gzip, compressed'}
@@ -3,22 +3,24 @@ module Gnip
3
3
 
4
4
  attr_accessor :publisher, :label, :account,
5
5
  :username, :password,
6
- :backfill_client
6
+ :backfill_client, :replay_label
7
7
 
8
- attr_reader :rules, :replay_rules, :full_archive, :stream, :replay
8
+ attr_reader :rules, :replay_rules, :full_archive, :stream, :replay, :power_track_version
9
9
 
10
10
  def initialize(options = {})
11
- @account = options[:account]
12
- @publisher = options[:publisher]||"twitter"
13
- @label = options[:label]||"dev"
14
- @username = options[:username]
15
- @password = options[:password]
16
- @backfill_client = options[:backfill_client]||nil
17
- @rules = Gnip::GnipRules::Rules.new(self)
18
- @replay_rules = Gnip::GnipRules::Rules.new(self, true)
19
- @full_archive = Gnip::GnipFullArchive::FullArchive.new(self)
20
- @stream = Gnip::GnipStream::Stream.new(self)
21
- @replay = Gnip::GnipStream::Replay.new(self)
11
+ @account = options[:account]
12
+ @publisher = options[:publisher]||"twitter"
13
+ @label = options[:label]||"dev"
14
+ @replay_label = options[:replay_label]||@label
15
+ @username = options[:username]
16
+ @password = options[:password]
17
+ @backfill_client = options[:backfill_client]||nil
18
+ @power_track_version = options[:power_track_version]||'2.0'
19
+ @rules = Gnip::GnipRules::Rules.new(self)
20
+ @replay_rules = Gnip::GnipRules::Rules.new(self, true)
21
+ @full_archive = Gnip::GnipFullArchive::FullArchive.new(self)
22
+ @stream = Gnip::GnipStream::Stream.new(self)
23
+ @replay = Gnip::GnipStream::Replay.new(self)
22
24
  end
23
25
 
24
26
  end
@@ -1,3 +1,3 @@
1
1
  module Gnip
2
- VERSION = "0.2.2"
2
+ VERSION = '0.2.3'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gnip-client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.2.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Duccio Giovannelli
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2015-11-17 00:00:00.000000000 Z
11
+ date: 2016-11-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler