Class: Trigonal

Inherits:
Object
  • Object
show all
Includes:
Sys
Defined in:
lib/trigonal/errors.rb,
lib/trigonal/trigonal.rb

Defined Under Namespace

Classes: InvalidCredentialsError, OptionsError, UnknownServerError

Constant Summary collapse

SERVER_PROTOCOL =
'http'
SERVER_HOST =
'trigonal.herokuapp.com'
SERVER_PORT =
'80'
SERVER_PATH =
'/api/manifests.json'
LIVE_ENVIRONMENTS =
%w[stage staging production master]

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) {|_self| ... } ⇒ Trigonal

Returns a new instance of Trigonal.

Yields:

  • (_self)

Yield Parameters:

  • _self (Trigonal)

    the object that the method was called on

Raises:



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/trigonal/trigonal.rb', line 17

def initialize(options = {}, &block)
  raise OptionsError unless options[:email]
  raise OptionsError unless (options[:password] || options[:key])

  @email = options[:email]
  @password = options[:password]
  @key = options[:key]

  yield self if block_given?

end

Instance Method Details

#appname(val = nil) ⇒ Object



109
110
111
# File 'lib/trigonal/trigonal.rb', line 109

def appname(val=nil)
  val ? @appname=val : @appname
end

#environment(val = nil) ⇒ Object



105
106
107
# File 'lib/trigonal/trigonal.rb', line 105

def environment(val=nil)
  val ? @environment=val : @environment
end

#get_appnameObject



72
73
74
# File 'lib/trigonal/trigonal.rb', line 72

def get_appname
  @appname
end

#get_environmentObject



80
81
82
# File 'lib/trigonal/trigonal.rb', line 80

def get_environment
  @environment ||= "development"
end

#get_gemsObject



84
85
86
# File 'lib/trigonal/trigonal.rb', line 84

def get_gems
  @gems || get_gems_from_host
end

#get_gems_from_hostObject



88
89
90
91
92
93
94
95
# File 'lib/trigonal/trigonal.rb', line 88

def get_gems_from_host
  Gem::Specification.map do |specification|
    {
      :name => specification.name,
      :version => specification.version.to_s
    }
  end
end

#get_host_nameObject



68
69
70
# File 'lib/trigonal/trigonal.rb', line 68

def get_host_name
  @host_name ||= Host.hostname
end

#get_nameObject



76
77
78
# File 'lib/trigonal/trigonal.rb', line 76

def get_name
  @name
end

#host_name(val = nil) ⇒ Object



113
114
115
# File 'lib/trigonal/trigonal.rb', line 113

def host_name(val=nil)
  val ? @host_name=val : @host_name
end

#host_rubyObject



64
65
66
# File 'lib/trigonal/trigonal.rb', line 64

def host_ruby
  @host_ruby ||= RUBY_DESCRIPTION
end

#host_system_softwareObject



60
61
62
# File 'lib/trigonal/trigonal.rb', line 60

def host_system_software
  @host_system_software ||= Uname.uname.version
end

#live_environment?Boolean

Returns:

  • (Boolean)


97
98
99
# File 'lib/trigonal/trigonal.rb', line 97

def live_environment?
  LIVE_ENVIRONMENTS.include?(get_environment)
end

#name(val = nil) ⇒ Object



101
102
103
# File 'lib/trigonal/trigonal.rb', line 101

def name(val=nil)
  val ? @name=val : @name
end

#post_manifest(options = {}) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/trigonal/trigonal.rb', line 33

def post_manifest(options = {})
  return unless (live_environment? || options[:force])
  response = HTTParty.post(server_url,
    :body => MultiJson.dump({
      :email       => @email,
      :key         => @key,
      :password    => @password,
      :system      => host_system_software,
      :ruby        => host_ruby,
      :hostname    => get_host_name,
      :name        => get_name,
      :appname     => get_appname,
      :environment => get_environment,
      :gems        => get_gems
    }),
    :headers => { 'Content-Type' => 'application/json' }
  ).response
  case response.code
  when /^2\d+/
    return true
  when "401"
    raise Trigonal::InvalidCredentialsError, "Email address and password or key was not accepted"
  else
    raise Trigonal::UnknownServerError, "Unknown upstream server (code #{response.code}) occured"
  end
end

#server_urlObject



29
30
31
# File 'lib/trigonal/trigonal.rb', line 29

def server_url
  "#{SERVER_PROTOCOL}://#{SERVER_HOST}:#{SERVER_PORT}#{SERVER_PATH}"
end