yam 1.1.0 → 2.0.0
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 +4 -4
- checksums.yaml.gz.sig +0 -0
- data/.travis.yml +1 -2
- data/CHANGELOG.md +7 -1
- data/README.md +6 -7
- data/lib/yammer/http_adapter.rb +65 -65
- data/lib/yammer/resources/base.rb +210 -0
- data/lib/yammer/{group.rb → resources/group.rb} +9 -7
- data/lib/yammer/{group_membership.rb → resources/group_membership.rb} +7 -5
- data/lib/yammer/resources/identity_map.rb +58 -0
- data/lib/yammer/resources/message.rb +34 -0
- data/lib/yammer/{message.rb → resources/message_body.rb} +10 -13
- data/lib/yammer/{pending_attachment.rb → resources/pending_attachment.rb} +0 -0
- data/lib/yammer/resources/thread.rb +60 -0
- data/lib/yammer/resources/user.rb +67 -0
- data/lib/yammer/{message_body.rb → resources.rb} +8 -13
- data/lib/yammer/version.rb +2 -2
- data/lib/yammer.rb +1 -7
- data/spec/{model → resources}/base_spec.rb +2 -2
- data/spec/{model → resources}/group_membership_spec.rb +2 -2
- data/spec/{model → resources}/group_spec.rb +2 -2
- data/spec/{identity_map_spec.rb → resources/identity_map_spec.rb} +6 -6
- data/spec/{model → resources}/message_spec.rb +2 -2
- data/spec/{model → resources}/thread_spec.rb +7 -7
- data/spec/{model → resources}/user_spec.rb +7 -7
- data.tar.gz.sig +0 -0
- metadata +25 -24
- metadata.gz.sig +0 -0
- data/lib/yammer/base.rb +0 -208
- data/lib/yammer/identity_map.rb +0 -56
- data/lib/yammer/thread.rb +0 -58
- data/lib/yammer/user.rb +0 -66
@@ -0,0 +1,67 @@
|
|
1
|
+
# Copyright (c) Microsoft Corporation
|
2
|
+
# All rights reserved.
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
4
|
+
# you may not use this file except in compliance with the License.
|
5
|
+
# You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
|
6
|
+
#
|
7
|
+
# THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR
|
8
|
+
# CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING
|
9
|
+
# WITHOUT LIMITATION ANY IMPLIED WARRANTIES OR CONDITIONS OF TITLE,
|
10
|
+
# FITNESS FOR A PARTICULAR PURPOSE, MERCHANTABLITY OR NON-INFRINGEMENT.
|
11
|
+
|
12
|
+
# See the Apache Version 2.0 License for specific language governing
|
13
|
+
# permissions and limitations under the License.
|
14
|
+
module Yammer
|
15
|
+
module Resources
|
16
|
+
class User < Yammer::Resources::Base
|
17
|
+
|
18
|
+
# Returns authenticated user's details
|
19
|
+
# @!scope class
|
20
|
+
def self.current
|
21
|
+
result = api_handler.current_user
|
22
|
+
return nil unless result.success?
|
23
|
+
new(result.body)
|
24
|
+
end
|
25
|
+
|
26
|
+
# Creates a new user from email address
|
27
|
+
# @!scope class
|
28
|
+
def self.create(email)
|
29
|
+
result = api_handler.create_user(:email => email)
|
30
|
+
return nil unless result.created?
|
31
|
+
id = result.headers[:location].split('/').last.to_i
|
32
|
+
new(:id => id)
|
33
|
+
end
|
34
|
+
|
35
|
+
attr_accessor_deffered :first_name, :last_name, :full_name, :hire_date, :mugshot, :state,
|
36
|
+
:type, :admin, :verified_admin, :expertise, :birth_date, :stats, :show_ask_for_photo, :job_title,
|
37
|
+
:web_url, :url, :external_urls, :activated_at, :summary, :department, :previous_companies,
|
38
|
+
:follow_general_messages, :schools, :interests, :significant_other, :network_name, :network_id,
|
39
|
+
:can_broadcast, :web_preferences, :network_domains, :location, :contact, :kids_names, :guid,
|
40
|
+
:name, :mugshot_url, :mugshot_url_template, :settings, :timezone
|
41
|
+
|
42
|
+
# Returns user's primary email
|
43
|
+
def email
|
44
|
+
@email ||= begin
|
45
|
+
self.contact[:email_addresses].map do |e|
|
46
|
+
e[:address] if e[:type] == 'primary'
|
47
|
+
end.first
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
# Returns all users that this user is following
|
52
|
+
def following
|
53
|
+
api_handler.users_followed_by(@id)
|
54
|
+
end
|
55
|
+
|
56
|
+
# Returns all user's follwing this user
|
57
|
+
def followers
|
58
|
+
api_handler.users_following(@id)
|
59
|
+
end
|
60
|
+
|
61
|
+
# Updates the user attributes
|
62
|
+
def update!(params)
|
63
|
+
api_handler.update_user(@id, params)
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
@@ -12,16 +12,11 @@
|
|
12
12
|
# See the Apache Version 2.0 License for specific language governing
|
13
13
|
# permissions and limitations under the License.
|
14
14
|
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
@rich = opts.fetch(:rich,'')
|
24
|
-
@urls = opts.fetch(:urls,'')
|
25
|
-
end
|
26
|
-
end
|
27
|
-
end
|
15
|
+
require 'yammer/resources/identity_map'
|
16
|
+
require 'yammer/resources/base'
|
17
|
+
require 'yammer/resources/user'
|
18
|
+
require 'yammer/resources/thread'
|
19
|
+
require 'yammer/resources/message'
|
20
|
+
require 'yammer/resources/message_body'
|
21
|
+
require 'yammer/resources/group'
|
22
|
+
require 'yammer/resources/group_membership'
|
data/lib/yammer/version.rb
CHANGED
@@ -14,8 +14,8 @@
|
|
14
14
|
|
15
15
|
module Yammer
|
16
16
|
class Version
|
17
|
-
MAJOR =
|
18
|
-
MINOR =
|
17
|
+
MAJOR = 2 unless defined? Yammer::MAJOR
|
18
|
+
MINOR = 0 unless defined? Yammer::MINOR
|
19
19
|
PATCH = 0 unless defined? Yammer::PATCH
|
20
20
|
PRE = nil unless defined? Yammer::PRE
|
21
21
|
|
data/lib/yammer.rb
CHANGED
@@ -20,13 +20,7 @@ require 'yammer/http_adapter'
|
|
20
20
|
require 'yammer/client'
|
21
21
|
require 'yammer/api_handler'
|
22
22
|
require 'yammer/api_response'
|
23
|
-
require 'yammer/
|
24
|
-
require 'yammer/base'
|
25
|
-
require 'yammer/user'
|
26
|
-
require 'yammer/group'
|
27
|
-
require 'yammer/group_membership'
|
28
|
-
require 'yammer/message'
|
29
|
-
require 'yammer/thread'
|
23
|
+
require 'yammer/resources'
|
30
24
|
|
31
25
|
module Yammer
|
32
26
|
class << self
|
@@ -19,9 +19,9 @@
|
|
19
19
|
|
20
20
|
require File.expand_path('../../spec_helper', __FILE__)
|
21
21
|
|
22
|
-
describe Yammer::Base do
|
22
|
+
describe Yammer::Resources::Base do
|
23
23
|
|
24
|
-
class DummyModel < Yammer::Base
|
24
|
+
class DummyModel < Yammer::Resources::Base
|
25
25
|
attr_accessor_deffered :first_name, :last_name
|
26
26
|
end
|
27
27
|
|
@@ -19,7 +19,7 @@
|
|
19
19
|
|
20
20
|
require File.expand_path('../../spec_helper', __FILE__)
|
21
21
|
|
22
|
-
describe Yammer::GroupMembership do
|
22
|
+
describe Yammer::Resources::GroupMembership do
|
23
23
|
|
24
24
|
before :all do
|
25
25
|
Yammer.configure do |conf|
|
@@ -33,7 +33,7 @@ describe Yammer::GroupMembership do
|
|
33
33
|
|
34
34
|
context 'class methods' do
|
35
35
|
|
36
|
-
subject { Yammer::GroupMembership }
|
36
|
+
subject { Yammer::Resources::GroupMembership }
|
37
37
|
|
38
38
|
describe '#create_group_membership' do
|
39
39
|
it 'creates a new group membership' do
|
@@ -19,7 +19,7 @@
|
|
19
19
|
|
20
20
|
require File.expand_path('../../spec_helper', __FILE__)
|
21
21
|
|
22
|
-
describe Yammer::Group do
|
22
|
+
describe Yammer::Resources::Group do
|
23
23
|
context 'class methods' do
|
24
24
|
|
25
25
|
before :all do
|
@@ -32,7 +32,7 @@ describe Yammer::Group do
|
|
32
32
|
Yammer.reset!
|
33
33
|
end
|
34
34
|
|
35
|
-
subject { Yammer::Group }
|
35
|
+
subject { Yammer::Resources::Group }
|
36
36
|
|
37
37
|
describe '#create' do
|
38
38
|
it 'creates a new group' do
|
@@ -17,12 +17,12 @@
|
|
17
17
|
# See the Apache Version 2.0 License for specific language governing
|
18
18
|
# permissions and limitations under the License.
|
19
19
|
|
20
|
-
require File.expand_path('
|
21
|
-
require 'yammer/identity_map'
|
20
|
+
require File.expand_path('../../spec_helper', __FILE__)
|
21
|
+
require 'yammer/resources/identity_map'
|
22
22
|
|
23
|
-
describe Yammer::IdentityMap do
|
23
|
+
describe Yammer::Resources::IdentityMap do
|
24
24
|
|
25
|
-
subject { Yammer::IdentityMap.new }
|
25
|
+
subject { Yammer::Resources::IdentityMap.new }
|
26
26
|
|
27
27
|
after :each do
|
28
28
|
subject.purge!
|
@@ -54,13 +54,13 @@ describe Yammer::IdentityMap do
|
|
54
54
|
describe '#put' do
|
55
55
|
context 'with nil key' do
|
56
56
|
it 'should throw exception' do
|
57
|
-
expect { subject.put(nil, 'value') }.to raise_error(Yammer::IdentityMap::InvalidKeyError)
|
57
|
+
expect { subject.put(nil, 'value') }.to raise_error(Yammer::Resources::IdentityMap::InvalidKeyError)
|
58
58
|
end
|
59
59
|
end
|
60
60
|
|
61
61
|
context 'with empty string' do
|
62
62
|
it 'should throw exception' do
|
63
|
-
expect { subject.put('', 'value') }.to raise_error(Yammer::IdentityMap::InvalidKeyError)
|
63
|
+
expect { subject.put('', 'value') }.to raise_error(Yammer::Resources::IdentityMap::InvalidKeyError)
|
64
64
|
end
|
65
65
|
end
|
66
66
|
|
@@ -19,7 +19,7 @@
|
|
19
19
|
|
20
20
|
require File.expand_path('../../spec_helper', __FILE__)
|
21
21
|
|
22
|
-
describe Yammer::Message do
|
22
|
+
describe Yammer::Resources::Message do
|
23
23
|
|
24
24
|
before :all do
|
25
25
|
Yammer.configure do |conf|
|
@@ -33,7 +33,7 @@ describe Yammer::Message do
|
|
33
33
|
|
34
34
|
context 'class methods' do
|
35
35
|
|
36
|
-
subject { Yammer::Message }
|
36
|
+
subject { Yammer::Resources::Message }
|
37
37
|
|
38
38
|
describe '#create' do
|
39
39
|
it 'creates a new group' do
|
@@ -19,7 +19,7 @@
|
|
19
19
|
|
20
20
|
require File.expand_path('../../spec_helper', __FILE__)
|
21
21
|
|
22
|
-
describe Yammer::Thread do
|
22
|
+
describe Yammer::Resources::Thread do
|
23
23
|
|
24
24
|
before :all do
|
25
25
|
Yammer.configure do |conf|
|
@@ -31,10 +31,10 @@ describe Yammer::Thread do
|
|
31
31
|
Yammer.reset!
|
32
32
|
end
|
33
33
|
|
34
|
-
subject { Yammer::GroupMembership }
|
34
|
+
subject { Yammer::Resources::GroupMembership }
|
35
35
|
|
36
36
|
context 'existing public thread' do
|
37
|
-
subject { Yammer::Thread.new(MultiJson.load(fixture("public_thread.json"), :symbolize_keys => true)) }
|
37
|
+
subject { Yammer::Resources::Thread.new(MultiJson.load(fixture("public_thread.json"), :symbolize_keys => true)) }
|
38
38
|
|
39
39
|
describe "#id" do
|
40
40
|
it 'returns id' do
|
@@ -44,14 +44,14 @@ describe Yammer::Thread do
|
|
44
44
|
|
45
45
|
describe "#first_reply" do
|
46
46
|
it 'returns first_reply' do
|
47
|
-
expect(subject.first_reply).to be_instance_of(Yammer::Message)
|
47
|
+
expect(subject.first_reply).to be_instance_of(Yammer::Resources::Message)
|
48
48
|
expect(subject.first_reply.id).to eq 11
|
49
49
|
end
|
50
50
|
end
|
51
51
|
|
52
52
|
describe "#last_reply" do
|
53
53
|
it 'returns last_reply' do
|
54
|
-
expect(subject.last_reply).to be_instance_of(Yammer::Message)
|
54
|
+
expect(subject.last_reply).to be_instance_of(Yammer::Resources::Message)
|
55
55
|
expect(subject.last_reply.id).to eq 13
|
56
56
|
end
|
57
57
|
end
|
@@ -79,11 +79,11 @@ describe Yammer::Thread do
|
|
79
79
|
end
|
80
80
|
|
81
81
|
context 'existing private thread' do
|
82
|
-
subject { Yammer::Thread.new(MultiJson.load(fixture("private_thread.json"), :symbolize_keys => true)) }
|
82
|
+
subject { Yammer::Resources::Thread.new(MultiJson.load(fixture("private_thread.json"), :symbolize_keys => true)) }
|
83
83
|
describe "people" do
|
84
84
|
it 'makes an http request and hydrates object' do
|
85
85
|
subject.people.each do |person|
|
86
|
-
expect(person).to be_instance_of(Yammer::User)
|
86
|
+
expect(person).to be_instance_of(Yammer::Resources::User)
|
87
87
|
end
|
88
88
|
end
|
89
89
|
end
|
@@ -19,7 +19,7 @@
|
|
19
19
|
|
20
20
|
require File.expand_path('../../spec_helper', __FILE__)
|
21
21
|
|
22
|
-
describe Yammer::User do
|
22
|
+
describe Yammer::Resources::User do
|
23
23
|
|
24
24
|
before :all do
|
25
25
|
Yammer.configure do |conf|
|
@@ -33,7 +33,7 @@ describe Yammer::User do
|
|
33
33
|
|
34
34
|
context 'class methods' do
|
35
35
|
|
36
|
-
subject { Yammer::User }
|
36
|
+
subject { Yammer::Resources::User }
|
37
37
|
|
38
38
|
describe '#create' do
|
39
39
|
it 'creates a new user' do
|
@@ -75,10 +75,10 @@ describe Yammer::User do
|
|
75
75
|
context 'new user object with id' do
|
76
76
|
|
77
77
|
before :each do
|
78
|
-
Yammer::User.identity_map.purge!
|
78
|
+
Yammer::Resources::User.identity_map.purge!
|
79
79
|
end
|
80
80
|
|
81
|
-
subject { Yammer::User.new(:id => 1) }
|
81
|
+
subject { Yammer::Resources::User.new(:id => 1) }
|
82
82
|
|
83
83
|
describe "#id" do
|
84
84
|
it 'returns id' do
|
@@ -105,7 +105,7 @@ describe Yammer::User do
|
|
105
105
|
|
106
106
|
describe "creating duplicate object" do
|
107
107
|
it 'retrieves data from identitymap' do
|
108
|
-
user = Yammer::User.new(:id => 1)
|
108
|
+
user = Yammer::Resources::User.new(:id => 1)
|
109
109
|
stub_request(:get, "https://www.yammer.com/api/v1/users/1").with(
|
110
110
|
:headers => {
|
111
111
|
'Accept' => 'application/json',
|
@@ -119,14 +119,14 @@ describe Yammer::User do
|
|
119
119
|
)
|
120
120
|
expect(user.full_name).to eq 'John Smith'
|
121
121
|
|
122
|
-
duplicate = Yammer::User.new(:id => 1)
|
122
|
+
duplicate = Yammer::Resources::User.new(:id => 1)
|
123
123
|
expect(duplicate.full_name).to eq 'John Smith'
|
124
124
|
end
|
125
125
|
end
|
126
126
|
end
|
127
127
|
|
128
128
|
context 'hydrated user object' do
|
129
|
-
subject { Yammer::User.new(MultiJson.load(fixture('user.json'), :symbolize_keys => true)) }
|
129
|
+
subject { Yammer::Resources::User.new(MultiJson.load(fixture('user.json'), :symbolize_keys => true)) }
|
130
130
|
|
131
131
|
describe "#id" do
|
132
132
|
it 'returns id' do
|
data.tar.gz.sig
CHANGED
Binary file
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: yam
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 2.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Kevin Mutyaba
|
@@ -182,19 +182,20 @@ files:
|
|
182
182
|
- lib/yammer/api/user.rb
|
183
183
|
- lib/yammer/api_handler.rb
|
184
184
|
- lib/yammer/api_response.rb
|
185
|
-
- lib/yammer/base.rb
|
186
185
|
- lib/yammer/client.rb
|
187
186
|
- lib/yammer/configurable.rb
|
188
187
|
- lib/yammer/error.rb
|
189
|
-
- lib/yammer/group.rb
|
190
|
-
- lib/yammer/group_membership.rb
|
191
188
|
- lib/yammer/http_adapter.rb
|
192
|
-
- lib/yammer/
|
193
|
-
- lib/yammer/
|
194
|
-
- lib/yammer/
|
195
|
-
- lib/yammer/
|
196
|
-
- lib/yammer/
|
197
|
-
- lib/yammer/
|
189
|
+
- lib/yammer/resources.rb
|
190
|
+
- lib/yammer/resources/base.rb
|
191
|
+
- lib/yammer/resources/group.rb
|
192
|
+
- lib/yammer/resources/group_membership.rb
|
193
|
+
- lib/yammer/resources/identity_map.rb
|
194
|
+
- lib/yammer/resources/message.rb
|
195
|
+
- lib/yammer/resources/message_body.rb
|
196
|
+
- lib/yammer/resources/pending_attachment.rb
|
197
|
+
- lib/yammer/resources/thread.rb
|
198
|
+
- lib/yammer/resources/user.rb
|
198
199
|
- lib/yammer/version.rb
|
199
200
|
- spec/api/activity_spec.rb
|
200
201
|
- spec/api/autocomplete_spec.rb
|
@@ -225,14 +226,14 @@ files:
|
|
225
226
|
- spec/fixtures/users_followed.json
|
226
227
|
- spec/fixtures/users_following.json
|
227
228
|
- spec/http_adapter_spec.rb
|
228
|
-
- spec/identity_map_spec.rb
|
229
229
|
- spec/mocks/attachment.txt
|
230
|
-
- spec/
|
231
|
-
- spec/
|
232
|
-
- spec/
|
233
|
-
- spec/
|
234
|
-
- spec/
|
235
|
-
- spec/
|
230
|
+
- spec/resources/base_spec.rb
|
231
|
+
- spec/resources/group_membership_spec.rb
|
232
|
+
- spec/resources/group_spec.rb
|
233
|
+
- spec/resources/identity_map_spec.rb
|
234
|
+
- spec/resources/message_spec.rb
|
235
|
+
- spec/resources/thread_spec.rb
|
236
|
+
- spec/resources/user_spec.rb
|
236
237
|
- spec/spec_helper.rb
|
237
238
|
- yam.gemspec
|
238
239
|
homepage: http://yammer.github.io/yam
|
@@ -289,12 +290,12 @@ test_files:
|
|
289
290
|
- spec/fixtures/users_followed.json
|
290
291
|
- spec/fixtures/users_following.json
|
291
292
|
- spec/http_adapter_spec.rb
|
292
|
-
- spec/identity_map_spec.rb
|
293
293
|
- spec/mocks/attachment.txt
|
294
|
-
- spec/
|
295
|
-
- spec/
|
296
|
-
- spec/
|
297
|
-
- spec/
|
298
|
-
- spec/
|
299
|
-
- spec/
|
294
|
+
- spec/resources/base_spec.rb
|
295
|
+
- spec/resources/group_membership_spec.rb
|
296
|
+
- spec/resources/group_spec.rb
|
297
|
+
- spec/resources/identity_map_spec.rb
|
298
|
+
- spec/resources/message_spec.rb
|
299
|
+
- spec/resources/thread_spec.rb
|
300
|
+
- spec/resources/user_spec.rb
|
300
301
|
- spec/spec_helper.rb
|
metadata.gz.sig
CHANGED
Binary file
|
data/lib/yammer/base.rb
DELETED
@@ -1,208 +0,0 @@
|
|
1
|
-
# Copyright (c) Microsoft Corporation
|
2
|
-
# All rights reserved.
|
3
|
-
# Licensed under the Apache License, Version 2.0 (the "License");
|
4
|
-
# you may not use this file except in compliance with the License.
|
5
|
-
# You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
|
6
|
-
#
|
7
|
-
# THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR
|
8
|
-
# CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING
|
9
|
-
# WITHOUT LIMITATION ANY IMPLIED WARRANTIES OR CONDITIONS OF TITLE,
|
10
|
-
# FITNESS FOR A PARTICULAR PURPOSE, MERCHANTABLITY OR NON-INFRINGEMENT.
|
11
|
-
|
12
|
-
# See the Apache Version 2.0 License for specific language governing
|
13
|
-
# permissions and limitations under the License.
|
14
|
-
|
15
|
-
module Yammer
|
16
|
-
class Base
|
17
|
-
class << self
|
18
|
-
include ApiHandler
|
19
|
-
|
20
|
-
# Returns the non-qualified class name
|
21
|
-
# @!scope class
|
22
|
-
def base_name
|
23
|
-
@base_name ||= begin
|
24
|
-
word = "#{name.split(/::/).last}"
|
25
|
-
word.gsub!(/::/, '/')
|
26
|
-
word.gsub!(/([A-Z]+)([A-Z][a-z])/,'\1_\2')
|
27
|
-
word.gsub!(/([a-z\d])([A-Z])/,'\1_\2')
|
28
|
-
word.tr!("-", "_")
|
29
|
-
word.downcase!
|
30
|
-
word
|
31
|
-
end
|
32
|
-
end
|
33
|
-
|
34
|
-
# Fetches JSON reprsentation for object model with provided `id`
|
35
|
-
# and returns a model instance with attributes
|
36
|
-
# @return [Yammer::Base]
|
37
|
-
# @param id [Integer]
|
38
|
-
# @!scope class
|
39
|
-
def get(id)
|
40
|
-
attrs = fetch(id)
|
41
|
-
attrs ? new(attrs) : nil
|
42
|
-
end
|
43
|
-
|
44
|
-
|
45
|
-
# @!scope class
|
46
|
-
def fetch(id)
|
47
|
-
return unless identity_map
|
48
|
-
attributes = identity_map.get("#{base_name}_#{id}")
|
49
|
-
unless attributes
|
50
|
-
result = api_handler.send("get_#{base_name}", id)
|
51
|
-
attributes = result.empty? ? nil : result.body
|
52
|
-
unless attributes.empty?
|
53
|
-
identity_map.put("#{base_name}_#{id}", attributes)
|
54
|
-
end
|
55
|
-
end
|
56
|
-
attributes
|
57
|
-
end
|
58
|
-
|
59
|
-
# @!scope class
|
60
|
-
def identity_map
|
61
|
-
@identity_map ||= Yammer::IdentityMap.new
|
62
|
-
end
|
63
|
-
|
64
|
-
# Returns a hash of all attributes that are meant to trigger an HTTP request
|
65
|
-
# @!scope class
|
66
|
-
def model_attributes
|
67
|
-
@model_attributes ||= {}
|
68
|
-
end
|
69
|
-
|
70
|
-
protected
|
71
|
-
|
72
|
-
def attr_accessor_deffered(*symbols)
|
73
|
-
symbols.each do |key|
|
74
|
-
# track attributes that should trigger a fetch
|
75
|
-
model_attributes[key] = false
|
76
|
-
|
77
|
-
# getter
|
78
|
-
define_method(key.to_s) do
|
79
|
-
load_deferred_attribute!(key)
|
80
|
-
instance_variable_get("@#{key}")
|
81
|
-
end
|
82
|
-
|
83
|
-
# setter
|
84
|
-
define_method("#{key}=") do |value|
|
85
|
-
load_deferred_attribute!(key)
|
86
|
-
if persisted? && loaded?
|
87
|
-
@modified_attributes[key] = value
|
88
|
-
else
|
89
|
-
@attrs[key] = value
|
90
|
-
end
|
91
|
-
instance_variable_set("@#{key}", value)
|
92
|
-
end
|
93
|
-
end
|
94
|
-
end
|
95
|
-
end
|
96
|
-
|
97
|
-
attr_reader :id, :attrs
|
98
|
-
|
99
|
-
def initialize(props={})
|
100
|
-
@klass = self.class
|
101
|
-
@modified_attributes = {}
|
102
|
-
@new_record = true
|
103
|
-
@loaded = false
|
104
|
-
@attrs = props
|
105
|
-
self.id = @attrs.delete(:id)
|
106
|
-
self.update(@attrs)
|
107
|
-
|
108
|
-
yield self if block_given?
|
109
|
-
end
|
110
|
-
|
111
|
-
def api_handler
|
112
|
-
@klass.api_handler
|
113
|
-
end
|
114
|
-
|
115
|
-
def base_name
|
116
|
-
@klass.base_name
|
117
|
-
end
|
118
|
-
|
119
|
-
def new_record?
|
120
|
-
@new_record
|
121
|
-
end
|
122
|
-
|
123
|
-
def persisted?
|
124
|
-
!new_record?
|
125
|
-
end
|
126
|
-
|
127
|
-
def changes
|
128
|
-
@modified_attributes
|
129
|
-
end
|
130
|
-
|
131
|
-
def modified?
|
132
|
-
!changes.empty?
|
133
|
-
end
|
134
|
-
|
135
|
-
def loaded?
|
136
|
-
@loaded
|
137
|
-
end
|
138
|
-
|
139
|
-
def load!
|
140
|
-
@attrs = @klass.fetch(@id)
|
141
|
-
@loaded = true
|
142
|
-
update(@attrs)
|
143
|
-
self
|
144
|
-
end
|
145
|
-
|
146
|
-
def reload!
|
147
|
-
reset!
|
148
|
-
load!
|
149
|
-
end
|
150
|
-
|
151
|
-
def save
|
152
|
-
return self if ((persisted? && @modified_attributes.empty?) || @attrs.empty?)
|
153
|
-
|
154
|
-
result = if new_record?
|
155
|
-
api_handler.send("create_#{base_name}", @attrs)
|
156
|
-
else
|
157
|
-
api_handler.send("update_#{base_name}", @id, @modified_attributes)
|
158
|
-
end
|
159
|
-
@modified_attributes = {}
|
160
|
-
self
|
161
|
-
end
|
162
|
-
|
163
|
-
def delete!
|
164
|
-
return if new_record?
|
165
|
-
result = api_handler.send("delete_#{base_name}", @id)
|
166
|
-
result.success?
|
167
|
-
end
|
168
|
-
|
169
|
-
private
|
170
|
-
|
171
|
-
def id=(model_id)
|
172
|
-
return if model_id.nil?
|
173
|
-
@id = model_id.to_i
|
174
|
-
@new_record = false
|
175
|
-
end
|
176
|
-
|
177
|
-
# clear the entire class
|
178
|
-
def reset!
|
179
|
-
@modified_attributes = {}
|
180
|
-
@attrs = {}
|
181
|
-
@new_record = true
|
182
|
-
@loaded = false
|
183
|
-
end
|
184
|
-
|
185
|
-
protected
|
186
|
-
# loads model
|
187
|
-
def load_deferred_attribute!(key)
|
188
|
-
if @attrs.empty? && persisted? && !loaded?
|
189
|
-
load!
|
190
|
-
if !@attrs.has_key?(key)
|
191
|
-
raise "The key: #{key} appears not to be supported for model: #{self.base_name} \n #{@attrs.keys.inspect}"
|
192
|
-
end
|
193
|
-
end
|
194
|
-
end
|
195
|
-
|
196
|
-
# set all fetchable attributes
|
197
|
-
def update(attrs={})
|
198
|
-
attrs.each do |key, value|
|
199
|
-
send("#{key}=", value)
|
200
|
-
end
|
201
|
-
if persisted? && !loaded?
|
202
|
-
@loaded = @klass.model_attributes.keys.inject(true) do |result, key|
|
203
|
-
result && @attrs.has_key?(key)
|
204
|
-
end
|
205
|
-
end
|
206
|
-
end
|
207
|
-
end
|
208
|
-
end
|
data/lib/yammer/identity_map.rb
DELETED
@@ -1,56 +0,0 @@
|
|
1
|
-
# Copyright (c) Microsoft Corporation
|
2
|
-
# All rights reserved.
|
3
|
-
# Licensed under the Apache License, Version 2.0 (the "License");
|
4
|
-
# you may not use this file except in compliance with the License.
|
5
|
-
# You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
|
6
|
-
#
|
7
|
-
# THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR
|
8
|
-
# CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING
|
9
|
-
# WITHOUT LIMITATION ANY IMPLIED WARRANTIES OR CONDITIONS OF TITLE,
|
10
|
-
# FITNESS FOR A PARTICULAR PURPOSE, MERCHANTABLITY OR NON-INFRINGEMENT.
|
11
|
-
|
12
|
-
# See the Apache Version 2.0 License for specific language governing
|
13
|
-
# permissions and limitations under the License.
|
14
|
-
|
15
|
-
module Yammer
|
16
|
-
class IdentityMap
|
17
|
-
|
18
|
-
class InvalidKeyError < StandardError; end
|
19
|
-
|
20
|
-
def initialize
|
21
|
-
@map = {}
|
22
|
-
@size = 0
|
23
|
-
end
|
24
|
-
|
25
|
-
# @note retrives key from identity map
|
26
|
-
# @return [Hash]
|
27
|
-
# @param key [string]
|
28
|
-
# @param default [Hash]
|
29
|
-
def get(key, default=nil)
|
30
|
-
@map["#{key}"] || default
|
31
|
-
end
|
32
|
-
|
33
|
-
# @note inserts a hash of attributes into identity map
|
34
|
-
# @return [Hash]
|
35
|
-
# @param key [string]
|
36
|
-
# @param value [Hash]
|
37
|
-
def put(key, value)
|
38
|
-
if key.nil? || key.empty?
|
39
|
-
raise InvalidKeyError.new
|
40
|
-
end
|
41
|
-
@map["#{key}"] = value
|
42
|
-
end
|
43
|
-
|
44
|
-
# @note returns the current size of identity map
|
45
|
-
# @return [Integer]
|
46
|
-
def size
|
47
|
-
@map.keys.count
|
48
|
-
end
|
49
|
-
|
50
|
-
# clears the entire identity map
|
51
|
-
# @return [Hash]
|
52
|
-
def purge!
|
53
|
-
@map = {}
|
54
|
-
end
|
55
|
-
end
|
56
|
-
end
|