Skip to content

Commit

Permalink
Merge pull request #17 from Evesy/master
Browse files Browse the repository at this point in the history
Add support for custom_hostnames
  • Loading branch information
thde authored May 22, 2019
2 parents 995d015 + bc3abc3 commit a7568a4
Show file tree
Hide file tree
Showing 10 changed files with 224 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ A good reference on how to use this wrapper are also the Rspecs.
* `/zones/:zone_id/available_plans` GET
* `/zones/:zone_id/available_plans/:plan_id` GET
* `/zones/:zone_id/available_rate_plans`
* `/zones/:zone_id/custom_hostnames` GET, POST, PATCH
* `/zones/:zone_id/dns_records` GET, POST
* `/zones/:zone_id/dns_records/:record_id` GET, DELETE (PUT not implemented)
* `/zones/:zone_id/purge_cache` POST
Expand Down
1 change: 1 addition & 0 deletions lib/cloudflair/api/zone.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class Zone
require 'cloudflair/api/zone__dns_records'
require 'cloudflair/api/zone__available_plans'
require 'cloudflair/api/zone__railguns'
require 'cloudflair/api/zone__custom_hostnames'

attr_reader :zone_id

Expand Down
17 changes: 17 additions & 0 deletions lib/cloudflair/api/zone/custom_hostname.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
require 'cloudflair/entity'

module Cloudflair
class CustomHostname
include Cloudflair::Entity

attr_reader :zone_id, :custom_hostname_id
deletable true
patchable_fields :ssl, :custom_origin_server, :custom_metadata
path 'zones/:zone_id/custom_hostnames/:custom_hostname_id'

def initialize(zone_id, custom_hostname_id)
@zone_id = zone_id
@custom_hostname_id = custom_hostname_id
end
end
end
29 changes: 29 additions & 0 deletions lib/cloudflair/api/zone__custom_hostnames.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
require 'cloudflair/api/zone/custom_hostname'

module Cloudflair
class Zone
def custom_hostnames(filter = {})
raw_hostnames = response connection.get("#{path}/custom_hostnames", filter)

raw_hostnames.map { |raw_hostname| build_custom_hostname(raw_hostname) }
end

def custom_hostname(custom_hostname_id)
Cloudflair::CustomHostname.new zone_id, custom_hostname_id
end

def new_custom_hostname(hostname_data)
raw_hostname = response connection.post("#{path}/custom_hostnames", hostname_data)

build_custom_hostname raw_hostname
end

private

def build_custom_hostname(raw_hostname)
hostname = custom_hostname raw_hostname['id']
hostname.data = raw_hostname
hostname
end
end
end
46 changes: 46 additions & 0 deletions spec/cloudflair/api/zone/custom_hostname_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
require 'spec_helper'

describe Cloudflair::CustomHostname do
let(:zone_identifier) { '023e105f4ecef8ad9ca31a8372d0c353' }
let(:hostname_identifier) { '0d89c70d-ad9f-4843-b99f-6cc0252067e9' }
let(:response_json) { File.read('spec/cloudflair/fixtures/zone/custom_hostname.json') }
let(:url) { "/client/v4/zones/#{zone_identifier}/custom_hostnames/#{hostname_identifier}" }
subject { Cloudflair.zone(zone_identifier).custom_hostname(hostname_identifier) }

before do
faraday_stubs.get(url) do |_env|
[200, { content_type: 'application/json' }, response_json]
end
allow(Faraday).to receive(:new).and_return faraday
end

it 'loads the data on demand and caches' do
expect(faraday).to receive(:get).once.and_call_original

expect(subject.id).to eq hostname_identifier
expect(subject.hostname).to eq 'app.example.com'
end

it 'reloads the AvailablePlan on request' do
expect(faraday).to receive(:get).twice.and_call_original

expect(subject.reload).to be subject
expect(subject.reload).to be subject
end

describe '#delete' do
let(:response_json) { File.read('spec/cloudflair/fixtures/zone/custom_hostname_deleted.json') }

before do
faraday_stubs.delete(url) do |_env|
[200, { content_type: 'application/json' }, response_json]
end
end

it 'performs the delete' do
expect(faraday).to receive(:delete).once.and_call_original

subject.delete
end
end
end
78 changes: 78 additions & 0 deletions spec/cloudflair/api/zone__custom_hostnames_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
require 'spec_helper'

describe Cloudflair::Zone, 'custom_hostname things' do
before do
allow(Faraday).to receive(:new).and_return faraday
end

