Class: GandiV5

Inherits:
Object
  • Object
show all
Defined in:
lib/gandi_v5.rb,
lib/gandi_v5/data.rb,
lib/gandi_v5/email.rb,
lib/gandi_v5/error.rb,
lib/gandi_v5/domain.rb,
lib/gandi_v5/billing.rb,
lib/gandi_v5/version.rb,
lib/gandi_v5/live_dns.rb,
lib/gandi_v5/domain/tld.rb,
lib/gandi_v5/email/slot.rb,
lib/gandi_v5/email/offer.rb,
lib/gandi_v5/billing/info.rb,
lib/gandi_v5/domain/dates.rb,
lib/gandi_v5/organization.rb,
lib/gandi_v5/email/forward.rb,
lib/gandi_v5/email/mailbox.rb,
lib/gandi_v5/live_dns/zone.rb,
lib/gandi_v5/data/converter.rb,
lib/gandi_v5/domain/contact.rb,
lib/gandi_v5/domain/contract.rb,
lib/gandi_v5/domain/live_dns.rb,
lib/gandi_v5/live_dns/domain.rb,
lib/gandi_v5/domain/auto_renew.rb,
lib/gandi_v5/error/gandi_error.rb,
lib/gandi_v5/data/converter/time.rb,
lib/gandi_v5/domain/availability.rb,
lib/gandi_v5/live_dns/record_set.rb,
lib/gandi_v5/billing/info/prepaid.rb,
lib/gandi_v5/domain/sharing_space.rb,
lib/gandi_v5/data/converter/symbol.rb,
lib/gandi_v5/live_dns/zone/snapshot.rb,
lib/gandi_v5/data/converter/array_of.rb,
lib/gandi_v5/domain/availability/tax.rb,
lib/gandi_v5/email/mailbox/responder.rb,
lib/gandi_v5/live_dns/has_zone_records.rb,
lib/gandi_v5/domain/renewal_information.rb,
lib/gandi_v5/domain/restore_information.rb,
lib/gandi_v5/domain/availability/product.rb,
lib/gandi_v5/domain/availability/product/price.rb,
lib/gandi_v5/domain/availability/product/period.rb

Overview

Namespace for classes which access LiveDNS details.

Defined Under Namespace

Modules: Data Classes: Billing, Domain, Email, Error, LiveDNS, Organization

Constant Summary collapse

BASE =
'https://api.gandi.net/v5/'
VERSION =
'0.3.0'

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.api_key=(value) ⇒ Object

Sets the attribute api_key

Parameters:

  • value

    the value to set the attribute api_key to.



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

def api_key=(value)
  @api_key = value
end

Instance Attribute Details

#api_key=(value) ⇒ String (writeonly)

Returns:

  • (String)


40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/gandi_v5.rb', line 40

