Method: OneAndOne::User#create

Defined in:
lib/1and1/user.rb

#create(name: nil, description: nil, password: nil, email: nil) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/1and1/user.rb', line 57

def create(name: nil, description: nil, password: nil, email: nil)

  # Build POST body
  new_user = {
    'name' => name,
    'description' => description,
    'password' => password,
    'email' => email
  }

  # Clean out null keys in POST body
  body = OneAndOne.clean_hash(new_user)

  # Stringify the POST body
  string_body = body.to_json

  # Build URL
  path = OneAndOne.build_url('/users')

  # Perform request
  response = @connection.request(:method => :post,
    :path => path,
    :headers => $header,
    :body => string_body)

  # Check response status
  OneAndOne.check_response(response.body, response.status)

  #JSON-ify the response string
  json = JSON.parse(response.body)

  # Save new user ID to User instance
  @id = json['id']
  @specs = json

  # If all good, return JSON
  json

end