Class: Stammer::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/stammer/client.rb

Constant Summary collapse

ACCEPTABLE_SUBSETS =
[:all, :sent, :received, :following]
FORMAT =
'json'
HEADERS =
{'User-Agent' => 'Stammer v0.0.3', 'Accept' => "text/#{FORMAT}"}
API_URL =
"https://yammer.com/api/v1"

Instance Method Summary collapse

Constructor Details

#initialize(user, password, secret = nil, client = nil) ⇒ Client

Returns a new instance of Client.



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/stammer/client.rb', line 13

def initialize(user, password, secret = nil, client = nil)
  @user     = user
  @password = password

  unless !secret || !client
    @secret = secret
    @client = client
  else
    raise Stammer::InvalidCredentials if secret != client
  end
end

Instance Method Details

#messages(subset = :all) ⇒ Object

TODO: pagination via older_than/newer_than

Raises:

  • (ArgumentError)


26
27
28
29
30
# File 'lib/stammer/client.rb', line 26

def messages(subset = :all)
  raise ArgumentError.new("Subset must be in [#{ACCEPTABLE_SUBSETS.join(', ')}]") unless ACCEPTABLE_SUBSETS.include?(subset)
  subset = nil if subset == :all
  MessageList.new(send_request('messages', subset.to_s))
end

#tags(id = nil) ⇒ Object



37
38
39
# File 'lib/stammer/client.rb', line 37

def tags(id = nil)
  TagList.new(send_request('tags', id))
end

#update_status(status) ⇒ Object

Raises:

  • (ArgumentError)


41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/stammer/client.rb', line 41

def update_status(status)
  raise ArgumentError.new("must update with a status") if status.to_s == ""
  url = URI.parse(API_URL + "/messages")
  conn = create_connection(url)
  res = conn.start do |http|
    req = Net::HTTP::Post.new(url.path, HEADERS)
    req.basic_auth(@user, @password)
    req.form_data = {'body' => status}
    http.request(req)
  end
  return res.is_a?(Net::HTTPCreated)
end

#users(id = nil) ⇒ Object

TODO: pagination via page



33
34
35
# File 'lib/stammer/client.rb', line 33

def users(id = nil)
  UserList.new(send_request('users', id))
end