class GandiV5
  BASE = 'https://api.gandi.net/v5/'

  # @see GandiV5::Domain.fetch
  def self.domain(fqdn)
    GandiV5::Domain.fetch(fqdn)
  end

  # @see GandiV5::Domain.list
  def self.domains(**params)
    GandiV5::Domain.list(**params)
  end

  # @see GandiV5::Email::Mailbox.list
  def self.mailboxes(fqdn, **params)
    GandiV5::Email::Mailbox.list(fqdn, **params)
  end

  # @see GandiV5::Email::Slot.list
  def self.mailbox_slots(fqdn)
    GandiV5::Email::Slot.list(fqdn)
  end

  class << self
    attr_writer :api_key

    # Might raise:
    #  * RestClient::NotFound
    #  * RestClient::Unauthorized
    #      Bad authentication attempt because of a wrong API Key.
    #  * RestClient::Forbidden
    #      Access to the resource is denied.
    #      Mainly due to a lack of permissions to access it.
    #  * GandiV5::Error
    #  * JSON::ParserError
    def get(url, **headers)
      prepare_headers headers, url
      response = RestClient.get url, **headers
      [response, parse_response(response)]
    rescue RestClient::BadRequest => e
      handle_bad_request(e)
    end

    # Might raise:
    #  * RestClient::NotFound
    #  * RestClient::Unauthorized
    #      Bad authentication attempt because of a wrong API Key.
    #  * RestClient::Forbidden
    #      Access to the resource is denied.
    #      Mainly due to a lack of permissions to access it.
    #  * RestClient::Conflict
    #  * GandiV5::Error
    #  * JSON::ParserError
    def delete(url, **headers)
      prepare_headers headers, url
      response = RestClient.delete url, **headers
      [response, parse_response(response)]
    rescue RestClient::BadRequest => e
      handle_bad_request(e)
    end

    # Might raise:
    #  * RestClient::NotFound
    #  * RestClient::Unauthorized
    #      Bad authentication attempt because of a wrong API Key.
    #  * RestClient::Forbidden
    #      Access to the resource is denied.
    #      Mainly due to a lack of permissions to access it.
    #  * RestClient::BadRequest
    #  * RestClient::Conflict
    #  * GandiV5::Error
    #  * JSON::ParserError
    def patch(url, payload = '', **headers)
      prepare_headers headers, url
      headers[:'content-type'] ||= 'application/json'
      response = RestClient.patch url, payload, **headers
      [response, parse_response(response)]
    rescue RestClient::BadRequest => e
      handle_bad_request(e)
    end

    # Might raise:
    #  * RestClient::NotFound
    #  * RestClient::Unauthorized
    #      Bad authentication attempt because of a wrong API Key.
    #  * RestClient::Forbidden
    #      Access to the resource is denied.
    #      Mainly due to a lack of permissions to access it.
    #  * RestClient::BadRequest
    #  * RestClient::Conflict
    #  * GandiV5::Error
    #  * JSON::ParserError
    def post(url, payload = '', **headers)
      prepare_headers headers, url
      headers[:'content-type'] ||= 'application/json'
      response = RestClient.post url, payload, **headers
      [response, parse_response(response)]
    rescue RestClient::BadRequest => e
      handle_bad_request(e)
    end

    # Might raise:
    #  * RestClient::NotFound
    #  * RestClient::Unauthorized
    #      Bad authentication attempt because of a wrong API Key.
    #  * RestClient::Forbidden
    #      Access to the resource is denied.
    #      Mainly due to a lack of permissions to access it.
    #  * RestClient::BadRequest
    #  * RestClient::Conflict
    #  * GandiV5::Error
    #  * JSON::ParserError
    def put(url, payload = '', **headers)
      prepare_headers headers, url
      headers[:'content-type'] ||= 'application/json'
      response = RestClient.put url, payload, **headers
      [response, parse_response(response)]
    rescue RestClient::BadRequest => e
      handle_bad_request(e)
    end

    private

    def api_key
      @api_key ||= ENV.fetch('GANDI_API_KEY')
    end

    def authorisation_header(url)
      if url.start_with?(BASE)
        { Authorization: "Apikey #{api_key}" }
      elsif url.start_with?(GandiV5::LiveDNS::BASE)
        { 'X-Api-Key': api_key }
      else
        fail ArgumentError, "Don't know how to authorise for url: #{url}"
      end
    end

    def parse_response(response)
      type = response.headers.fetch(:content_type).split(';').first.chomp
      case type
      when 'text/plain'
        response.body.to_s
      when 'application/json'
        response = JSON.parse(response.body)
        if response.is_a?(Hash) && response['status'].eql?('error')
          fail GandiV5::Error::GandiError.from_hash(response)
        end

        response
      else
        fail ArgumentError, "Don't know how to parse a #{type} response"
      end
    end

    def prepare_headers(headers, url)
      headers.transform_keys!(&:to_sym)
      headers[:accept] ||= 'application/json'
      headers.merge!(authorisation_header(url))
    end

    def handle_bad_request(exception)
      data = JSON.parse exception.response.body
      unless data.is_a?(Hash) && data['status'].eql?('error') && data['errors'].is_a?(Array)
        raise exception
      end

      field, message = data['errors'].first.values_at('name', 'description')
      fail GandiV5::Error::GandiError, "#{field}: #{message}"
    rescue JSON::ParserError
      raise exception
    end
  end
end

Class Method Details

.delete(url, **headers) ⇒ Object

Might raise:

* RestClient::NotFound
* RestClient::Unauthorized
    Bad authentication attempt because of a wrong API Key.
