bscofield-stammer 0.0.2 → 0.0.3

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.
data/README.txt CHANGED
@@ -21,7 +21,7 @@ TODO
21
21
  require 'rubygems'
22
22
  require 'stammer'
23
23
 
24
- stammer = Stammer.new('user@example.com', 'password')
24
+ stammer = Stammer::Client.new('user@example.com', 'password')
25
25
  messages = stammer.messages
26
26
  sent_messages = stammer.messages('sent')
27
27
 
@@ -1,9 +1,5 @@
1
1
  # $Id$
2
2
 
3
- # Equivalent to a header guard in C/C++
4
- # Used to prevent the class/module from being loaded more than once
5
- unless defined? Stammer
6
-
7
3
  module Stammer
8
4
 
9
5
  # :stopdoc:
@@ -49,8 +45,4 @@ module Stammer
49
45
 
50
46
  end # module Stammer
51
47
 
52
- Stammer.require_all_libs_relative_to __FILE__
53
-
54
- end # unless defined?
55
-
56
- # EOF
48
+ Stammer.require_all_libs_relative_to __FILE__
@@ -4,11 +4,16 @@ require 'net/https'
4
4
 
5
5
  module Stammer
6
6
  class Client
7
- def initialize(user, password, secret = nil, client = nil, format = 'json')
7
+
8
+ ACCEPTABLE_SUBSETS = [:all, :sent, :received, :following]
9
+ FORMAT = 'json'
10
+ HEADERS = {'User-Agent' => 'Stammer v0.0.3', 'Accept' => "text/#{FORMAT}"}
11
+ API_URL = "https://yammer.com/api/v1"
12
+
13
+ def initialize(user, password, secret = nil, client = nil)
8
14
  @user = user
9
15
  @password = password
10
- @format = format
11
-
16
+
12
17
  unless !secret || !client
13
18
  @secret = secret
14
19
  @client = client
@@ -17,23 +22,54 @@ module Stammer
17
22
  end
18
23
  end
19
24
 
20
- def messages(subset = nil)
21
- MessageList.new(send_request('messages', subset))
25
+ # TODO: pagination via older_than/newer_than
26
+ def messages(subset = :all)
27
+ raise ArgumentError.new("Subset must be in [#{ACCEPTABLE_SUBSETS.join(', ')}]") unless ACCEPTABLE_SUBSETS.include?(subset)
28
+ subset = nil if subset == :all
29
+ MessageList.new(send_request('messages', subset.to_s))
30
+ end
31
+
32
+ # TODO: pagination via page
33
+ def users(id = nil)
34
+ UserList.new(send_request('users', id))
35
+ end
36
+
37
+ def tags(id = nil)
38
+ TagList.new(send_request('tags', id))
39
+ end
40
+
41
+ def update_status(status)
42
+ raise ArgumentError.new("must update with a status") if status.to_s == ""
43
+ url = URI.parse(API_URL + "/messages")
44
+ conn = create_connection(url)
45
+ res = conn.start do |http|
46
+ req = Net::HTTP::Post.new(url.path, HEADERS)
47
+ req.basic_auth(@user, @password)
48
+ req.form_data = {'body' => status}
49
+ http.request(req)
50
+ end
51
+ return res.is_a?(Net::HTTPCreated)
22
52
  end
23
53
 
24
54
  private
55
+
25
56
  def send_request(*args)
26
- url = URI.parse("https://yammer.com/api/v1/#{args.compact.join('/')}.#{@format}")
27
-
28
- conn = Net::HTTP.new(url.host, url.port)
29
- conn.use_ssl = true
30
- conn.verify_mode = OpenSSL::SSL::VERIFY_NONE
31
- res = conn.start { |http|
32
- req = Net::HTTP::Get.new(url.path, {'User-Agent' => 'Stammer v0.0.1', 'Accept' => 'text/json'})
57
+ url = URI.parse("#{API_URL}/#{args.compact.join('/')}.#{FORMAT}")
58
+ puts "grabbing #{url.to_s}"
59
+ conn = create_connection(url)
60
+ res = conn.start do |http|
61
+ req = Net::HTTP::Get.new(url.path, HEADERS)
33
62
  req.basic_auth(@user, @password)
34
63
  http.request(req)
35
- }
64
+ end
36
65
  JSON.load(res.body)
37
66
  end
67
+
68
+ def create_connection(url)
69
+ conn = Net::HTTP.new(url.host, url.port)
70
+ conn.use_ssl = true
71
+ conn.verify_mode = OpenSSL::SSL::VERIFY_NONE
72
+ conn
73
+ end
38
74
  end
39
75
  end
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = "stammer"
3
- s.version = "0.0.2"
3
+ s.version = "0.0.3"
4
4
  s.date = "2008-09-19"
5
5
  s.summary = "Super-simple wrapper for the Yammer API"
6
6
  s.email = "ben.scofield@viget.com"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bscofield-stammer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ben Scofield