Class: MaxCDN::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/maxcdn.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(company_alias, key, secret, server = "rws.maxcdn.com", secure_connection = true, _debug = false) ⇒ Client

Returns a new instance of Client.



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/maxcdn.rb', line 13

def initialize(company_alias, key, secret, server="rws.maxcdn.com", secure_connection=true, _debug=false)
  @debug = _debug
  @secure_connection = secure_connection
  @company_alias = company_alias
  @server = server
  @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.



12
13
14
# File 'lib/maxcdn.rb', line 12

def client
  @client
end

#debugObject

Returns the value of attribute debug.



12
13
14
# File 'lib/maxcdn.rb', line 12

def debug
  @debug
end

Instance Method Details

#_connection_typeObject



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

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

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



30
31
32
33
34
35
36
37
# File 'lib/maxcdn.rb', line 30

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

  url
end

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



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

def _response_as_json method, uri, options={}, data={}
  puts "Making #{method.upcase} request to #{_get_url uri}" if debug

  req_opts = {
    :method => method
  }

  req_opts[:uri]  = _get_url(uri, (options[:body] ? {} : data))
  req_opts[:body] = data.to_params if options[:body]

  request = @request_signer.generate_authenticated_request(req_opts)

  # crazyness for headers
  headers = options.delete(:headers) || {}
  headers["User-Agent"] = "Ruby MaxCDN API Client"

  # because CurbFu overwrites 'content-type' header, strip it
  # to set it later
  content_type = headers.case_indifferent_delete("Content-Type") || (options[:body] ? "application/json" : "application/x-www-form-urlencoded")

  # merge headers with request headers
  request.headers.case_indifferent_merge(headers)

  begin
    curb_opts = {
      :url => req_opts[:uri],
      :headers => request.headers
    }

    CurbFu.debug = debug

    response = CurbFu.send method, curb_opts, request.body do |curb|
      curb.verbose = debug

      # Because CurbFu overwrites the content-type header passed
      # to it. so we'll be setting our own.
      #
      # First, remove any existing 'Content-Type' header.
      curb.headers.case_indifferent_delete("Content-Type")

      # Second, set 'Content-Type' to our desired value.
      curb.headers["Content-Type"] = content_type
    end

    return response if options[:debug_request]
    pp response if debug

    response_json = JSON.load(response.body)

    return response_json if options[:debug_json]
    pp response_json if debug

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

  response_json
end

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



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/maxcdn.rb', line 116

def purge zone_id, file_or_files=nil, options={}
  if file_or_files.nil?
    return self.delete("/zones/pull.json/#{zone_id}/cache", {}, options)
  end

  if file_or_files.is_a?(String)
    return self.delete("/zones/pull.json/#{zone_id}/cache", { "files" => file_or_files }, options)
  end

  if file_or_files.is_a?(Array)
    result = {}
    file_or_files.each do |file|
      result[file] = self.delete("/zones/pull.json/#{zone_id}/cache", { "files" => file }, options)
    end
    return result
  end

  if file_or_files.is_a?(Hash)
    return self.purge(zone_id, file_or_files[:files]) if file_or_files.has_key?("files")
    return self.purge(zone_id, file_or_files[:files]) if file_or_files.has_key?(:files)
  end

  raise MaxCDN::APIException.new("Invalid file_or_files argument for delete method.")
end