Class: MrMurano::Account

Inherits:
Object
  • Object
show all
Includes:
Http, Verbose
Defined in:
lib/MrMurano/Account.rb

Constant Summary collapse

@@token =

Store the token in a class variable so that we only fetch it once per run session of this tool

nil

Instance Method Summary collapse

Methods included from Verbose

#debug, #error, #outf, #tabularize, #verbose, #warning

Methods included from Http

#curldebug, #delete, #get, #http, #http_reset, #isJSON, #json_opts, #post, #postf, #put, #set_def_headers, #showHttpError, #workit

Instance Method Details

#_loginInfoObject



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/MrMurano/Account.rb', line 64

def _loginInfo
  host = $cfg['net.host']
  user = $cfg['user.name']
  if user.nil? or user.empty? then
    error("No Murano user account found; please login")
    user = ask("User name: ")
    $cfg.set('user.name', user, :user)
  end
  pff = $cfg.file_at('passwords', :user)
  pf = Passwords.new(pff)
  pf.load
  pws = pf.get(host, user)
  if pws.nil? then
    error("Couldn't find password for #{user}")
    pws = ask("Password:  ") { |q| q.echo = "*" }
    pf.set(host, user, pws)
    pf.save
  end
  {
    :email => $cfg['user.name'],
    :password => pws
  }
end

#businessesObject



120
121
122
123
# File 'lib/MrMurano/Account.rb', line 120

def businesses
  _loginInfo if $cfg['user.name'].nil?
  get('user/' + $cfg['user.name'] + '/membership/')
end

#delete_product(modelId) ⇒ Object



136
137
138
139
# File 'lib/MrMurano/Account.rb', line 136

def delete_product(modelId)
  raise "Missing Business ID" if $cfg['business.id'].nil?
  delete('business/' + $cfg['business.id'] + '/product/' + modelId)
end

#delete_solution(apiId) ⇒ Object



153
154
155
156
# File 'lib/MrMurano/Account.rb', line 153

def delete_solution(apiId)
  raise "Missing Business ID" if $cfg['business.id'].nil?
  delete('business/' + $cfg['business.id'] + '/solution/' + apiId)
end

#endPoint(path) ⇒ Object



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

def endPoint(path)
  URI('https://' + $cfg['net.host'] + '/api:1/' + path.to_s)
end

#new_product(name, type = 'onepModel') ⇒ Object

Create a new product in the current business



131
132
133
134
# File 'lib/MrMurano/Account.rb', line 131

def new_product(name, type='onepModel')
  raise "Missing Business ID" if $cfg['business.id'].nil?
  post('business/' + $cfg['business.id'] + '/product/', {:label=>name, :type=>type})
end

#new_solution(name, type = 'dataApi') ⇒ Object

Create a new solution



147
148
149
150
151
# File 'lib/MrMurano/Account.rb', line 147

def new_solution(name, type='dataApi')
  raise "Missing Business ID" if $cfg['business.id'].nil?
  raise "Solution name must be lowercase" if name.match(/[A-Z]/)
  post('business/' + $cfg['business.id'] + '/solution/', {:label=>name, :type=>type})
end

#productsObject



125
126
127
128
# File 'lib/MrMurano/Account.rb', line 125

def products
  raise "Missing Business ID" if $cfg['business.id'].nil?
  get('business/' + $cfg['business.id'] + '/product/')
end

#solutionsObject



141
142
143
144
# File 'lib/MrMurano/Account.rb', line 141

def solutions
  raise "Missing Business ID" if $cfg['business.id'].nil?
  get('business/' + $cfg['business.id'] + '/solution/')
end

#tokenObject



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/MrMurano/Account.rb', line 91

def token
  if @@token.nil? then
    # Cannot have token call token, so cannot use workit.
    uri = endPoint('token/')
    request = Net::HTTP::Post.new(uri)
    request['User-Agent'] = "MrMurano/#{MrMurano::VERSION}"
    request.content_type = 'application/json'
    curldebug(request)
    #request.basic_auth(username(), password())
    request.body = JSON.generate(_loginInfo)

    response = http.request(request)
    case response
    when Net::HTTPSuccess
      token = JSON.parse(response.body, json_opts)
      @@token = token[:token]
    else
      showHttpError(request, response)
      error "Check to see if username and password are correct."
      @@token = nil
    end
  end
  @@token
end

#token_reset(value = nil) ⇒ Object



116
117
118
# File 'lib/MrMurano/Account.rb', line 116

def token_reset(value=nil)
  @@token = value
end