Permalink
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...
1 parent f423171 commit d9b7d0179662a85dc4f95ef5b71a1757182967b9 @jstewart jstewart committed with jpatterson Feb 13, 2009
Showing with 64 additions and 40 deletions.
  1. +3 −1 .gitignore
  2. +30 −0 ext/core_ext.rb
  3. +0 −12 ext/stdlib.rb
  4. +2 −2 yammer.rb
  5. +29 −25 yammer/client.rb
View
@@ -1 +1,3 @@
-oauth.yml
+*.swp
+*.swo
+oauth.yml
View
@@ -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
@@ -1,12 +0,0 @@
-class String
- def to_boolean
- case self
- when 'true'
- true
- when 'false'
- false
- else
- nil
- end
- end
-end
View
@@ -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
@@ -1,56 +1,60 @@
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|
Yammer::Message.new(m)
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

0 comments on commit d9b7d01

Please sign in to comment.