Class: TicketEvolution::Connection

Inherits:
Base
  • Object
show all
Defined in:
lib/ticket_evolution/core/connection.rb

Constant Summary collapse

DEFAULT_SUBDOMAIN =
'api'
DEFAULT_URL_BASE =
'ticketevolution.com'
DEFAULT_PROTOCOL =
'https'
@@oldest_version_in_service =
8
@@default_options =
HashWithIndifferentAccess.new({
  :version => @@oldest_version_in_service,
  :mode => :sandbox,
  :ssl_verify => true,
  :test_responses => false,
  :logger => nil
})
@@expected_options =
[
  'version',
  'mode',
  'token',
  'secret',
  'ssl_verify',
  'test_responses',
  'logger'
]
@@subdomain =
DEFAULT_SUBDOMAIN
@@url_base =
DEFAULT_URL_BASE
@@protocol =
DEFAULT_PROTOCOL
@@adapter =
:net_http

Instance Method Summary collapse

Methods inherited from Base

#method_missing

Constructor Details

#initialize(opts = {}) ⇒ Connection

Returns a new instance of Connection.



38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/ticket_evolution/core/connection.rb', line 38

def initialize(opts = {})
  @adapter = self.class.adapter
  @config = self.class.default_options.merge(opts)
  @config.delete_if{|k, v| ! TicketEvolution::Connection.expected_options.include?(k)}

  # Error Notification
  if @config.keys.sort_by{|x|x} == TicketEvolution::Connection.expected_options.sort_by{|x|x}
    raise InvalidConfiguration.new("Invalid Token Format") unless @config[:token] =~ /^[a-zA-Z0-9]{32}$/
    raise InvalidConfiguration.new("Invalid Secret Format") unless @config[:secret] =~ /^\S{40}$/
    raise InvalidConfiguration.new("Please Use API Version #{TicketEvolution::Connection.oldest_version_in_service} or Above") unless @config[:version] >= TicketEvolution::Connection.oldest_version_in_service
  else
    raise InvalidConfiguration.new("Missing: #{(self.class.expected_options - @config.keys).join(', ')}")
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class TicketEvolution::Base

Instance Method Details

#build_request(method, path, params = nil) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/ticket_evolution/core/connection.rb', line 84

def build_request(method, path, params = nil)
  options = {
    :headers => {
      "X-Signature" => sign(method, self.uri(path), params),
      "X-Token" => @config[:token],
      "Content-Type" => "application/json"
    },
    :ssl => {
      :verify => @config[:ssl_verify]
    }
  }
  options[:params] = params if method == :GET
  options[:headers]["Accept"] = "application/vnd.ticketevolution.api+json; version=#{@config[:version]}" unless @config[:version] > 8
  Faraday.new(self.uri(path), options) do |builder|
    builder.use Faraday::Response::VerboseLogger, self.logger if self.logger.present?
    builder.use FaradayLocalhostHeader, [ /(?:^|\.)lvh\.me$/i, /(?:^|\.)((?:\d+\.){4})xip\.io$/i ]
    builder.adapter @adapter
    builder.options[:timeout] = 120
    builder.options[:open_timeout] = 120
  end
end

#configObject



53
54
55
# File 'lib/ticket_evolution/core/connection.rb', line 53

def config
  @config
end

#loggerObject



106
107
108
# File 'lib/ticket_evolution/core/connection.rb', line 106

def logger
  @config[:logger]
end

#sign(method, path, content = nil) ⇒ Object



74
75
76
77
78
79
80
81
82
# File 'lib/ticket_evolution/core/connection.rb', line 74

def sign(method, path, content = nil)
  d = "#{method} #{process_params(method, path, content).gsub(TicketEvolution::Connection.protocol+'://', '').gsub(/\:\d{2,5}\//, '/')}"
  Base64.encode64(
    OpenSSL::HMAC.digest(
      OpenSSL::Digest::Digest.new('sha256'),
      @config[:secret],
      d
  )).chomp
end

#uri(path) ⇒ Object



66
67
68
69
70
71
72
# File 'lib/ticket_evolution/core/connection.rb', line 66

def uri(path)
  parts = [].tap do |parts|
    parts << self.url
    parts << "/v#{@config[:version]}" if @config[:version] > 8
    parts << path
  end.join
end

#urlObject



57
58
59
60
61
62
63
64
# File 'lib/ticket_evolution/core/connection.rb', line 57

def url
  @url ||= [].tap do |parts|
    parts << TicketEvolution::Connection.protocol
    parts << "://#{ TicketEvolution::Connection.subdomain }."
    parts << "#{@config[:mode]}." if @config[:mode].present? && @config[:mode].to_sym != :production
    parts << TicketEvolution::Connection.url_base
  end.join
end