Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
require "bundler/gem_tasks"
# frozen_string_literal: true

require 'bundler/gem_tasks'
require 'rspec/core/rake_task'

RSpec::Core::RakeTask.new('spec')

# If you want to make this the default task
task default: :spec
task default: :spec
51 changes: 26 additions & 25 deletions lib/mailup.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
require 'oauth2'
require 'multi_json'
require "net/https"
require 'net/https'
require 'json'
require "uri"
require 'uri'

require 'mailup/version'
require 'mailup/errors'
Expand Down Expand Up @@ -48,19 +48,21 @@ class API
# }
# mailup = MailUp::API.new(credentials)
#
def initialize(credentials=nil, debug=false)
def initialize(credentials = nil, debug = false)
@debug = debug
@host = 'https://services.mailup.com'
@path = ''
@credentials = credentials

# Validate the credentials
raise Error.new, 'MailUp credentials missing' if credentials.nil? or !credentials.is_a?(Hash)
[:client_id, :client_secret, :oauth].each do |key|
raise Error.new, "MailUp credentials must include a #{key.to_s} key" unless credentials.has_key?(key)
raise Error.new, 'MailUp credentials missing' if credentials.nil? || !credentials.is_a?(Hash)

%i[client_id client_secret oauth].each do |key|
raise Error.new, "MailUp credentials must include a #{key} key" unless credentials.has_key?(key)
end
raise Error.new, 'MailUp credentials :oauth must be a hash' unless credentials[:oauth].is_a?(Hash)
[:token, :refresh_token, :expires_at].each do |key|

%i[token refresh_token expires_at].each do |key|
raise Error.new, "MailUp credentials :oauth hash must include a #{key.to_s} key" unless credentials[:oauth].has_key?(key)
end

Expand All @@ -69,8 +71,8 @@ def initialize(credentials=nil, debug=false)
credentials[:client_id],
credentials[:client_secret],
site: @host,
authorize_url: "/Authorization/OAuth/LogOn",
token_url: "/Authorization/OAuth/Token",
authorize_url: '/Authorization/OAuth/LogOn',
token_url: '/Authorization/OAuth/Token',
raise_errors: @debug
)

Expand All @@ -92,19 +94,19 @@ def initialize(credentials=nil, debug=false)
# @param [Hash] opts the options to make the request with
#
def request(method, path, opts={}, &block) # :nodoc:
unless @access_token == nil
# Refresh token if needed
@access_token = @access_token.refresh! if @access_token.expired?
# Ensure the body is JSON
opts[:body] = MultiJson.dump(opts[:body]) if opts[:body]
# Set the headers
opts[:headers] ||= {}
opts[:headers].merge!(headers)
# Make the request
req = @access_token.send(method, path, opts)
# Handle the response
handle_response(req)
end
return if @access_token.nil?

# Refresh token if needed
@access_token = @access_token.refresh! if @access_token.expired?
# Ensure the body is JSON
opts[:body] = MultiJson.dump(opts[:body]) if opts[:body]
# Set the headers
opts[:headers] ||= {}
opts[:headers].merge!(headers)
# Make the request
req = @access_token.send(method, path, opts)
# Handle the response
handle_response(req)
end

# Make a request with for Provisioning Calls.
Expand All @@ -119,7 +121,7 @@ def provisioning_request(path, body = nil) # :nodoc:
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_PEER

req = Net::HTTP::Post.new(path, initheader = {'Content-Type' =>'application/json'})
req = Net::HTTP::Post.new(path, initheader = { 'Content-Type' => 'application/json' })
req.basic_auth @credentials[:client_id], @credentials[:client_secret]
req.body = body.to_json

Expand Down Expand Up @@ -224,6 +226,5 @@ def public
def stats
Stats::Base.new self
end

end
end
end
33 changes: 17 additions & 16 deletions lib/mailup/console/base.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
# frozen_string_literal: true

module MailUp
module Console
class Base
attr_accessor :api

def initialize(api) # :nodoc:
@api = api
@api.path = "/API/v#{MailUp::API_VERSION}/Rest/ConsoleService.svc/Console"
@api = api
@api.path = "/API/v#{MailUp::API_VERSION}/Rest/ConsoleService.svc/Console"
end

# Create an email object
#
# @return [MailUp::Console::Email]
# @return [MailUp::Console::Email]
#
# @example
#
Expand All @@ -21,10 +23,10 @@ def email
end

# Create a group object
#
#
# @param [Integer] id The group_id of the group to access.
#
# @return [MailUp::Console::Group]
# @return [MailUp::Console::Group]
#
# @example
#
Expand All @@ -36,7 +38,7 @@ def group(id)

# Create an images object
#
# @return [MailUp::Console::Images]
# @return [MailUp::Console::Images]
#
# @example
#
Expand All @@ -45,12 +47,12 @@ def group(id)
def images
Images.new @api
end

# Create an import object
#
#
# @param [Integer] idImport The ID of the import process.
#
# @return [MailUp::Console::Import]
# @return [MailUp::Console::Import]
#
# @example
#
Expand All @@ -61,10 +63,10 @@ def import(id)
end

