gnip_api 1.0.1 → 1.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 90f541c5fd7133a7416208c0cff50618e397f47c
4
- data.tar.gz: 38b7f7a8a7259bd10f69682ad2fbf4d60d0f950a
3
+ metadata.gz: 0a575aa7bda2ab49439ff9cd2c46a2b2324fbc16
4
+ data.tar.gz: ad04004a6303514eff8aec1f7aed4c1496f7f376
5
5
  SHA512:
6
- metadata.gz: 95049acfaabc043671d936642d5b0a8b97dae3fe6914ec3cbb599a4e94e12361d6b60642f7bb28f2832befe76216f58c0745c635997f075d7ceec2f21f39b0b6
7
- data.tar.gz: fc49073866aa88340defea4aac2f4769dd2c6aae2c17ad1b510c35f774e4b572405dcd0d391b4e1f72bacbce6cd81ad7935cde0ab2a007487a0748af6bb8ee7f
6
+ metadata.gz: fca8be3517fb79eeacdc09944f468db1f5f9f9c593f1fb528d996c8be23574c655c646b7ace69bd881769df9daef30d3d3a40a27ab620fee3bcf7ae0fce201c2
7
+ data.tar.gz: da35c7a89f8c3e09cd0e1623e836acce8c092048d0749371658569236d91563900a22ad784317929b41ea1e530136cb151c8a3b88ef95723c0e95d47081ab013
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- gnip_api (1.0.1)
4
+ gnip_api (1.0.2)
5
5
  addressable
6
6
  httparty
7
7
  yajl-ruby
data/README.md CHANGED
@@ -6,16 +6,11 @@ Connect with different Gnip APIs and get data from streams.
6
6
 
7
7
  ## Recent Changes
8
8
 
9
+ - Added a debug mode to output more information on logger from adapter
9
10
  - 2.0 APIs implemented partially, more will come soon
10
11
  - Fixed a memory issue with HTTParty
11
12
  - Timeout for requests added to fail if API is non responsive, time can be configured
12
13
  - Search API returns parsed data either for counts or activities, which also makes Search API usable to get activities now
13
- - Removed unused RateLimiter
14
- - Removed unused Mutex
15
- - Added source and label to config as default values
16
- - Added Search API
17
- - All APIs now default their parameters from config, you can still call an API with different label or source
18
- - More docs
19
14
 
20
15
  ## Notes
21
16
 
@@ -53,6 +48,8 @@ GnipApi.configure |config|
53
48
  config.source = 'twitter' # General source, if none defined when quering, this will be used
54
49
  config.label = 'mystream' # General stream label, if none defined when quering, this will be used
55
50
  config.request_timeout = 120 # Default time out on all requests, defaults to 60
51
+ config.debug = false # Defaults to false, enables/disables debug output on log
52
+ config.stream_output_format = :activity # What stream should return: :json, :parsed_json or :activity?, default if :activity
56
53
  end
