Class: NetDNARWS::NetDNA

Inherits:
Object
  • Object
show all
Includes:
Utils
Defined in:
lib/netdnarws.rb

Constant Summary

Constants included from Utils

Utils::RESERVED_CHARACTERS

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utils

#encode_params, #escape

Constructor Details

#initialize(company_alias, key, secret, server = 'rws.netdna.com', secure_connection = true, *options) ⇒ NetDNA

Returns a new instance of NetDNA.



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/netdnarws.rb', line 27

def initialize company_alias, key, secret,
  server='rws.netdna.com', secure_connection=true, *options
  @company_alias = company_alias
  @server = server
  @secure_connection = secure_connection
  @request_signer = Signet::OAuth1::Client.new(
    :client_credential_key => key,
    :client_credential_secret => secret,
    :two_legged => true
  )
end

Instance Attribute Details

#clientObject

Returns the value of attribute client.



26
27
28
# File 'lib/netdnarws.rb', line 26

def client
  @client
end

Instance Method Details

#_connection_typeObject



39
40
41
42
# File 'lib/netdnarws.rb', line 39

def _connection_type
  return "http" unless @secure_connection
  "https"
end

#_encode_params(params = {}) ⇒ Object



44
45
46
47
48
# File 'lib/netdnarws.rb', line 44

def _encode_params params={}
  encode_params(params).map { |k, v|
    "#{k}=#{v}"
  }.join "&"
end

#_get_url(uri, params = {}) ⇒ Object



50
51
52
53
54
55
56
57
# File 'lib/netdnarws.rb', line 50

def _get_url uri, params={}
  url = "#{_connection_type}://#{@server}/#{@company_alias}#{uri}"
  if not params.empty?
    url += "?#{_encode_params(params)}"
  end

  url
end

#_response_as_json(method, uri, options = {}, *attributes) ⇒ Object



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
# File 'lib/netdnarws.rb', line 59

def _response_as_json method, uri, options={}, *attributes
  if options.delete(:debug)
    puts "Making #{method.upcase} request to #{_get_url uri}"
  end

  request_options = {
    :uri => _get_url(uri, options[:body] ? attributes[0] : {}),
    :method => method
  }

  request_options[:body] = _encode_params(attributes[0]) if options[:body]
  request = @request_signer.generate_authenticated_request(request_options)
  request.headers["User-Agent"] = "Ruby NetDNA API Client"

  begin
    curb_options = {}
    curb_options[:url] = request_options[:uri]
    curb_options[:headers] = request.headers

    if not options[:body]
      response = CurbFu.send method, curb_options
    else
      response = CurbFu.send method, curb_options, request.body
    end

    return response if options[:debug_request]

    response_json = JSON.load(response.body)

    return response_json if options[:debug_json]

    if not (response.success? or response.redirect?)
      error_message = response_json['error']['message']
      raise NetDNARWS::APIException.new("#{response.status}: #{error_message}")
    end
  rescue TypeError
    raise NetDNARWS::APIException.new(
      "#{response.status}: No information supplied by the server"
    )
  end

  response_json
end

#delete(uri, data = {}, options = {}) ⇒ Object



118
119
120
121
# File 'lib/netdnarws.rb', line 118

def delete uri, data={}, options={}
  options[:body] = false
  self._response_as_json 'delete', uri, options, data
end

#get(uri, data = {}, options = {}) ⇒ Object



103
104
105
106
# File 'lib/netdnarws.rb', line 103

def get uri, data={}, options={}
  options[:body] = false
  self._response_as_json 'get', uri, options, data
end

#post(uri, data = {}, options = {}) ⇒ Object



108
109
110
111
# File 'lib/netdnarws.rb', line 108

def post uri, data={}, options={}
  options[:body] = true
  self._response_as_json 'post', uri, options, data
end

#purge(zone_id, file_or_files = nil, options = {}) ⇒ Object



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
# File 'lib/netdnarws.rb', line 123

def purge zone_id, file_or_files=nil, options={}
  if file_or_files.respond_to? :each
    options[:debug_json] = true if options[:debug_json].nil?
    responses = Hash.new

    file_or_files.each { |e|
      responses[e] = self.delete(
        "/zones/pull.json/#{zone_id}/cache",
        {'file' => e},
          options
      )
    }
    return responses
  end


  unless file_or_files.nil?
    return self.delete(
      "/zones/pull.json/#{zone_id}/cache",
      {'file' => file_or_files},
        options
    )
  end

  self.delete("/zones/pull.json/#{zone_id}/cache", {}, options)
end

#put(uri, data = {}, options = {}) ⇒ Object



113
114
115
116
# File 'lib/netdnarws.rb', line 113

def put uri, data={}, options={}
  options[:body] = true
  self._response_as_json 'put', uri, options, data
end