# Create a list object
#
#
# @param [Integer] id The list_id of the list to access.
#
# @return [MailUp::Console::List]
# @return [MailUp::Console::List]
#
# @example
#
Expand All @@ -76,7 +78,7 @@ def list(id)

# Create a public methods object
#
# @return [MailUp::Console::List]
# @return [MailUp::Console::List]
#
# @example
#
Expand All @@ -88,7 +90,7 @@ def public

# Create a recipient object
#
# @return [MailUp::Console::Recipient]
# @return [MailUp::Console::Recipient]
#
# @example
#
Expand All @@ -100,7 +102,7 @@ def recipient

# Create a user object
#
# @return [MailUp::Console::User]
# @return [MailUp::Console::User]
#
# @example
#
Expand All @@ -109,7 +111,6 @@ def recipient
def user
User.new @api
end

end
end
end
end
12 changes: 7 additions & 5 deletions lib/mailup/console/email.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module MailUp
module Console
class Email
Expand Down Expand Up @@ -27,16 +29,16 @@ def initialize(api)
# => 1
#
def send(message_id, email)
@api.post("#{@api.path}/Email/Send", body: {:idMessage => message_id, :Email => email})
@api.post("#{@api.path}/Email/Send", body: { idMessage: message_id, Email: email })
end

# Schedules a mailing for immediate sending
#
# @param [Integer] Id Sending.
#
def send_immediate_confirmation(sending_id)
@api.post("#{@api.path}/Email/Sendings/#{sending_id}/Immediate")
end
@api.post("#{@api.path}/Email/Sendings/#{sending_id}/Immediate")
end

# Retrieves the earliest date to schedule the given sending task.
#
Expand All @@ -52,7 +54,7 @@ def get_deferred_confirmation_date(sending_id)
# @param [String] :Date date/time for a deferred sending(should be UTC).
#
def send_deferred_confirmation(sending_id, date = nil)
@api.post("#{@api.path}/Email/Sendings/#{sending_id}/Deferred", body: {'Date' => date})
@api.post("#{@api.path}/Email/Sendings/#{sending_id}/Deferred", body: { 'Date' => date })
end

# Retrieves the list of email messages that are currently queued up for "immediate sending".
Expand All @@ -77,4 +79,4 @@ def get_undefined_confirmation_queque
end
end
end
end
end
5 changes: 3 additions & 2 deletions lib/mailup/console/group.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module MailUp
module Console
class Group
Expand Down Expand Up @@ -119,7 +121,6 @@ def unsubscribe(recipient_id)
def send_message(message_id, params = {})
@api.post("#{@api.path}/Group/#{@id}/Email/#{message_id}/Send", params: params)
end

end
end
end
end
19 changes: 10 additions & 9 deletions lib/mailup/console/images.rb
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
# frozen_string_literal: true

module MailUp
module Console
class Images
attr_accessor :api

def initialize(api)
@api = api
@api = api
end

# Get the list of all shared images for the current console.
#
# @return [Array<String>] An array of Image strings.
#
# @see http://help.mailup.com/display/mailupapi/Console+methods+v1.1#Consolemethodsv1.1-GetSharedImages
#
#
# @example
#
# images = mailup.console.images.list
Expand All @@ -22,7 +24,7 @@ def initialize(api)
def list
@api.get("#{@api.path}/Images")
end

# Add a new image to the shared images list.
#
# @param [Hash] image A hash of image attributes:
Expand All @@ -32,7 +34,7 @@ def list
# @return [Array] An array of Image strings.
#
# @see http://help.mailup.com/display/mailupapi/Console+methods+v1.1#Consolemethodsv1.1-AddSharedImage
#
#
# @example
#
# image = {
Expand All @@ -44,17 +46,17 @@ def list
# => 51
#
def add_image(image)
@api.post("#{@api.path}/Images", body:image)
@api.post("#{@api.path}/Images", body: image)
end

# Delete the image corresponding to the provided full path name.
#
# @param [String] path The path of the image to delete.
#
# @return [Boolean] `true` if successful.
#
# @see http://help.mailup.com/display/mailupapi/Console+methods+v1.1#Consolemethodsv1.1-DeleteImage
#
#
# @example
#
# delete = mailup.console.images.delete_image("#{image_path}")
Expand All @@ -63,7 +65,6 @@ def add_image(image)
def delete_image(path)
@api.delete("#{@api.path}/Images", body: path.to_s)
end

end
end
end
end
5 changes: 3 additions & 2 deletions lib/mailup/console/import.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module MailUp
module Console
class Import
Expand Down Expand Up @@ -40,7 +42,6 @@ def status
def confirmation_email_id
@api.get("#{@api.path}/Import/#{@id}/Sending")
end

end
end
end
end
5 changes: 3 additions & 2 deletions lib/mailup/console/list.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module MailUp
module Console
class List
Expand Down Expand Up @@ -907,7 +909,6 @@ def update_list(params={})
update_params['IdList'] = @id
@api.put("#{@api.path}/User/List/#{@id}", body: update_params)
end

end
end
end
end
Loading