class Weeblycloud::CloudClient

Public Class Methods

configure(api_key, api_secret) click to toggle source

Globally configure API key and secret

# File lib/weeblycloud/cloudclient/cloudclient.rb, line 23
def self.configure(api_key, api_secret)
  @@api_key = api_key
  @@api_secret = api_secret
end
new(api_key = nil, api_secret = nil) click to toggle source
# File lib/weeblycloud/cloudclient/cloudclient.rb, line 12
def initialize(api_key = nil, api_secret = nil)
  if api_key || api_secret
    self.configure(api_key, api_secret)
  elsif @@api_key.nil? || @@api_secret.nil?
    raise "No API keys provided."
  end

  @BASE_API = "https://api.weeblycloud.com/"
end

Public Instance Methods

delete(endpoint, options={}) click to toggle source

Make a DELETE request

# File lib/weeblycloud/cloudclient/cloudclient.rb, line 73
def delete(endpoint, options={})
  ops = {:params => {}, :content => {}}
  ops.merge!(options)

  return call("DELETE", endpoint, ops[:content], ops[:params])
end
get(endpoint, options={}) click to toggle source

Make a GET request

# File lib/weeblycloud/cloudclient/cloudclient.rb, line 29
def get(endpoint, options={})
  ops = {
    :page_size => nil,
    :page => nil,
    :params => {},
    :content => {}
  }
  ops.merge!(options)
  if ops[:page_size]
    ops[:params].merge!({"page_size" => ops[:page_size]})
  end

  if ops[:page]
    ops[:params].merge!({"page" => options[:page]})
  end

  return call("GET", endpoint, ops[:content], ops[:params])
end
patch(endpoint, options={}) click to toggle source

Make a PATCH request

# File lib/weeblycloud/cloudclient/cloudclient.rb, line 57
def patch(endpoint, options={})
  ops = {:params => {}, :content => {}}
  ops.merge!(options)

  return call("PATCH", endpoint, ops[:content], ops[:params])
end
post(endpoint, options={}) click to toggle source

Make a POST request

# File lib/weeblycloud/cloudclient/cloudclient.rb, line 49
def post(endpoint, options={})
  ops = {:params => {}, :content => {}}
  ops.merge!(options)

  return call("POST", endpoint, ops[:content], ops[:params])
end
put(endpoint, options={}) click to toggle source

Make a PUT request

# File lib/weeblycloud/cloudclient/cloudclient.rb, line 65
def put(endpoint, options={})
  ops = {:params => {}, :content => {}}
  ops.merge!(options)

  return call("PUT", endpoint, ops[:content], ops[:params])
end

Private Instance Methods

call(method, endpoint, content={}, params={}) click to toggle source

Make an API call

# File lib/weeblycloud/cloudclient/cloudclient.rb, line 83
def call(method, endpoint, content={}, params={})
  json_data = content.to_json
  strip_slashes(endpoint)
  headers = {
    "Content-Type" => "application/json",
    "X-Cloud-Client-Type" => "ruby",
    "X-Cloud-Client-Version" => VERSION,
    "X-Public-Key" => @@api_key,
    "X-Signed-Request-Hash" => sign(method, endpoint, json_data)
  }
  url = @BASE_API + endpoint
  response = HTTP.headers(headers).request(method, url, :body => json_data, :params => params)
  return WeeblyCloudResponse.new(response, url, headers, content, params)
end
sign(request_type, endpoint, content) click to toggle source

Signs a request and returns the HMAC hash. See cloud-developer.weebly.com/about-the-rest-apis.html#signing-and-authenticating-requests

# File lib/weeblycloud/cloudclient/cloudclient.rb, line 100
def sign(request_type, endpoint, content)
  request = request_type + "\n" + endpoint + "\n" + content
  digest = OpenSSL::Digest.new("sha256")
  mac = OpenSSL::HMAC.hexdigest(digest, @@api_secret, request)
  base = Base64.strict_encode64(mac)
  return base
end
strip_slashes(str) click to toggle source

Remove a “/” if it is the first or last character in a string.

# File lib/weeblycloud/cloudclient/cloudclient.rb, line 109
def strip_slashes(str)
  if str.index("/") === 0
    str = str.slice(1..-1)
  end
  if str.index("/") === str.length
    str = str.slice(0..-2)
  end
end