Upload files to 'lib/postman'

This commit is contained in:
nikkel 2020-03-20 02:24:35 +07:00
parent 0bfd0c04e0
commit 51395d835a
5 changed files with 184 additions and 0 deletions

61
lib/postman/client.rb Normal file
View File

@ -0,0 +1,61 @@
require 'moonrope_client'
require 'postman/message_scope'
require 'postman/send_message'
require 'postman/send_raw_message'
module Postman
class Client
#
# Create and cache a global instance of client based on the environment variables
# which can be provided. In 90% of cases, Postman will be accessed through this.
#
def self.instance
@instance ||= Client.new(Postman.config.host, Postman.config.server_key)
end
#
# Initialize a new client with the host and API key
#
def initialize(host, server_key)
@host = host
@server_key = server_key
end
#
# Provide a scope to access messages
#
def messages
MessageScope.new(self)
end
#
# Send a message
#
def send_message(&block)
message = SendMessage.new(self)
block.call(message)
message.send!
end
#
# Send a raw message
#
def send_raw_message(&block)
message = SendRawMessage.new(self)
block.call(message)
message.send!
end
#
# Return the backend moonrope instance for this client
#
def moonrope
@moonrope ||= begin
headers= {'X-Server-API-Key' => @server_key}
MoonropeClient::Connection.new(@host, :headers => headers, :ssl => true)
end
end
end
end

15
lib/postman/config.rb Normal file
View File

@ -0,0 +1,15 @@
module Postman
class Config
def host
@host || ENV['POSTMAN_HOST'] || raise(Error, "Host has not been configured. Set it using the `Postman.configure` block or use `POSTMAN_HOST` environment variable.")
end
attr_writer :host
def server_key
@server_key || ENV['POSTMAN_KEY'] || raise(Error, "Server key has not been configured. Set it using the `Postman.configure` block or use `POSTMAN_KEY` environment variable.")
end
attr_writer :server_key
end
end

47
lib/postman/error.rb Normal file
View File

@ -0,0 +1,47 @@
module Postman
#
# A generic error that all errors will inherit from
#
class Error < StandardError
end
#
# Raised when a message cannot be found by its ID
#
class MessageNotFound < Error
def initialize(id)
@id = id
end
def message
"No message found matching ID '#{@id}'"
end
def to_s
message
end
end
#
# Raised when a message cannot be found by its ID
#
class SendError < Error
def initialize(code, error_message)
@code = code
@error_message = error_message
end
attr_reader :code
attr_reader :error_message
def message
"[#{@code}] #{@error_message}"
end
def to_s
message
end
end
end

21
lib/postman/header_set.rb Normal file
View File

@ -0,0 +1,21 @@
module Postman
class HeaderSet
def initialize(headers)
@headers = headers
end
def [](name)
@headers[name.to_s.downcase]
end
def has_key?(key)
@headers.has_key?(name.to_s.downcase)
end
def method_missing(*args, &block)
@headers.send(*args, &block)
end
end
end

View File

@ -0,0 +1,40 @@
require 'postman/message'
module Postman
class MessageScope
attr_reader :client
def initialize(client)
@client = client
@includes = []
end
#
# Add includes to the scope
#
def includes(*includes)
@includes.push(*includes)
self
end
#
# Return the current includes
#
def expansions
if @includes.include?(:all)
true
else
@includes.map(&:to_s)
end
end
#
# Find a given message by its ID
#
def find_by_id(id)
Message.find_with_scope(self, id)
end
end
end