diff --git a/lib/postman/client.rb b/lib/postman/client.rb new file mode 100644 index 0000000..68ff47d --- /dev/null +++ b/lib/postman/client.rb @@ -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 diff --git a/lib/postman/config.rb b/lib/postman/config.rb new file mode 100644 index 0000000..5758a50 --- /dev/null +++ b/lib/postman/config.rb @@ -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 diff --git a/lib/postman/error.rb b/lib/postman/error.rb new file mode 100644 index 0000000..3b6e758 --- /dev/null +++ b/lib/postman/error.rb @@ -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 diff --git a/lib/postman/header_set.rb b/lib/postman/header_set.rb new file mode 100644 index 0000000..ac63a28 --- /dev/null +++ b/lib/postman/header_set.rb @@ -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 diff --git a/lib/postman/message_scope.rb b/lib/postman/message_scope.rb new file mode 100644 index 0000000..64196f5 --- /dev/null +++ b/lib/postman/message_scope.rb @@ -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