conversocial 0.0.7 → 0.0.8

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: da63dfa82a8c5eea9d576e2b53aa4a49e4b1e6d9
4
- data.tar.gz: 73121a7f101b21496f2a9619b90b82386ca610d0
3
+ metadata.gz: 3186bb8dceef834f750a92652082fa931e8051ae
4
+ data.tar.gz: 75328ce6fa7ee90cb4537957cab4028a2a442c34
5
5
  SHA512:
6
- metadata.gz: 3ef6fc9d185148037ad701a8921c87e10a31509cc91875d3c14cc055fb83033d38382a5920560278f543960fda751d6742b3f3a0eb2b2e7f2e96c12cfbb1fa69
7
- data.tar.gz: 2ae5a97a6778e0d27dbbed132608efd9572b729d74a9526557acd72b89353044a81d06a12ac3c6490a296667eafc1555fb32009fb01213747ae3dca4bd7edd83
6
+ metadata.gz: 53442840ec73fbdd6bba59c41922871ecef49053946a4cbd9ba85be73061cd78af50098eb6cf6860f9fa2318799bac8ab859a8ae19c3b41c4fba6c8ae4886815
7
+ data.tar.gz: 0f4f8fe97978cdb488680092ce9d2598d6ecb9bcce03e99f6fb50df37867d035cdafdfd15ff818587f695e98537c335d741ce5a40b89c9f36d4a72beaf3668d2
data/README.md CHANGED
@@ -22,6 +22,11 @@ Initialize the client with your key and secret
22
22
 
23
23
  client = Conversocial::Client.new :key => '...', :secret => '...'
24
24
 
25
+ You can optionally pass a logger to log api requests made by the client.
26
+
27
+ client = Conversocial::Client.new :key => '...', :secret => '...', :logger => Logger.new(STDOUT)
28
+
29
+
25
30
  ## Resources
26
31
 
27
32
  Currently, this gem wraps most of the resources exposed by the Conversocial API. You can access these resources via the following query engines on the client object.
data/lib/conversocial.rb CHANGED
@@ -1,10 +1,12 @@
1
1
  require "conversocial/version"
2
- require 'conversocial/client'
3
2
 
4
3
  #require utils
5
4
  require 'conversocial/utils/http'
6
5
  require 'conversocial/utils/strings'
7
6
 
7
+ #require client
8
+ require 'conversocial/client'
9
+
8
10
  #require models
9
11
  require 'conversocial/resources/models/base'
10
12
  require 'conversocial/resources/models/account'
@@ -1,12 +1,12 @@
1
1
  module Conversocial
2
2
  class Client
3
- attr_reader :version, :key, :secret
4
-
3
+ attr_reader :version, :key, :secret, :logger
5
4
 
6
5
  def initialize options={}
7
6
  @key = options[:key]
8
7
  @secret = options[:secret]
9
8
  @version = options[:version]
9
+ @logger = options[:logger]
10
10
  end
11
11
 
12
12
  def accounts
@@ -41,5 +41,13 @@ module Conversocial
41
41
  def conversations
42
42
  (@conversations ||= Conversocial::Resources::QueryEngines::Conversation.new self).clear
43
43
  end
44
+
45
+ protected
46
+
47
+ def log engine, message
48
+ if logger
49
+ logger.debug "\033[32m#{[engine.class.name, message].join ' : '}\e[0m"
50
+ end
51
+ end
44
52
  end
45
53
  end
@@ -20,9 +20,7 @@ module Conversocial
20
20
  end
21
21
 
22
22
  def assign_attributes params={}
23
- params.each do |k, v|
24
- send "#{k}=".to_sym, v
25
- end
23
+ params.each { |k, v| send "#{k}=".to_sym, v }
26
24
  end
27
25
 
28
26
  def fields
@@ -30,55 +28,43 @@ module Conversocial
30
28
  end
31
29
 
32
30
  def attributes mark_not_yet_loaded=true
33
- inspect_unloaded_association = ->(v) do
34
- "#<#{singularized_resource_type_from_association_attribute(v).capitalize}(id: #{v['id']})>"
35
- end
36
-
37
-
38
- attrs = nil
39
31
  disable_association_resolving do
40
- attrs = self.class.fields.map do |field_name|
32
+ self.class.fields.map do |field_name|
41
33
  val = send(field_name.to_sym)
42
- if mark_not_yet_loaded
43
- if val.nil? && @loaded_attributes[field_name.to_sym].nil?
44
- val = :not_yet_loaded
45
- elsif association_attribute? val
46
- val = inspect_unloaded_association.call val
47
- elsif val.kind_of? Array
48
- val = val.map do |v|
49
- if association_attribute? v
50
- inspect_unloaded_association.call v
51
- else
52
- v
53
- end
54
- end
55
- end
56
- end
34
+ val = :not_yet_loaded if mark_not_yet_loaded && val.nil? && @loaded_attributes[field_name.to_sym].nil?
57
35
  [field_name, val]
