Skip to content

dsgraham/cloudflare

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Cloudflare

A Ruby client library for Cloudflare API v4.

Installation

Add this line to your application's Gemfile:

gem 'cloudflare'

And then execute:

$ bundle

Or install it yourself as:

$ gem install cloudflare

Usage

Zones

Listing zones

zones = Cloudflare::Client.new(api_token: '[API_TOKEN]').zones.list
zones.each do |zone|
  puts zone.id
end

Get a zone by ID

zones = Cloudflare::Client.new(api_token: '[API_TOKEN]').zones.retrieve(zone_id: '[ZONE_ID]')

Firewall Rules

List firewall rules for a zone

rules = Cloudflare::Client.new(api_token: '[API_TOKEN]').firewall_rules.list(zone_id: '[ZONE_ID]')
rules.each do |rule|
  puts rule.id
end

Get a firewall rule by ID

rule = Cloudflare::Client.new(api_token: '[API_TOKEN]').firewall_rules.retrieve(zone_id: '[ZONE_ID]', id: '[RULE_ID]')

Create a firewall rule with an existing filter

client = Cloudflare::Client.new(auth_emai: '[EMAIL]', auth_key: '[AUTH_KEY]')
rule = client.firewall_rules.create(zone_id: '[ZONE_ID]', 
                                    filter_id: '[FILTER_ID]',
                                    action: 'block',
                                    description: 'Block traffic from...')

Create a firewall rule and filter at the same time

client = Cloudflare::Client.new(auth_emai: '[EMAIL]', auth_key: '[AUTH_KEY]')
rule = client.firewall_rules.create_with_filter(zone_id: '[ZONE_ID]', 
                                    filter_id: {description: 'Stop Russian hackers', expression: '(ip.geoip.country eq "RU")'},
                                    action: 'block',
                                    description: 'Block traffic from...')

Delete a firewall rule

client = Cloudflare::Client.new(auth_emai: '[EMAIL]', auth_key: '[AUTH_KEY]')
client.firewall_rules.delete(zone_id: '[ZONE_ID]', id: '[RULE_ID]')

Filters

List filters for a zone

filters = Cloudflare::Client.new(api_token: '[API_TOKEN]').filters.list(zone_id: '[ZONE_ID]')

Get a filter by ID

filter = = Cloudflare::Client.new(api_token: '[API_TOKEN]').filters.retrieve(zone_id: '[ZONE_ID]', id: '[FILTER_ID]')

Create a filter

client = Cloudflare::Client.new(auth_emai: '[EMAIL]', auth_key: '[AUTH_KEY]')
filter = client.filters.create(zone_id: '[ZONE_ID]', expression: '(ip.geoip.country eq "RU")', description: 'Country is Russia')

Delete a filter

client = Cloudflare::Client.new(auth_emai: '[EMAIL]', auth_key: '[AUTH_KEY]')
client.filters.delete(zone_id: '[ZONE_ID]', id: '[FILTER_ID]')

Development

After checking out the repo, run bin/setup to install dependencies. Then, run rake spec to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and tags, and push the .gem file to rubygems.org.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/dsgraham/cloudflare.

License

The gem is available as open source under the terms of the MIT License.

About

A Ruby client library for Cloudflare API v4.

Resources

License

Stars

Watchers

Forks

Packages

No packages published