Class: Communicator::Client

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/communicator/client.rb

Defined Under Namespace

Classes: AuthError, InvalidStartingId, ServerError

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.passwordObject

Return configured password for http auth basic or raise an error message if not configured



18
19
20
# File 'lib/communicator/client.rb', line 18

def password
  @password || raise(Communicator::MissingCredentials.new("No Password specified for HTTP AUTH. Please configure using Communicator::Client.password='xyz'"))
end

.usernameObject

Return configured username for http auth basic or raise an error message if not configured



13
14
15
# File 'lib/communicator/client.rb', line 13

def username
  @username || raise(Communicator::MissingCredentials.new("No Username specified for HTTP AUTH. Please configure using Communicator::Client.username='xyz'"))
end

Class Method Details

.credentialsObject

Helper for basic auth in httparty-expected format for request options



23
24
25
# File 'lib/communicator/client.rb', line 23

def credentials
  {:username => username, :password => password}
end

.pullObject



27
28
29
30
31
32
33
34
35
36
# File 'lib/communicator/client.rb', line 27

def pull
  request = get('/messages.json', :query => {:from_id => Communicator::InboundMessage.last_id+1}, :basic_auth => credentials)
  verify_response request.response
  Communicator::InboundMessage.create_from_json_collection!(JSON.parse(request.parsed_response))
  request
  
# Customize error message on HTTP Timeout
rescue Timeout::Error => err
  raise Timeout::Error, "Failed to PULL from #{base_uri} - Request timed out!"
end

.push(from_id = nil) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/communicator/client.rb', line 38

def push(from_id=nil)
  messages = Communicator::OutboundMessage.delivery_collection(from_id)
  request = post("/messages.json", :body => messages.map(&:payload).to_json, :basic_auth => credentials)
  verify_response request.response
  # Everything went fine? Mark the messages as delivered
  messages.each {|m| m.delivered! }
  request

# Retry when server sent a from_id expectation
rescue Communicator::Client::InvalidStartingId => err
  unless from_id
    push(request.response.body)
  else
    raise "Could not agree upon from_id with server!"
  end
  
# Customize error message on HTTP Timeout
rescue Timeout::Error => err
  raise Timeout::Error, "Failed to PUSH to #{base_uri} - Request timed out!"
end

.verify_response(response) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
# File 'lib/communicator/client.rb', line 59

def verify_response(response)
  if response.kind_of?(Net::HTTPSuccess)
    return true
  elsif response.kind_of?(Net::HTTPUnauthorized) or response.kind_of?(Net::HTTPForbidden)
    raise Communicator::Client::AuthError.new("Failed to authenticate!")
  elsif response.kind_of?(Net::HTTPConflict)
    raise Communicator::Client::InvalidStartingId.new("Expected from_id to begin with #{response.body.strip.chomp}")
  elsif response.kind_of?(Net::HTTPServerError)
    raise Communicator::Client::ServerError.new("Request failed with #{response.class}")
  end
end