yammer-client 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
data.tar.gz.sig CHANGED
Binary file
data/Gemfile.lock ADDED
@@ -0,0 +1,61 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ yammer-client (0.1.2)
5
+ addressable (~> 2.3.3)
6
+ multi_json (~> 1.3)
7
+ oauth2-client (~> 1.1.2)
8
+ oj (~> 2.0.10)
9
+
10
+ GEM
11
+ remote: https://rubygems.org/
12
+ specs:
13
+ addressable (2.3.3)
14
+ bcrypt-ruby (3.0.1)
15
+ colorize (0.5.8)
16
+ coveralls (0.6.0)
17
+ colorize
18
+ multi_json (~> 1.3)
19
+ rest-client
20
+ simplecov (>= 0.7)
21
+ thor
22
+ crack (0.3.2)
23
+ diff-lcs (1.2.1)
24
+ json (1.7.5)
25
+ mime-types (1.21)
26
+ multi_json (1.6.1)
27
+ oauth2-client (1.1.2)
28
+ addressable
29
+ bcrypt-ruby (~> 3.0.0)
30
+ oj (2.0.10)
31
+ rake (10.0.3)
32
+ rest-client (1.6.7)
33
+ mime-types (>= 1.16)
34
+ rspec (2.13.0)
35
+ rspec-core (~> 2.13.0)
36
+ rspec-expectations (~> 2.13.0)
37
+ rspec-mocks (~> 2.13.0)
38
+ rspec-core (2.13.0)
39
+ rspec-expectations (2.13.0)
40
+ diff-lcs (>= 1.1.3, < 2.0)
41
+ rspec-mocks (2.13.0)
42
+ simplecov (0.7.1)
43
+ multi_json (~> 1.0)
44
+ simplecov-html (~> 0.7.1)
45
+ simplecov-html (0.7.1)
46
+ thor (0.17.0)
47
+ webmock (1.10.1)
48
+ addressable (>= 2.2.7)
49
+ crack (>= 0.3.2)
50
+
51
+ PLATFORMS
52
+ ruby
53
+
54
+ DEPENDENCIES
55
+ coveralls
56
+ json
57
+ rake
58
+ rspec (>= 2.11)
59
+ simplecov
60
+ webmock (>= 1.10.1)
61
+ yammer-client!
data/README.md CHANGED
@@ -14,9 +14,11 @@ A Yammer Ruby gem
14
14
 
15
15
  ## Documentation
16
16
 
