Sha256: 2b66a4be88975b9cab63ae505aba971be4627785e73fe1f113e800bdc00e831d

Contents?: true

Size: 1.87 KB

Versions: 2

Compression:

Stored size: 1.87 KB

Contents

require 'rest-client'
require 'json'

module Stastic
  class InvalidCredentials < RestClient::Forbidden; end
  class ValidationFailed < RestClient::UnprocessableEntity; end

  class Client
    attr_accessor :user, :token, :host

    def self.authenticate(email, password)
      client = new
      client.request(:get, "/authenticate", {:email => email, :password => password})["token"]
    end

    def self.request(method, uri, payload = nil, headers = {})
      client = new
      client.request(method, uri, payload, headers)
    end

    def self.create(name = nil)
      if name
        request(:post, "/sites", {:site => {:name => name}})
      else
        request(:post, "/sites")
      end
    end

    def self.list
      request(:get, "/sites")
    end

    def self.publish(site_id, archive_path)
      request(:put, "/sites/#{site_id}/publish", {:site => {:archive => File.new(archive_path)}})
    end

    def self.rename(site_id, name)
      request(:put, "/sites/#{site_id}", {:site => {:name => name}})
    end

    def self.add_domain(site_id, domain_name)
      request(:post, "/sites/#{site_id}/domains", {:domain => {:host => domain_name}})
    end

    def self.remove_domain(site_id, domain_name)
      request(:delete, "/sites/#{site_id}/domains/#{domain_name}")
    end

    def initialize(host = "http://stastic.com")
      if Stastic::Config.exists?
        self.user = Stastic::Config.user
        self.token = Stastic::Config.token
      end
      self.host = host
    end

    def request(method, uri, payload = nil, headers = {})
      headers.merge!(:accept => 'json', "X-STASTIC" => Stastic::VERSION)

      response = RestClient::Request.execute(
        :method   => method,
        :url      => host + uri,
        :headers  => headers,
        :payload  => payload,
        :user     => user,
        :password => token)

      return JSON::parse(response.body)
    end

  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
stastic-0.2.1 lib/stastic/client.rb
stastic-0.2.0 lib/stastic/client.rb