let(:zone_identifier) { '023e105f4ecef8ad9ca31a8372d0c353' }
let(:url) { "/client/v4/zones/#{zone_identifier}/custom_hostnames" }
subject(:zone) { Cloudflair.zone zone_identifier }

describe '#custom_hostname' do
it 'returns a Custom Hostname instance' do
expect(subject.custom_hostname('abcdef')).to be_a Cloudflair::CustomHostname
end
end

describe '#new_custom_hostname=' do
let(:new_custom_hostname_data) { { hostname: 'app.example.com', ssl: {method: 'http', type: 'dv', settings: {http2: 'on', min_tls_version: '1.2', tls_1_3: 'on', ciphers: ['ECDHE-RSA-AES128-GCM-SHA256', 'AES128-SHA']}}} }
let(:response_json) { File.read('spec/cloudflair/fixtures/zone/custom_hostname.json') }
let(:the_new_custom_hostname) { subject.new_custom_hostname(new_custom_hostname_data) }

before do
faraday_stubs.post(url, new_custom_hostname_data) do |_env|
[200, { content_type: 'application/json' }, response_json]
end
end

it 'returns a new CustomHostname instance' do
expect(faraday).to receive(:post).and_call_original

expect(the_new_custom_hostname).to be_a Cloudflair::CustomHostname
end

it 'prepopulates the returned CustomHostname instance' do
expect(faraday).to_not receive(:get)

expect(the_new_custom_hostname.id).to eq('0d89c70d-ad9f-4843-b99f-6cc0252067e9')
expect(the_new_custom_hostname.hostname).to eq('app.example.com')
expect(the_new_custom_hostname.ssl['type']).to eq('dv')
end
end

describe '#custom_hostnames' do
let(:response_json) { File.read('spec/cloudflair/fixtures/zone/custom_hostnames.json') }
subject { zone.custom_hostnames }

before do
faraday_stubs.get(url) do |_env|
[200, { content_type: 'application/json' }, response_json]
end
end

it 'calls the other url' do
expect(faraday).to receive(:get).once.and_call_original

subject
end

it 'returns the correct types' do
expect(subject).to be_a Array
subject.each do |plan|
expect(plan).to be_a Cloudflair::CustomHostname
end
end

it 'returns the correct amount' do
expect(subject.length).to be 1
end

it 'returns the correct values' do
custom_hostname = subject[0]
expect(custom_hostname.id).to eq '0d89c70d-ad9f-4843-b99f-6cc0252067e9'
expect(custom_hostname.hostname).to eq 'app.example.com'
expect(custom_hostname.ssl['type']).to eq 'dv'
end
end
end
16 changes: 16 additions & 0 deletions spec/cloudflair/fixtures/zone/custom_hostname.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"success": true,
"errors": [],
"messages": [],
"result": {
"id": "0d89c70d-ad9f-4843-b99f-6cc0252067e9",
"hostname": "app.example.com",
"ssl": {
"status": "pending_validation",
"method": "http",
"type": "dv",
"cname_target": "dcv.digicert.com",
"cname": "810b7d5f01154524b961ba0cd578acc2.app.example.com"
}
}
}
10 changes: 10 additions & 0 deletions spec/cloudflair/fixtures/zone/custom_hostname_deleted.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"result": {
"id": "8a5577f0-ed62-4716-8c1f-1ae34a7f7cdb",
"ssl": null,
"created_at": "2019-05-03T16:51:01.146758Z"
},
"success": true,
"errors": [],
"messages": []
}
25 changes: 25 additions & 0 deletions spec/cloudflair/fixtures/zone/custom_hostnames.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"success": true,
"errors": [],
"messages": [],
"result": [
{
"id": "0d89c70d-ad9f-4843-b99f-6cc0252067e9",
"hostname": "app.example.com",
"ssl": {
"status": "pending_validation",
"method": "http",
"type": "dv",
"cname_target": "dcv.digicert.com",
"cname": "810b7d5f01154524b961ba0cd578acc2.app.example.com"
}
}
],
"result_info": {
"page": 1,
"per_page": 20,
"count": 1,
"total_count": 2000
}
}

1 change: 1 addition & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
require 'cloudflair/api/zone/analytics'
require 'cloudflair/api/zone/available_plan'
require 'cloudflair/api/zone/available_rate_plan'
require 'cloudflair/api/zone/custom_hostname'
require 'cloudflair/api/zone/dns_record'
require 'cloudflair/api/zone/purge_cache'
require 'cloudflair/api/zone/railgun'
Expand Down

0 comments on commit a7568a4

Please sign in to comment.