conversocial 0.0.8 → 0.0.9

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: 3186bb8dceef834f750a92652082fa931e8051ae
4
- data.tar.gz: 75328ce6fa7ee90cb4537957cab4028a2a442c34
3
+ metadata.gz: 6c42f9330f82bab1e667f6bea9c1ba0b5e362ef1
4
+ data.tar.gz: bba02656ef441ecf363e587f4d49e97ceed80bc9
5
5
  SHA512:
6
- metadata.gz: 53442840ec73fbdd6bba59c41922871ecef49053946a4cbd9ba85be73061cd78af50098eb6cf6860f9fa2318799bac8ab859a8ae19c3b41c4fba6c8ae4886815
7
- data.tar.gz: 0f4f8fe97978cdb488680092ce9d2598d6ecb9bcce03e99f6fb50df37867d035cdafdfd15ff818587f695e98537c335d741ce5a40b89c9f36d4a72beaf3668d2
6
+ metadata.gz: c114cd299740b29b8743bf5fb208a3d17fd776a82f3241ab3957154001c667a0ff06d3f3a7a90da3073aad14dd34e4edd1d4320f8c01c8b49fe3c075dca0c58a
7
+ data.tar.gz: 52cad5373410c2757c0047013f903d208ee56ab001ab8b42f556dd483f43b1cd7d6dfd5ac170610d2932ad4a0b67b2c7d6e5a81c4a3878202aaf3f9a9c657946
data/README.md CHANGED
@@ -26,6 +26,9 @@ You can optionally pass a logger to log api requests made by the client.
26
26
 
27
27
  client = Conversocial::Client.new :key => '...', :secret => '...', :logger => Logger.new(STDOUT)
28
28
 
29
+ Yet another option is the cache_expiry option. If set (by default it is not), the client will cache api requests. This is useful if you are getting rate limited.
30
+
31
+ client = Conversocial::Client.new :key => '...', :secret => '...', :cache_expiry => 1.hour
29
32
 
30
33
  ## Resources
31
34
 
@@ -1,12 +1,13 @@
1
1
  module Conversocial
2
2
  class Client
3
- attr_reader :version, :key, :secret, :logger
3
+ attr_reader :version, :key, :secret, :logger, :cache_expiry
4
4
 
5
5
  def initialize options={}
6
6
  @key = options[:key]
7
7
  @secret = options[:secret]
8
8
  @version = options[:version]
9
9
  @logger = options[:logger]
10
+ @cache_expiry = options[:cache_expiry]
10
11
  end
11
12
 
12
13
  def accounts
@@ -12,6 +12,7 @@ module Conversocial
12
12
 
13
13
  def initialize client
14
14
  @client = client
15
+ @cache = Conversocial::Resources::QueryEngines::Cache.new client.cache_expiry
15
16
  clear
16
17
  end
17
18
 
@@ -52,10 +53,10 @@ module Conversocial
52
53
  end
53
54
 
54
55
  def where options
55
- options = options.map do |k,v|
56
- v = v.utc.strftime '%Y-%m-%dT%H:%M:%S' if v.kind_of? Time
56
+ options = options.map do |k, v|
57
+ v = v.utc.strftime '%Y-%m-%dT%H:%M:%S' if v.kind_of? Time
57
58
  v = v.to_s if v.kind_of? Date
58
- [k,v]
59
+ [k, v]
59
60
  end.to_h
60
61
 
61
62
  @query_params.merge! options
@@ -63,19 +64,22 @@ module Conversocial
63
64
  end
64
65
 
65
66
  def find find_id
66
- @query_params[:fields] ||= model_klass.fields.join(',')
67
67
 
68
- json = get_json add_query_params("/#{find_id}", default_find_query_params.merge(@query_params))
69
- clear
70
- if json
71
- item = new json[resource_name]
72
- attach_content_to_items( [item], json['content']).first
73
- end
68
+ @query_params[:fields] ||= model_klass.fields.join(',')
69
+
70
+ json = get_json add_query_params("/#{find_id}", default_find_query_params.merge(@query_params))
71
+ clear
72
+ if json
73
+ item = new json[resource_name]
74
+ attach_content_to_items([item], json['content']).first
75
+ end
76
+
74
77
  end
