Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Allow passing in of options instead of just assuming yaml file in cwd

Signed-off-by: Jim Patterson <jimp79@gmail.com>
  • Loading branch information...
commit d9b7d0179662a85dc4f95ef5b71a1757182967b9 1 parent f423171
@jstewart jstewart authored jpatterson committed
View
4 .gitignore
@@ -1 +1,3 @@
-oauth.yml
+*.swp
+*.swo
+oauth.yml
View
30 ext/core_ext.rb
@@ -0,0 +1,30 @@
+class String
+ def to_boolean
+ case self
+ when 'true'
+ true
+ when 'false'
+ false
+ else
+ nil
+ end
+ end
+end
+
+class Hash
+ def symbolize_keys
+ inject({}) do |options, (key, value)|
+ options[(key.to_sym rescue key) || key] = value
+ options
+ end
+ end
+
+ def symbolize_keys!
+ self.replace(self.symbolize_keys)
+ end
+
+ def assert_has_keys(*valid_keys)
+ missing_keys = [valid_keys].flatten - keys
+ raise(ArgumentError, "Missing Option(s): #{missing_keys.join(", ")}") unless missing_keys.empty?
+ end
+end
View
12 ext/stdlib.rb
@@ -1,12 +0,0 @@
-class String
- def to_boolean
- case self
- when 'true'
- true
- when 'false'
- false
- else
- nil
- end
- end
-end
View
4 yammer.rb
@@ -11,8 +11,8 @@ def require_local(suffix)
require('json')
require('oauth/consumer')
-require_local('ext/stdlib')
+require_local('ext/core_ext')
require_local('yammer/client')
require_local('yammer/message')
require_local('yammer/message_list')
-require_local('yammer/user')
+require_local('yammer/user')
View
54 yammer/client.rb
@@ -1,26 +1,30 @@
class Yammer::Client
-
- URL = 'https://www.yammer.com'
-
- attr_reader :access_token
-
- def initialize
- config = YAML.load(open('oauth.yml'))
- @consumer = OAuth::Consumer.new(config['consumer']['key'],config['consumer']['secret'],:site => URL)
- @token = config['access']['token']
- @secret = config['access']['secret']
- @access_token = OAuth::AccessToken.new(@consumer,@token,@secret)
+
+ URL = 'https://www.yammer.com'
+
+ attr_reader :access_token
+
+ def initialize(options={})
+ options.assert_has_keys(:consumer, :access) unless options.has_key?(:yaml_file)
+ if options[:yaml_file]
+ config = YAML.load(open(options[:yaml_file]))
+ options[:consumer] = config['consumer'].symbolize_keys
+ options[:access] = config['access'].symbolize_keys
+ end
+
+ consumer = OAuth::Consumer.new(options[:consumer][:key], options[:consumer][:secret], :site => options[:site] || URL)
+ @access_token = OAuth::AccessToken.new(consumer, options[:access][:key], options[:access][:secret])
end
-
+
def messages(action = :all)
response = case action
- when :all:
- @access_token.get "/api/v1/messages.json"
- when :sent, :received, :following:
- @access_token.get "/api/v1/messages/#{action}.json"
- else
- raise ArgumentError, "Invalid messaging action: #{action}"
- end
+ when :all:
+ @access_token.get "/api/v1/messages.json"
+ when :sent, :received, :following:
+ @access_token.get "/api/v1/messages/#{action}.json"
+ else
+ raise ArgumentError, "Invalid messaging action: #{action}"
+ end
parsed_response = JSON.parse(response.body)
older_available = parsed_response['meta']['older_available']
ml = parsed_response['messages'].map do |m|
@@ -28,29 +32,29 @@ def messages(action = :all)
end
Yammer::MessageList.new(ml, older_available, self)
end
-
+
def users
response = @access_token.get "/api/v1/users.json"
JSON.parse(response.body).map do |u|
Yammer::User.new(u, self)
end
end
-
+
def user(id)
response = @access_token.get "/api/v1/users/#{id}.json"
u = JSON.parse(response.body)
Yammer::User.new(u, self)
end
-
+
def me
@me ||= current_user
end
-
+
private
def current_user
response = @access_token.get "/api/v1/users/current.json"
u = JSON.parse(response.body)
Yammer::User.new(u, self)
end
-
-end
+
+end
Please sign in to comment.
Something went wrong with that request. Please try again.