58
36
  end.to_h
59
37
  end
60
- attrs
61
38
  end
62
39
 
63
40
  def refresh
64
41
  disable_association_resolving do
65
- assign_attributes query_engine.find(id).attributes(false)
42
+ fully_loaded_instance = query_engine.find id
43
+ if fully_loaded_instance
44
+ assign_attributes fully_loaded_instance.attributes(false)
45
+ else
46
+ fields.each do |f|
47
+ @loaded_attributes[f.to_sym] = 1
48
+ end
49
+ end
66
50
  end
67
51
  self
68
52
  end
69
53
 
70
-
71
54
  protected
72
55
 
73
56
  def disable_association_resolving
74
57
  @disable_association_resolving = true
75
- yield
58
+ result = yield
76
59
  @disable_association_resolving = false
60
+ result
77
61
  end
78
62
 
79
63
  def self.attributize_tags
80
64
  fields.map(&:to_sym).each do |f|
81
-
65
+ #############################################
66
+ #### define attribute writer for field #####
67
+ #############################################
82
68
  define_method "#{f}=".to_sym do |v|
83
69
  @loaded_attributes[f] = 1
84
70
 
@@ -91,12 +77,28 @@ module Conversocial
91
77
  v = Time.parse v
92
78
  end
93
79
  end
80
+ else
81
+ if v.kind_of? Array
82
+ v = v.map do |vv|
83
+ if association_attribute? vv
84
+ client.send(pluralized_resource_type_from_association_attribute(vv).to_sym).new vv
85
+ else
86
+ vv
87
+ end
88
+ end
89
+ end
90
+
91
+ if association_attribute? v
92
+ v = client.send(pluralized_resource_type_from_association_attribute(v).to_sym).new v
93
+ end
94
94
  end
95
95
 
96
96
  instance_variable_set "@#{f}", v
97
97
  end
98
98
 
99
-
99
+ #############################################
100
+ #### define attribute reader for field #####
101
+ #############################################
100
102
  define_method f do
101
103
  value = instance_variable_get "@#{f}"
102
104
  unless @disable_association_resolving
@@ -107,23 +109,7 @@ module Conversocial
107
109
  value = instance_variable_get "@#{f}"
108
110
  end
109
111
  end
110
-
111
- #lazily load association if it hasn't been loaded yet
112
- if value.kind_of? Array
113
- value = value.map do |v|
114
- if association_attribute? v
115
- load_association v
116
- else
117
- v
118
- end
119
- end
120
- send "#{f}=".to_sym, value
121
- else
122
- value = load_association value if association_attribute? value
123
- send "#{f}=".to_sym, value
124
- end
125
112
  end
126
-
127
113
  value
128
114
  end
129
115
  end
@@ -151,10 +137,7 @@ module Conversocial
151
137
  end
152
138
 
153
139
  def association_attribute? attribute_value
154
- if attribute_value.kind_of? Hash
155
- return true if attribute_value['url'].present?
156
- end
157
- false
140
+ attribute_value.kind_of?(Hash) && attribute_value.keys.sort == %w{id url}
158
141
  end
159
142
 
160
143
  def pluralized_resource_type_from_association_attribute attribute_value
@@ -128,6 +128,10 @@ module Conversocial
128
128
  absolute_path add_query_params("", default_fetch_query_params.merge(@query_params))
129
129
  end
130
130
 
131
+ def new params={}
132
+ new_instance_of_klass model_klass, params
133
+ end
134
+
131
135
  protected
132
136
 
133
137
  def comparison_filter field, comparison_operator_modifier, value
@@ -184,9 +188,10 @@ module Conversocial
184
188
  end
185
189
 
186
190
  def get_json path
187
- #puts "getting json for #{absolute_path(path)}"
191
+ full_path = absolute_path path
192
+ client.send :log, self, full_path
188
193
 
189
- response = https_basic_auth_get client.key, client.secret, absolute_path(path)
194
+ response = https_basic_auth_get client.key, client.secret, full_path
190
195
 
191
196
  if 429 == response.code.to_i
192
197
  raise Conversocial::Resources::Exceptions::RateLimitExceeded.new response.code, nil, response.body
@@ -208,14 +213,11 @@ module Conversocial
208
213
  end
209
214
  end
210
215
 
211
- def new params={}
212
- new_instance_of_klass model_klass, params
213
- end
214
-
215
216
  def new_instance_of_klass klass, params
216
- new_instance = klass.new params
217
+ new_instance = klass.new
217
218
  new_instance.send :assign_client, client
218
219
  new_instance.send :assign_query_engine, self
220
+ new_instance.assign_attributes params
219
221
  new_instance
220
222
  end
221
223
 
@@ -1,3 +1,3 @@
1
1
  module Conversocial
2
- VERSION = "0.0.7"
2
+ VERSION = "0.0.8"
3
3
  end
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.7
4
+ version: 0.0.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Misha Conway