Class: Webbynode::ApiClient
- Includes:
- HTTParty
- Defined in:
- lib/webbynode/api_client.rb
Constant Summary collapse
- CREDENTIALS_FILE =
"#{Io.home_dir}/.webbynode"Class.new(StandardError)
- InactiveZone =
Class.new(StandardError)
- ApiError =
Class.new(StandardError)
Instance Method Summary collapse
- #create_a_record(id, record, ip, original_record) ⇒ Object
- #create_record(record, ip) ⇒ Object
- #create_zone(zone) ⇒ Object
- #credentials ⇒ Object
- #handle_error(response) ⇒ Object
- #init_credentials(overwrite = false) ⇒ Object
- #io ⇒ Object
- #ip_for(hostname) ⇒ Object
- #post(uri, options = {}) ⇒ Object
- #properties ⇒ Object
- #webbies ⇒ Object
- #zones ⇒ Object
Instance Method Details
#create_a_record(id, record, ip, original_record) ⇒ Object
46 47 48 49 50 51 52 53 54 55 |
# File 'lib/webbynode/api_client.rb', line 46 def create_a_record(id, record, ip, original_record) response = post("/dns/#{id}/records/new", :query => {"record[name]" => record, "record[type]" => "A", "record[data]" => ip}) if response["errors"] and response["errors"] =~ /Data has already been taken/ io.log "'#{original_record}' is already setup on Webbynode DNS, make sure it's pointing to #{ip}", :warning return end handle_error(response) response["record"] end |
#create_record(record, ip) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/webbynode/api_client.rb', line 23 def create_record(record, ip) original_record = record url = Domainatrix.parse("http://#{record}") record = url.subdomain domain = "#{url.domain}.#{url.public_suffix}." zone = zones[domain] if zone raise InactiveZone, domain unless zone[:status] == 'Active' else zone = create_zone(domain) end create_a_record(zone[:id], record, ip, original_record) end |
#create_zone(zone) ⇒ Object
40 41 42 43 44 |
# File 'lib/webbynode/api_client.rb', line 40 def create_zone(zone) response = post("/dns/new", :query => {"zone[domain]" => zone, "zone[ttl]" => "86400"}) handle_error(response) response end |
#credentials ⇒ Object
76 77 78 |
# File 'lib/webbynode/api_client.rb', line 76 def credentials @credentials ||= init_credentials end |
#handle_error(response) ⇒ Object
57 58 59 60 |
# File 'lib/webbynode/api_client.rb', line 57 def handle_error(response) raise ApiError, response["error"] if response["error"] raise ApiError, "invalid response from the API (code #{response.code})" unless response.code == 200 end |
#init_credentials(overwrite = false) ⇒ Object
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/webbynode/api_client.rb', line 80 def init_credentials(overwrite=false) creds = if io.file_exists?(CREDENTIALS_FILE) and !overwrite properties else email = overwrite[:email] if overwrite.is_a?(Hash) and overwrite[:email] token = overwrite[:token] if overwrite.is_a?(Hash) and overwrite[:token] io.log io.read_from_template("api_token") unless email and token email ||= ask("Login email: ") token ||= ask("API token: ") response = self.class.post("/webbies", :body => { :email => email, :token => token }) if response.code == 401 or response.code == 411 raise Unauthorized, "You have provided the wrong credentials" end properties['email'] = email properties['token'] = token properties.save puts { :email => email, :token => token } end end |
#ip_for(hostname) ⇒ Object
62 63 64 |
# File 'lib/webbynode/api_client.rb', line 62 def ip_for(hostname) (webbies[hostname] || {})['ip'] end |
#post(uri, options = {}) ⇒ Object
111 112 113 114 115 116 117 |
# File 'lib/webbynode/api_client.rb', line 111 def post(uri, ={}) response = self.class.post(uri, { :body => credentials }.merge()) if response.code == 401 or response.code == 411 raise Unauthorized, "You have provided the wrong credentials" end response end |
#properties ⇒ Object
107 108 109 |
# File 'lib/webbynode/api_client.rb', line 107 def properties @properties ||= Webbynode::Properties.new(CREDENTIALS_FILE) end |
#webbies ⇒ Object
66 67 68 69 70 71 72 73 74 |
# File 'lib/webbynode/api_client.rb', line 66 def webbies unless @webbies response = post("/webbies") || {} @webbies = response end @webbies['webbies'].inject({}) { |h, webby| h[webby['name']] = webby; h } end |
#zones ⇒ Object
16 17 18 19 20 21 |
# File 'lib/webbynode/api_client.rb', line 16 def zones response = post("/dns") if zones = response["zones"] zones.inject({}) { |h, zone| h[zone[:domain]] = zone; h } end end |