57
54
  ```
58
55
 
@@ -155,6 +152,19 @@ There are a few considerations to make when doing this:
155
152
  GnipApi is not dependent of a single adapter (there's a dependency with HTTParty, but shhh... it won't last too long). You can use one of the provided adapters, or you can make your own, using the BaseAdapter class. You only need to implement the desired connector POST, GET and DELETE methods. There's an extra stream_get method, but it's just a variant of GET. Keep in mind that Gnip uses compression, I found out that Excon doesn't decompress responses by default, just to name an example.
156
153
  The custom adapter does not require to live within the gem files, as long as GnipApi has access to your class, just put it in the config and you're ready to go. See the current adapters for reference.
157
154
 
155
+ ## Debug mode
156
+
157
+ At any time you can enable/disable the debug mode like this:
158
+
159
+ ```ruby
160
+ GnipApi.configuration.debug = true
161
+ ```
162
+
163
+ This will output information about the request/response. Response body is not returned to avoid excessive logging.
164
+
165
+ Note that debug data is dependent on what adapter you are using. You should be able to dump the necesary information from
166
+ your adapter and call the internal debugger to log the information.
167
+
158
168
  ## WIP State
159
169
 
160
170
  GnipApi is a WIP. Call it a beta, alpha, gem that has part of the features, whatever. It is currently usable and it's being used by... well.. me. The custom adapter feature is there, and some of the APIs of Gnip were implemented. I'll be coding more things into this, such as other APIs, request retries, error handling, different adapters for known connectors like RestClient or HTTP/net, etc.
@@ -11,6 +11,7 @@ module GnipApi
11
11
  raise GnipApi::Errors::MissingAdapter unless GnipApi.adapter_class?
12
12
  @adapter = GnipApi.config.adapter_class.new
13
13
  @logger = GnipApi.config.logger
14
+ @debug = GnipApi.config.debug
14
15
  end
15
16
 
16
17
  def get request
@@ -65,6 +65,30 @@ module GnipApi
65
65
  def create_response request, status, body, headers
66
66
  GnipApi::Response.new request, status, body, headers
67
67
  end
68
+
69
+ private
70
+ def debug_request options={}
71
+ return nil unless GnipApi.config.debug
72
+ request_headers = options[:request_headers]
73
+ request_body = options[:request_body]
74
+ request_method = options[:request_method]
75
+ request_url = options[:request_url]
76
+ response_headers = options[:response_headers]
77
+ response_status = options[:response_status]
78
+ response_body = options[:response_body]
79
+ debug_message = "REQUEST/RESPONSE DEBUG\n"
80
+ debug_message += "#{request_method} -- #{request_url}\n"
81
+ debug_message += "Request Headers:\n"
82
+ debug_message += request_headers.map{|k,v| "#{k} -> #{v.join(', ')}"}.join("\n")
83
+ debug_message += "\n\nRequest Body:\n"
84
+ debug_message += request_body
85
+ debug_message += "\n\nResponse Headers:\n"
86
+ debug_message += response_headers.map{|k,v| "#{k} -> #{v.join(', ')}"}.join("\n")
87
+ debug_message += "\n\nResponse Status: #{response_status}"
88
+ # debug_message += "\n\nResponse Body: #{response_body}\n\n"
89
+ GnipApi.logger.debug debug_message
90
+ return nil
91
+ end
68
92
  end
69
93
  end
70
94
  end
@@ -3,16 +3,19 @@ module GnipApi
3
3
  class HTTPartyAdapter < GnipApi::Adapters::BaseAdapter
4
4
  def post request
5
5
  data = HTTParty.post request.uri, :basic_auth => auth, :body => request.payload, :timeout => default_timeout
6
+ httparty_debugger(data)
6
7
  return response(request, data)
7
8
  end
8
9
 
9
10
  def delete request
10
11
  data = HTTParty.post request.uri, :basic_auth => auth, :body => request.payload, :timeout => default_timeout
12
+ httparty_debugger(data)
11
13
  return response(request, data)
12
14
  end
13
15
 
14
16
  def get request
15
17
  data = HTTParty.get request.uri, :basic_auth => auth, :timeout => default_timeout
18
+ httparty_debugger(data)
16
19
  return response(request, data)
17
20
  end
18
21
 
@@ -32,6 +35,25 @@ module GnipApi
32
35
  def response request, data
33
36
  create_response request, data.code, data.body, data.headers
34
37
  end
38
+
39
+ private
40
+ def debug_data data
41
+ {
42
+ :request_headers => data.request.instance_variable_get(:@raw_request).to_hash,
43
+ :request_body => data.request.instance_variable_get(:@raw_request).body,
44
+ :request_method => data.request.http_method.to_s,
45
+ :request_url => data.request.path.to_s,
46
+ :response_headers => data.headers,
47
+ :response_status => data.code,
48
+ :response_body => data.body
49
+ }
50
+ end
51
+
52
+ def httparty_debugger data
53
+ return nil unless GnipApi.config.debug
54
+ debug_request(debug_data(data))
55
+ return nil
56
+ end
35
57
 
36
58
  end
37
59
  end
@@ -6,6 +6,8 @@ module GnipApi
6
6
 
7
7
  def initialize params = {}
8
8
  @stream = params[:stream] || GnipApi.config.label
9
+ @output_format = GnipApi.config.stream_output_format
10
+ raise GnipApi::Errors::Configuration::InvalidOutputFormat unless GnipApi::Configuration::OUTPUT_FORMATS.include?(@output_format)
9
11
  set_config
10
12
  end
11
13
 
@@ -29,10 +31,12 @@ module GnipApi
29
31
  end
30
32
 
31
33
  def process_entries entries
32
- entries.map!{|e| parse_json(e)}.compact!
33
- entries.map!{|e| build_message(e)}
34
- log_system_messages(entries)
35
- entries
34
+ return entries if @output_format == :json
35
+ return entries.map{|e| parse_json(e)}.compact if @output_format == :parsed_json
36
+ data = entries.map{|e| parse_json(e)}.compact
37
+ data.map!{|e| build_message(e)}
38
+ log_system_messages(data)
39
+ return data
36
40
  end
37
41
 
38
42
  def log_system_messages entries
@@ -1,11 +1,16 @@
1
1
  module GnipApi
2
2
  class Configuration
3
- attr_accessor :user, :password, :adapter_class, :account, :logger, :source, :label, :request_timeout
3
+ OUTPUT_FORMATS = [:activity, :json, :parsed_json]
4
+
5
+ attr_accessor :user, :password, :adapter_class, :account, :logger, :source, :label, :request_timeout, :debug,
6
+ :stream_output_format
4
7
 
5
8
  def initialize
6
9
  @adapter_class = GnipApi::Adapters::HTTPartyAdapter
7
10
  @logger = Logger.new('tmp/gnip_api.log')
8
11
  @request_timeout = 60
12
+ @debug = false
13
+ @stream_output_format = :activity
9
14
  end
10
15
  end
11
16
  end
@@ -1,5 +1,13 @@
1
1
  module GnipApi
2
2
  module Errors
3
+ module Configuration
4
+ class InvalidOutputFormat < StandardError
5
+ def initialize msg="Invalid output format. Available formats: #{GnipApi::Configuration::OUTPUT_FORMATS}"
6
+ @message = msg
7
+ end
8
+ end
9
+ end
10
+
3
11
  module JsonParser
4
12
  class ParseError < StandardError
5
13
  end
@@ -2,9 +2,10 @@ module Gnip
2
2
  class Activity < Gnip::Message
3
3
  attr_reader :id, :object_type, :actor, :verb, :posted_time, :generator, :provider, :link,
4
4
  :body, :object, :favorites_count, :twitter_entities, :twitter_filter_level, :twitter_lang,
5
- :retweet_count, :gnip
5
+ :retweet_count, :gnip, :raw
6
6
 
7
7
  def initialize params = {}
8
+ @raw = params
8
9
  @id = params['id']
9
10
  @object_type = params['objectType']
10
11
  @actor = Gnip::Actor.new params['actor']
@@ -23,24 +24,24 @@ module Gnip
23
24
  @gnip = Gnip::GnipData.new(params['gnip']) if params['gnip']
24
25
  end
25
26
 
26
- def original_attributes
27
+ def to_h
27
28
  {
28
29
  :id => @id,
29
30
  :objectType => @object_type,
30
- :actor => @actor.original_attributes,
31
+ :actor => @actor.to_h,
31
32
  :verb => @verb,
32
33
  :postedTime => @posted_time,
33
34
  :generator => @generator,
34
35
  :provider => @provider,
35
36
  :link => @link,
36
37
  :body => @body,
37
- :object => @object.kind_of?(Gnip::Activity) ? @object.original_attributes : @object,
38
+ :object => @object.kind_of?(Gnip::Activity) ? @object.to_h : @object,
38
39
  :favoritesCount => @favorites_count,
39
40
  :twitter_entities => @twitter_entities,
40
41
  :twitter_filter_level => @twitter_filter_level,
41
42
  :twitter_lang => @twitter_lang,
42
43
  :retweetCount => @retweet_count,
43
- :gnip => @gnip ? @gnip.original_attributes : nil
44
+ :gnip => @gnip ? @gnip.to_h : nil
44
45
  }
45
46
  end
46
47
 
@@ -57,7 +58,7 @@ module Gnip
57
58
  end
58
59
 
59
60
  def to_json
60
- generate_json(original_attributes)
61
+ @raw.to_json
61
62
  end
62
63
 
63
64
  def author
@@ -2,9 +2,10 @@ module Gnip
2
2
  class Actor < Gnip::Message
3
3
  attr_reader :object_type, :id, :link, :display_name, :posted_time, :image, :summary,
4
4
  :links, :friends_count, :followers_count, :statuses_count, :twitter_time_zone, :verified,
5
- :utc_offset, :preferred_username, :languages, :location, :favorites_count
5
+ :utc_offset, :preferred_username, :languages, :location, :favorites_count, :raw
6
6
 
7
7
  def initialize params = {}
8
+ @raw = params
8
9
  @object_type = params['objectType']
9
10
  @id = params['id']
10
11
  @link = params['link']
@@ -59,7 +60,7 @@ module Gnip
59
60
  end
60
61
 
61
62
  def to_json
62
- generate_json(original_attributes)
63
+ @raw.to_json
63
64
  end
64
65
  end
65
66
  end
@@ -3,6 +3,7 @@ module Gnip
3
3
  attr_reader :message, :sent, :message_type
4
4
 
5
5
  def initialize params
6
+ @raw = params
6
7
  @message_type = params.keys.first
7
8
  @message = params['message']
8
9
  @sent = params['sent']
@@ -24,7 +25,7 @@ module Gnip
24
25
  end
25
26
 
26
27
  def to_json
27
- generate_json(original_attributes)
28
+ @raw.to_json
28
29
  end
29
30
  end
30
31
  end
@@ -1,38 +1,32 @@
1
1
  module Gnip
2
2
  class Url < Gnip::Message
3
- attr_reader :url, :expanded_url, :expanded_status, :display_url, :indices, :expanded_url_title, :expanded_url_description
3
+ attr_reader :url, :expanded_url, :expanded_status, :display_url, :indices, :expanded_url_title, :expanded_url_description, :raw
4
4
 
5
5
  def initialize params={}
6
+ @raw = params
6
7
  @url = params['url']
7
- @expanded_url = params['expanded_url']
8
8
  @display_url = params['display_url']
9
+ @expanded_url = params['expanded_url']
9
10
  @expanded_status = params['expanded_status']
10
- @indices = params['indices']
11
11
  @expanded_url_title = params['expanded_url_title']
12
12
  @expanded_url_description = params['expanded_url_description']
13
+ @indices = params['indices']
13
14
  end
14
15
 
15
- def url
16
- Addressable::URI.parse(@url) unless @url.nil?
17
- end
18
-
19
- def expanded_url
20
- Addressable::URI.parse(@expanded_url) unless @expanded_url.nil?
21
- end
22
-
23
- def original_attributes
16
+ def to_h
24
17
  {
25
18
  :url => @url,
26
- :title => @title,
27
19
  :display_url => @display_url,
28
20
  :expanded_url => @expanded_url,
29
21
  :expanded_status => @expanded_status,
22
+ :expanded_url_title => @expanded_url_title,
23
+ :expanded_url_description => @expanded_url_description,
30
24
  :indices => @indices
31
25
  }.delete_if{|k,v| v.nil?}
32
26
  end
33
27
 
34
28
  def to_json
35
- generate_json(original_attributes)
29
+ @raw.to_json
36
30
  end
37
31
  end
38
32
  end
@@ -1,3 +1,3 @@
1
1
  module GnipApi
2
- VERSION = "1.0.1"
2
+ VERSION = "1.0.2"
3
3
  end
data/lib/gnip_api.rb CHANGED
@@ -27,7 +27,6 @@ require "gnip_api/response"
27
27
  require "gnip_api/request"
28
28
  require "gnip_api/json_parser"
29
29
 
30
-
31
30
  module GnipApi
32
31
  class << self
33
32
  attr_reader :configuration
@@ -6,19 +6,62 @@ describe GnipApi::Apis::PowerTrack::Stream do
6
6
  @stream = GnipApi::Apis::PowerTrack::Stream.new
7
7
  end
8
8
 
9
+ describe '#initialize' do
10
+ it 'throws GnipApi::Errors::Configuration::InvalidOutputFormat when not included' do
11
+ GnipApi.configuration.stream_output_format = :invalid
12
+ expect(Proc.new{GnipApi::Apis::PowerTrack::Stream.new}).to raise_error(GnipApi::Errors::Configuration::InvalidOutputFormat)
13
+ end
14
+ end
15
+
9
16
  describe '#process_entries' do
10
17
  before do
11
18
  @json = File.read('spec/fixtures/activities/real_activity.json')
12
19
  end
13
20
 
14
- it 'bulds a Message object from the json' do
15
- message = @stream.process_entries([@json])
16
- expect(message.first.class).to eq(Gnip::Activity)
21
+ context 'when output format is :activity' do
22
+ it 'bulds a Message object from the json' do
23
+ message = @stream.process_entries([@json])
24
+ expect(message.first.class).to eq(Gnip::Activity)
25
+ end
26
+
27
+ it 'returns empty array if could not parse json' do
28
+ message = @stream.process_entries(['lol'])
29
+ expect(message).to eq([])
30
+ end
31
+ end
32
+
33
+ context 'when output format is :parsed_json' do
34
+ before do
35
+ GnipApi.configuration.stream_output_format = :parsed_json
36
+ @stream = GnipApi::Apis::PowerTrack::Stream.new
37
+ end
38
+
39
+ it 'parses json' do
40
+ message = @stream.process_entries([@json])
41
+ expect(message.first.class).to eq(Hash)
42
+ end
43
+
44
+ it 'returns empty array if could not parse json' do
45
+ message = @stream.process_entries(['lol'])
46
+ expect(message).to eq([])
47
+ end
17
48
  end
18
49
 
19
- it 'returns empty array if could not parse json' do
20
- message = @stream.process_entries(['lol'])
21
- expect(message).to eq([])
50
+ context 'when output format is :json' do
51
+ before do
52
+ GnipApi.configuration.stream_output_format = :json
53
+ @stream = GnipApi::Apis::PowerTrack::Stream.new
54
+ end
55
+
56
+ it 'returns raw json' do
57
+ message = @stream.process_entries([@json])
58
+ expect(message.first.class).to eq(String)
59
+ end
60
+
61
+ it 'returns whatever stream returns' do
62
+ message = @stream.process_entries(['lol'])
63
+ expect(message).to eq(['lol'])
64
+ end
22
65
  end
23
66
  end
24
67
 
@@ -1,6 +1,20 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe GnipApi::Configuration do
4
+ describe 'OUTPUT_FORMATS' do
5
+ it 'includes :json' do
6
+ expect(GnipApi::Configuration::OUTPUT_FORMATS.include?(:json)).to eq(true)
7
+ end
8
+
9
+ it 'includes :activity' do
10
+ expect(GnipApi::Configuration::OUTPUT_FORMATS.include?(:activity)).to eq(true)
11
+ end
12
+
13
+ it 'includes :parsed_json' do
14
+ expect(GnipApi::Configuration::OUTPUT_FORMATS.include?(:parsed_json)).to eq(true)
15
+ end
16
+ end
17
+
4
18
  describe '#new' do
5
19
  it 'has the default values' do
6
20
  config = GnipApi::Configuration.new
@@ -12,6 +26,7 @@ describe GnipApi::Configuration do
12
26
  expect(config.logger).not_to eq(nil)
13
27
  expect(config.source).to eq(nil)
14
28
  expect(config.label).to eq(nil)
29
+ expect(config.debug).to eq(false)
15
30
  end
16
31
  end
17
32
 
@@ -0,0 +1,71 @@
1
+ require 'spec_helper'
2
+
3
+ describe Gnip::Url do
4
+ before do
5
+ # Taken from http://support.gnip.com/enrichments/expanded_urls.html
6
+ @raw_data = '{"url": "https://t.co/b9ZdzRxzFK","expanded_url": "http://www.today.com/parents/joke-s-you-kid-11-family-friendly-april-fools-pranks-t83276","expanded_status": 200,"expanded_url_title": "The joke\'s on you, kid: 11 family-friendly April Fools pranks","expanded_url_description": "If your kids are practical jokers, turn this April Fools\' Day into a family affair."}'
7
+ @data = JSON.parse(@raw_data)
8
+ end
9
+
10
+ describe '#initialize' do
11
+ it 'receives raw data' do
12
+ expect(Proc.new{Gnip::Url.new(@data)}).not_to raise_error
13
+ end
14
+
15
+ describe 'attributes' do
16
+ before{@url = Gnip::Url.new(@raw_data)}
17
+
18
+ it 'has url' do
19
+ expect(@url.url).not_to eq(nil)
20
+ end
21
+
22
+ it 'has expanded_url' do
23
+ expect(@url.expanded_url).not_to eq(nil)
24
+ end
25
+
26
+ it 'has expanded_url_status' do
27
+ expect(@url.expanded_status).not_to eq(nil)
28
+ end
29
+
30
+ it 'has expanded_url_title' do
31
+ expect(@url.expanded_url_title).not_to eq(nil)
32
+ end
33
+
34
+ it 'has expanded_url_description' do
35
+ expect(@url.expanded_url_description).not_to eq(nil)
36
+ end
37
+ end
38
+ end
39
+
40
+ describe '#to_h' do
41
+ before{@url = Gnip::Url.new(@data)}
42
+
43
+ it 'includes url' do
44
+ expect(@url.to_h[:url]).to eq(@url.url)
45
+ end
46
+
47
+ it 'includes expanded_url' do
48
+ expect(@url.to_h[:expanded_url]).to eq(@url.expanded_url)
49
+ end
50
+
51
+ it 'includes expanded_url_status' do
52
+ expect(@url.to_h[:expanded_status]).to eq(@url.expanded_status)
53
+ end
54
+
55
+ it 'includes expanded_url_title' do
56
+ expect(@url.to_h[:expanded_url_title]).to eq(@url.expanded_url_title)
57
+ end
58
+
59
+ it 'includes expanded_url_description' do
60
+ expect(@url.to_h[:expanded_url_description]).to eq(@url.expanded_url_description)
61
+ end
62
+ end
63
+
64
+ describe '#to_json' do
65
+ before{@url = Gnip::Url.new(@data)}
66
+
67
+ it 'converts to original json' do
68
+ expect(JSON.parse(@url.to_json)).to eq(@data)
69
+ end
70
+ end
71
+ end
data/spec/spec_helper.rb CHANGED
@@ -11,6 +11,8 @@ def configure_gem
11
11
  config.adapter_class = TestAdapter
12
12
  config.source = 'twitter'
13
13
  config.label = 'prod'
14
+ config.debug = false
15
+ config.stream_output_format = :activity
14
16
  end
15
17
  end
16
18
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gnip_api
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rayko
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-10-05 00:00:00.000000000 Z
11
+ date: 2016-10-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -228,6 +228,7 @@ files:
228
228
  - spec/gnip_api/gnip/activity_spec.rb
229
229
  - spec/gnip_api/gnip/gnip_data_spec.rb
230
230
  - spec/gnip_api/gnip/message_spec.rb
231
+ - spec/gnip_api/gnip/url_spec.rb
231
232
  - spec/gnip_api/json_parser_spec.rb
232
233
  - spec/gnip_api/request_spec.rb
233
234
  - spec/gnip_api/response_spec.rb
@@ -297,6 +298,7 @@ test_files:
297
298
  - spec/gnip_api/gnip/activity_spec.rb
298
299
  - spec/gnip_api/gnip/gnip_data_spec.rb
299
300
  - spec/gnip_api/gnip/message_spec.rb
301
+ - spec/gnip_api/gnip/url_spec.rb
300
302
  - spec/gnip_api/json_parser_spec.rb
301
303
  - spec/gnip_api/request_spec.rb
302
304
  - spec/gnip_api/response_spec.rb