Module: MailCatcher::API::Client

Defined in:
lib/mailcatcher/api/client.rb

Constant Summary collapse

MESSAGE_ENDPOINT =
'/messages/%{message_id}.json'.freeze
MESSAGE_INDEX_ENDPOINT =
'/messages'.freeze

Class Method Summary collapse

Class Method Details

.delete_allObject



19
20
21
# File 'lib/mailcatcher/api/client.rb', line 19

def self.delete_all
  request(:delete, MESSAGE_INDEX_ENDPOINT)
end

.delete_message(id) ⇒ Object



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

def self.delete_message(id)
  request(:delete, format(MESSAGE_ENDPOINT, message_id: id))
end

.fetch_message(id) ⇒ Object



15
16
17
# File 'lib/mailcatcher/api/client.rb', line 15

def self.fetch_message(id)
  request(:get, format(MESSAGE_ENDPOINT, message_id: id))
end

.fetch_message_indexObject



11
12
13
# File 'lib/mailcatcher/api/client.rb', line 11

def self.fetch_message_index
  request(:get, MESSAGE_INDEX_ENDPOINT)
end

.httpObject



37
38
39
40
# File 'lib/mailcatcher/api/client.rb', line 37

def self.http
  uri = URI(MailCatcher::API.config.server)
  Net::HTTP.new(uri.host, uri.port)
end

.request(method, path) ⇒ Object



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

def self.request(method, path)
  request = case method
            when :get; Net::HTTP::Get.new(path)
            when :delete; Net::HTTP::Delete.new(path)
            else raise NotImplementedError, "Don't know how to request #{method} method"
            end
  response = http.request(request)
  JSON.load(response.body)
end