* RestClient::Forbidden
    Access to the resource is denied.
    Mainly due to a lack of permissions to access it.
* RestClient::Conflict
* GandiV5::Error
* JSON::ParserError


93
94
95
96
97
98
99
# File 'lib/gandi_v5.rb', line 93

def delete(url, **headers)
  prepare_headers headers, url
  response = RestClient.delete url, **headers
  [response, parse_response(response)]
rescue RestClient::BadRequest => e
  handle_bad_request(e)
end

.domain(fqdn) ⇒ Object



44
45
46
# File 'lib/gandi_v5.rb', line 44

def self.domain(fqdn)
  GandiV5::Domain.fetch(fqdn)
end

.domains(**params) ⇒ Object



49
50
51
# File 'lib/gandi_v5.rb', line 49

def self.domains(**params)
  GandiV5::Domain.list(**params)
end

.get(url, **headers) ⇒ Object

Might raise:

* RestClient::NotFound
* RestClient::Unauthorized
    Bad authentication attempt because of a wrong API Key.
* RestClient::Forbidden
    Access to the resource is denied.
    Mainly due to a lack of permissions to access it.
* GandiV5::Error
* JSON::ParserError


75
76
77
78
79
80
81
# File 'lib/gandi_v5.rb', line 75

def get(url, **headers)
  prepare_headers headers, url
  response = RestClient.get url, **headers
  [response, parse_response(response)]
rescue RestClient::BadRequest => e
  handle_bad_request(e)
end

.mailbox_slots(fqdn) ⇒ Object



59
60
61
# File 'lib/gandi_v5.rb', line 59

def self.mailbox_slots(fqdn)
  GandiV5::Email::Slot.list(fqdn)
end

.mailboxes(fqdn, **params) ⇒ Object



54
55
56
# File 'lib/gandi_v5.rb', line 54

def self.mailboxes(fqdn, **params)
  GandiV5::Email::Mailbox.list(fqdn, **params)
end

.patch(url, payload = '', **headers) ⇒ Object

Might raise:

* RestClient::NotFound
* RestClient::Unauthorized
    Bad authentication attempt because of a wrong API Key.
* RestClient::Forbidden
    Access to the resource is denied.
    Mainly due to a lack of permissions to access it.
* RestClient::BadRequest
* RestClient::Conflict
* GandiV5::Error
* JSON::ParserError


112
113
114
115
116
117
118
119
# File 'lib/gandi_v5.rb', line 112

def patch(url, payload = '', **headers)
  prepare_headers headers, url
  headers[:'content-type'] ||= 'application/json'
  response = RestClient.patch url, payload, **headers
  [response, parse_response(response)]
rescue RestClient::BadRequest => e
  handle_bad_request(e)
end

.post(url, payload = '', **headers) ⇒ Object

Might raise:

* RestClient::NotFound
* RestClient::Unauthorized
    Bad authentication attempt because of a wrong API Key.
* RestClient::Forbidden
    Access to the resource is denied.
    Mainly due to a lack of permissions to access it.
* RestClient::BadRequest
* RestClient::Conflict
* GandiV5::Error
* JSON::ParserError


132
133
134
135
136
137
138
139
# File 'lib/gandi_v5.rb', line 132

def post(url, payload = '', **headers)
  prepare_headers headers, url
  headers[:'content-type'] ||= 'application/json'
  response = RestClient.post url, payload, **headers
  [response, parse_response(response)]
rescue RestClient::BadRequest => e
  handle_bad_request(e)
end

.put(url, payload = '', **headers) ⇒ Object

Might raise:

* RestClient::NotFound
* RestClient::Unauthorized
    Bad authentication attempt because of a wrong API Key.
* RestClient::Forbidden
    Access to the resource is denied.
    Mainly due to a lack of permissions to access it.
* RestClient::BadRequest
* RestClient::Conflict
* GandiV5::Error
* JSON::ParserError


152
153
154
155
156
157
158
159
# File 'lib/gandi_v5.rb', line 152

def put(url, payload = '', **headers)
  prepare_headers headers, url
  headers[:'content-type'] ||= 'application/json'
  response = RestClient.put url, payload, **headers
  [response, parse_response(response)]
rescue RestClient::BadRequest => e
  handle_bad_request(e)
end