17
+ This README provides only a basic overview of how to use this gem.For more information about the API endpoints and helper methods available, look at the rdoc documentation.
18
+
17
19
  [http://rdoc.info/github/tiabas/yammer-client][documentation]
18
20
 
19
- [documentation]: http://rdoc.info/github/tiabas/yammer-client
21
+ [documentation]: http://rdoc.info/github/tiabas/yammer-client/index
20
22
 
21
23
 
22
24
  ## Installation
@@ -41,19 +43,11 @@ $ gem install yammer-client
41
43
 
42
44
  ## Configuration
43
45
 
44
- The Yammer API requires authentication for access to certain endpoints. Below are the basic steps to get this done. For more information, take a look at Yammer's Developer Guide <http://developer.yammer.com/files/2012/10/PlatformDeveloperGuide.pdf>
45
-
46
+ The Yammer API requires authentication for access to certain endpoints. Below are the basic steps to get this done.
46
47
 
47
48
  ### Register your application
48
49
 
49
- 1. Sign in to Yammer
50
-
51
- 2. Go to `https://www.yammer.com/client_applications`
52
-
53
- 3. Click on 'Register New App' and fill out the registration form
54
-
55
- 4. Make note of your client_id and client_secret they will be needed for token authorization
56
-
50
+ Setup a Yammer client application as described in [Build your first Yammer App](https://developer.yammer.com/introduction/)
57
51
 
58
52
  ### Obtaining an access token
59
53
 
@@ -197,15 +191,14 @@ yamr.search(:search => 'thekev', :model_types => 'users;groups')
197
191
  *fetch a thread with a given id*
198
192
 
199
193
  ```ruby
200
- yamr.get_thread(42, :model_types => 'users;groups')
194
+ yamr.get_thread(42)
201
195
  #<Yammer::Response:0x007fb949434ec8 @headers=#<Net::HTTPOK 200 OK readbody=true>, @body="[JSON Response]", @code=200>
202
196
  ```
203
197
 
204
198
 
205
- ### Using the object models
199
+ ### Using the object models (Experimental)
206
200
 
207
- Object models are classes that take away the hussle of having to deal with parsing the JSON that Yammer returns for
208
- any given request. Each model has accessor metods for all keys contained in the JSON response for a given model type.
201
+ The object model is an abstraction that makes it easy to manipulate the JSON data return when accessing Yammer's API. Each model has accessor methods for all keys contained in the JSON response for a given model type.
209
202
 
210
203
 
211
204
  **User**
@@ -230,26 +223,28 @@ u.update!(:job_title => 'k0dR')
230
223
 
231
224
  ```ruby
232
225
  t = Yammer::Thread.get(3)
226
+ ```
233
227
 
234
- # view the participants in the thread
228
+ View the participants in the thread
235
229
 
230
+ ```ruby
236
231
  parts = t.participants
237
232
  #> [{:type=>"user", :id=>18}, {:type=>"user", :id=>64}]
233
+ ```
238
234
 
235
+ View the participants in the thread as user object models
239
236
 
240
- # view the participants in the thread as user object models
241
-
237
+ ```ruby
242
238
  peepl = t.people
243
239
  #> [#<Yammer::User:0x007f9f4c086630 @modified_attributes={}, @attrs={}, @new_record=false, @id=18>, #<Yammer::User:0x007f9f4c086568 @modified_attributes={}, @attrs={}, @new_record=false, @id=64>]
240
+ ```
244
241
 
242
+ Object models are lazyly loaded. Calling an accessor on a model will hydrate it
245
243
 
246
- # object models are lazyly loaded
247
-
244
+ ```ruby
248
245
  peepl[0]
249
246
  #> #<Yammer::User:0x007f9f4c086568 @modified_attributes={}, @attrs={}, @new_record=false, @id=18>
250
247
 
251
- # calling an accessor on a model will hydrate it
252
-
253
248
  peepl[0].permalink
254
249
  #> 'thekev'
255
250
 
data/lib/yammer/base.rb CHANGED
@@ -1,77 +1,93 @@
1
1
  module Yammer
2
2
  class Base
3
+ class << self
4
+
5
+ # Returns the non-qualified class name
6
+ # @!scope class
7
+ def base_name
8
+ @base_name ||= begin
9
+ word = "#{name.split(/::/).last}"
10
+ word.gsub!(/::/, '/')
11
+ word.gsub!(/([A-Z]+)([A-Z][a-z])/,'\1_\2')
12
+ word.gsub!(/([a-z\d])([A-Z])/,'\1_\2')
13
+ word.tr!("-", "_")
14
+ word.downcase!
15
+ word
16
+ end
17
+ end
3
18
 
4
- def self.attr_accessor_deffered(*symbols)
5
- symbols.each do |key|
6
- fetchable_keys[key] = true
19
+ # Fetches JSON reprsentation for object model with provided `id`
20
+ # and returns a model instance with attributes
21
+ # @return [Yammer::Base]
22
+ # @param id [Integer]
23
+ # @!scope class
24
+ def get(id)
25
+ attrs = fetch(id)
26
+ attrs ? new(attrs) : nil
27
+ end
7
28
 
8
- define_method (key.to_s) do
9
- if self.class.fetchable_keys.has_key?(key) && !@attrs.has_key?(key) && @id
10
- load!
11
- end
12
- instance_variable_get("@#{key}")
13
- end
14
29
 
15
- define_method ("#{key}=") do |value|
16
- if @attrs.fetch(key, false) && !new_record?
17
- @modified_attributes[key] = value
18
- else
19
- @attrs[key] = value
30
+ # @!scope class
31
+ def fetch(id)
32
+ return unless identity_map
33
+ attributes = identity_map.get("#{base_name}_#{id}")
34
+ unless attributes
35
+ result = Yammer.send("get_#{base_name}", id)
36
+ attributes = result.empty? ? nil : result.body
37
+ unless attributes.empty?
38
+ identity_map.put("#{base_name}_#{id}", attributes)
20
39
  end
21
- instance_variable_set("@#{key}", value)
22
40
  end
41
+ attributes
23
42
  end
24
- end
25
43
 
26
- # @!scope class
27
- def self.fetchable_keys
28
- @fetchable_keys ||= {}
29
- return @fetchable_keys
30
- end
44
+ # @!scope class
45
+ def identity_map
46
+ @identity_map ||= Yammer::IdentityMap.new
47
+ end
31
48
 
32
- # @!scope class
33
- def self.identity_map
34
- @identity_map ||= Yammer::IdentityMap.new
35
- end
49
+ # Returns a hash of all attributes that are meant to trigger an HTTP request
50
+ # @!scope class
51
+ def fetchable_attrs
52
+ @fetchable_keys ||= {}
53
+ end
36
54
 
37
- # @!scope class
38
- def self.base_name
39
- word = "#{self.name.split(/::/).last}"
40
- word.gsub!(/::/, '/')
41
- word.gsub!(/([A-Z]+)([A-Z][a-z])/,'\1_\2')
42
- word.gsub!(/([a-z\d])([A-Z])/,'\1_\2')
43
- word.tr!("-", "_")
44
- word.downcase!
45
- word
46
- end
55
+ protected
56
+
57
+ def attr_accessor_deffered(*symbols)
58
+ symbols.each do |key|
59
+ # track attributes the trigger fetch
60
+ fetchable_attrs[key] = false
61
+
62
+ # getter
63
+ define_method(key.to_s) do
64
+ load_deferred_attribute!(key)
65
+ instance_variable_get("@#{key}")
66
+ end
47
67
 
48
- # @!scope class
49
- def self.fetch(id)
50
- return unless identity_map
51
- attributes = identity_map.get("#{self.base_name}_#{id}")
52
- unless attributes
53
- result = Yammer.send("get_#{self.base_name}", id)
54
- attributes = result.body || {}
55
- unless attributes.empty?
56
- identity_map.put("#{self.base_name}_#{id}", attributes)
68
+ # setter
69
+ define_method("#{key}=") do |value|
70
+ load_deferred_attribute!(key)
71
+ if persisted? && loaded?
72
+ @modified_attributes[key] = value
73
+ else
74
+ @attrs[key] = value
75
+ end
76
+ instance_variable_set("@#{key}", value)
77
+ end
57
78
  end
58
79
  end
59
- attributes
60
- end
61
-
62
- # @!scope class
63
- def self.get(id)
64
- new(fetch(id))
65
80
  end
66
81
 
67
82
  attr_reader :id, :attrs
68
83
 
69
84
  def initialize(props={})
70
85
  @modified_attributes = {}
71
- @attrs = {}
72
- @new_record = true
73
- self.id = props.delete(:id)
74
- self.update(props)
86
+ @new_record = true
87
+ @loaded = false
88
+ @attrs = props
89
+ self.id = @attrs.delete(:id)
90
+ self.update(@attrs)
75
91
  end
76
92
 
77
93
  def base_name
@@ -82,6 +98,10 @@ module Yammer
82
98
  @new_record
83
99
  end
84
100
 
101
+ def persisted?
102
+ !new_record?
103
+ end
104
+
85
105
  def changes
86
106
  @modified_attributes
87
107
  end
@@ -90,18 +110,29 @@ module Yammer
90
110
  !changes.empty?
91
111
  end
92
112
 
113
+ def loaded?
114
+ @loaded
115
+ end
116
+
93
117
  def load!
94
- result = self.class.fetch(@id)
95
- update(result)
118
+ @attrs = self.class.fetch(@id)
119
+ @loaded = true
120
+ update(@attrs)
121
+ self
122
+ end
123
+
124
+ def reload!
125
+ reset!
126
+ load!
96
127
  end
97
- alias_method :reload!, :load!
98
128
 
99
129
  def save
100
- return self if ((!new_record? && @modified_attributes.empty?) || @attrs.empty?)
130
+ return self if ((persisted? && @modified_attributes.empty?) || @attrs.empty?)
131
+
101
132
  result = if new_record?
102
- Yammer.send("create_#{self.base_name}", @attrs)
133
+ Yammer.send("create_#{base_name}", @attrs)
103
134
  else
104
- Yammer.send("update_#{self.base_name}", @id, @modified_attributes)
135
+ Yammer.send("update_#{base_name}", @id, @modified_attributes)
105
136
  end
106
137
  @modified_attributes = {}
107
138
  self
@@ -109,7 +140,7 @@ module Yammer
109
140
 
110
141
  def delete!
111
142
  return if new_record?
112
- result = Yammer.send("delete_#{self.base_name}", @id)
143
+ result = Yammer.send("delete_#{base_name}", @id)
113
144
  result.success?
114
145
  end
115
146
 
@@ -121,11 +152,35 @@ module Yammer
121
152
  @new_record = false
122
153
  end
123
154
 
155
+ # clear the entire class
156
+ def reset!
157
+ @modified_attributes = {}
158
+ @attrs = {}
159
+ @new_record = true
160
+ @loaded = false
161
+ end
162
+
124
163
  protected
125
- def update(props={})
126
- props.each do |key, value|
164
+ # loads model
165
+ def load_deferred_attribute!(key)
166
+ if @attrs.empty? && persisted? && !loaded?
167
+ load!
168
+ if !@attrs.has_key?(key)
169
+ raise "The key: #{key} appears not to be supported for model: #{self.base_name} \n #{@attrs.keys.inspect}"
170
+ end
171
+ end
172
+ end
173
+
174
+ # set all fetchable attributes
175
+ def update(attrs={})
176
+ attrs.each do |key, value|
127
177
  send("#{key}=", value)
128
178
  end
179
+ if persisted? && !loaded?
180
+ @loaded = self.class.fetchable_attrs.keys.inject(true) do |result, key|
181
+ result && @attrs.has_key?(key)
182
+ end
183
+ end
129
184
  end
130
185
  end
131
186
  end
data/lib/yammer/client.rb CHANGED
@@ -15,7 +15,7 @@ module Yammer
15
15
  include Yammer::Api::Network
16
16
  include Yammer::Api::Search
17
17
  include Yammer::Api::Notification
18
- include Yammer::Api::Autocomplete
18
+ include Yammer::Api::Autocomplete
19
19
 
20
20
  attr_writer :connection_options
21
21
  attr_accessor :site_url, :http_adapter
@@ -66,19 +66,19 @@ module Yammer
66
66
 
67
67
  # Makes an HTTP request using the provided parameters
68
68
  # @raise [Yammer::Error::Unauthorized]
69
- # @param method [string]
69
+ # @param method [string]
70
70
  # @param path [string]
71
71
  # @param params [Hash]
72
72
  # @param opts [Hash]
73
73
  # @return [Yammer::Response]
74
74
  # @!visibility private
75
75
  def request(method, path, params={}, opts={})
76
- # Log request here
76
+ headers = opts.fetch(:headers, {}).merge({
77
+ 'Authorization' => "Bearer #{@access_token}"
78
+ })
77
79
  response = connection.send_request(method, path, {
78
80
  :params => params,
79
- :headers => {
80
- 'Authorization' => "Bearer #{@access_token}"
81
- }
81
+ :headers => headers
82
82
  })
83
83
  result = Yammer::Response.new(response)
84
84
  status = result.code
@@ -2,7 +2,7 @@ module Yammer
2
2
  class Version
3
3
  MAJOR = 0 unless defined? Yammer::MAJOR
4
4
  MINOR = 1 unless defined? Yammer::MINOR
5
- PATCH = 1 unless defined? Yammer::PATCH
5
+ PATCH = 2 unless defined? Yammer::PATCH
6
6
  PRE = nil unless defined? Yammer::PRE
7
7
 
8
8
  class << self
@@ -37,7 +37,7 @@ describe Yammer::HttpConnection do
37
37
  describe "#default_headers" do
38
38
  it "returns user_agent and response format" do
39
39
  expect(subject.default_headers).to eq ({
40
- "Accept" => "application/json",
40
+ "Accept" => "application/json",
41
41
  "User-Agent" => "Yammer Ruby Gem #{Yammer::Version}"
42
42
  })
43
43
  end
@@ -172,10 +172,14 @@ describe Yammer::HttpConnection do
172
172
  path = '/oauth/authorize'
173
173
  params = {:client_id => '001337', :client_secret => 'abcxyz'}
174
174
  method = :get
175
-
175
+
176
176
  normalized_path = '/oauth/authorize?client_id=001337&client_secret=abcxyz'
177
177
 
178
- Net::HTTP.any_instance.should_receive(:get).with(normalized_path, subject.default_headers).and_return(@http_ok)
178
+ stub_request(:get, "https://yammer.com/oauth/authorize?client_id=001337&client_secret=abcxyz").with(
179
+ :headers => {
180
+ 'Accept'=>'application/json',
181
+ 'User-Agent'=>"Yammer Ruby Gem #{Yammer::Version}"
182
+ })
179
183
  response = subject.send_request(method, path, :params => params)
180
184
 
181
185
  expect(response.code).to eq '200'
@@ -187,9 +191,13 @@ describe Yammer::HttpConnection do
187
191
  path = '/users/1'
188
192
  method = 'delete'
189
193
 
190
- Net::HTTP.any_instance.should_receive(:delete).with(path, subject.default_headers).and_return(@http_ok)
191
- response = subject.send_request(method, path)
194
+ stub_request(:delete, "https://yammer.com/users/1").with(
195
+ :headers => {
196
+ 'Accept'=>'application/json',
197
+ 'User-Agent'=>"Yammer Ruby Gem #{Yammer::Version}"
198
+ }).to_return(:status => 200, :body => "", :headers => {})
192
199
 
200
+ response = subject.send_request(method, path)
193
201
  expect(response.code).to eq '200'
194
202
  end
195
203
  end
@@ -201,9 +209,18 @@ describe Yammer::HttpConnection do
201
209
  query = Addressable::URI.form_encode(params)
202
210
  headers = {'Content-Type' => 'application/x-www-form-urlencoded' }.merge(subject.default_headers)
203
211
 
204
- Net::HTTP.any_instance.should_receive(:post).with(path, query, headers).and_return(@http_ok)
205
- response =subject.send_request(:post, path, :params => params)
212
+ stub_request(:post, "https://yammer.com/users").with(
213
+ :body => {
214
+ "first_name"=>"john",
215
+ "last_name"=>"smith"
216
+ },
217
+ :headers => {
218
+ 'Accept'=>'application/json',
219
+ 'Content-Type'=>'application/x-www-form-urlencoded',
220
+ 'User-Agent'=>"Yammer Ruby Gem #{Yammer::Version}"
221
+ }).to_return(:status => 200, :body => "", :headers => {})
206
222
 
223
+ response =subject.send_request(:post, path, :params => params)
207
224
  expect(response.code).to eq '200'
208
225
  end
209
226
  end
@@ -215,10 +232,19 @@ describe Yammer::HttpConnection do
215
232
  query = Addressable::URI.form_encode(params)
216
233
  headers = {'Content-Type' => 'application/x-www-form-urlencoded' }.merge(subject.default_headers)
217
234
 
218
- Net::HTTP.any_instance.should_receive(:put).with(path, query, headers).and_return(@http_ok)
235
+ stub_request(:put, "https://yammer.com/users/1").with(
236
+ :body => {
237
+ "first_name" => "jane",
238
+ "last_name" => "doe"
239
+ },
240
+ :headers => {
241
+ 'Accept'=>'application/json',
242
+ 'Content-Type'=>'application/x-www-form-urlencoded',
243
+ 'User-Agent'=>"Yammer Ruby Gem #{Yammer::Version}"
244
+ }).
245
+ to_return(:status => 200, :body => "", :headers => {})
219
246
 
220
247
  response = subject.send_request(:put, path, :params => params)
221
-
222
248
  expect(response.code).to eq '200'
223
249
  end
224
250
  end
@@ -2,7 +2,7 @@ require File.expand_path('../../spec_helper', __FILE__)
2
2
 
3
3
  describe Yammer::Base do
4
4
  class DummyModel < Yammer::Base
5
- attr_accessor_deffered :first_name, :last_name
5
+ attr_accessor_deffered :first_name, :last_name
6
6
  end
7
7
 
8
8
  class Yammer::Client
@@ -123,7 +123,7 @@ describe Yammer::Base do
123
123
  :last_name => 'smith')
124
124
  ).and_return(double('Response', :success? => true, :created? => true, :body => {:id => 2}))
125
125
  subject.first_name = 'john'
126
- subject.last_name = 'smith'
126
+ subject.last_name = 'smith'
127
127
  subject.save
128
128
  end
129
129
  end
@@ -143,9 +143,10 @@ describe Yammer::Base do
143
143
  subject { DummyModel.new(:first_name => 'jim', :last_name => 'peters') }
144
144
 
145
145
  it 'should create model' do
146
- Yammer.should_receive(:create_dummy_model).with(
147
- hash_including(:first_name =>'john', :last_name => 'peters')
148
- ).and_return(double('Response', :success? => true, :created? => true, :body => {:id => 2}))
146
+ Yammer.should_receive(:create_dummy_model).with({
147
+ :first_name =>'john',
148
+ :last_name => 'peters'
149
+ }).and_return(double('Response', :success? => true, :created? => true, :body => {:id => 2}))
149
150
  subject.first_name = 'john'
150
151
  subject.save
151
152
  end
@@ -85,7 +85,7 @@ describe Yammer::User do
85
85
  end
86
86
  end
87
87
 
88
- context 'hygrated user object' do
88
+ context 'hydrated user object' do
89
89
  subject { Yammer::User.new(MultiJson.load(fixture('user.json'), :symbolize_keys => true)) }
90
90
 
91
91
  describe "#id" do
data/yammer.gemspec CHANGED
@@ -5,7 +5,7 @@ require 'yammer/version'
5
5
  Gem::Specification.new do |s|
6
6
  s.name = 'yammer-client'
7
7
  s.version = Yammer::Version
8
- s.date = %q{2013-04-08}
8
+ s.date = %q{2013-04-09}
9
9
  s.summary = "Yammer API Client - beta"
10
10
  s.description = "A Ruby wrapper for accessing Yammer's REST API"
11
11
  s.authors = ["Kevin Mutyaba"]
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: yammer-client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -38,7 +38,7 @@ cert_chain:
38
38
  OGZyVURBOQpqNmZBVGcvNGZxcGdJTFBWcUZJR1pPTUpERmNKeS9vZWh3d3hM
39
39
  dTVYTXg4OFdGRDlqVDF2Umo3N0Q3aVBMYlhkCnJmR3MvcUNKS2dpZlhkLzFh
40
40
  bTVobEFINWpYVT0KLS0tLS1FTkQgQ0VSVElGSUNBVEUtLS0tLQo=
41
- date: 2013-04-08 00:00:00.000000000 Z
41
+ date: 2013-04-09 00:00:00.000000000 Z
42
42
  dependencies:
43
43
  - !ruby/object:Gem::Dependency
44
44
  name: oj
@@ -179,6 +179,7 @@ files:
179
179
  - .yardopts
180
180
  - CHANGELOG.md
181
181
  - Gemfile
182
+ - Gemfile.lock
182
183
  - LICENSE.md
183
184
  - README.md
184
185
  - Rakefile
metadata.gz.sig CHANGED
Binary file