75
78
 
76
79
  def size
77
80
  fetch.size
78
81
  end
82
+
79
83
  alias :count :size
80
84
 
81
85
  def last
@@ -92,6 +96,7 @@ module Conversocial
92
96
  field = field.join ',' if field.kind_of? Array
93
97
  where :sort => field
94
98
  end
99
+
95
100
  alias :sort_by :sort
96
101
  alias :order :sort
97
102
  alias :order_by :sort
@@ -157,7 +162,7 @@ module Conversocial
157
162
  if content_json_array.present?
158
163
  items.each do |item|
159
164
  item.content_ids.each do |content_id|
160
- content_json = content_json_array.find{ |ct| ct['id'] == content_id }
165
+ content_json = content_json_array.find { |ct| ct['id'] == content_id }
161
166
  if content_json
162
167
  item.send :append_content, new_instance_of_klass(Conversocial::Resources::Models::Content, content_json)
163
168
  end
@@ -189,9 +194,11 @@ module Conversocial
189
194
 
190
195
  def get_json path
191
196
  full_path = absolute_path path
192
- client.send :log, self, full_path
193
197
 
194
- response = https_basic_auth_get client.key, client.secret, full_path
198
+ response = @cache.get_or_set full_path do
199
+ client.send :log, self, full_path
200
+ https_basic_auth_get client.key, client.secret, full_path
201
+ end
195
202
 
196
203
  if 429 == response.code.to_i
197
204
  raise Conversocial::Resources::Exceptions::RateLimitExceeded.new response.code, nil, response.body
@@ -0,0 +1,42 @@
1
+ module Conversocial
2
+ module Resources
3
+ module QueryEngines
4
+ class Cache
5
+ attr_reader :expiry
6
+
7
+ def initialize expiry=60*60
8
+ @cache = {}
9
+ @expiry = expiry.to_i
10
+ end
11
+
12
+ def get_or_set key
13
+ result = get key
14
+ if result.nil?
15
+ result = yield
16
+ set key, result
17
+ end
18
+ result
19
+ end
20
+
21
+ def set key, value
22
+ @cache[key.to_s] = {:value => value, :timestamp => Time.now}
23
+ end
24
+
25
+ def delete key
26
+ @cache.delete key.to_s
27
+ end
28
+
29
+ def get key
30
+ if @cache[key.to_s]
31
+ if (Time.now - @cache[key.to_s][:timestamp]) < expiry
32
+ @cache[key.to_s][:value]
33
+ else
34
+ delete key
35
+ nil
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
@@ -1,3 +1,3 @@
1
1
  module Conversocial
2
- VERSION = "0.0.8"
2
+ VERSION = "0.0.9"
3
3
  end
data/lib/conversocial.rb CHANGED
@@ -3,6 +3,7 @@ require "conversocial/version"
3
3
  #require utils
4
4
  require 'conversocial/utils/http'
5
5
  require 'conversocial/utils/strings'
6
+ require 'conversocial/resources/query_engines/cache'
6
7
 
7
8
  #require client
8
9
  require 'conversocial/client'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: conversocial
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.8
4
+ version: 0.0.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - Misha Conway
@@ -71,6 +71,7 @@ files:
71
71
  - lib/conversocial/resources/query_engines/account.rb
72
72
  - lib/conversocial/resources/query_engines/author.rb
73
73
  - lib/conversocial/resources/query_engines/base.rb
74
+ - lib/conversocial/resources/query_engines/cache.rb
74
75
  - lib/conversocial/resources/query_engines/channel.rb
75
76
  - lib/conversocial/resources/query_engines/conversation.rb
76
77
  - lib/conversocial/resources/query_engines/keyvalue.rb