Class: Stastic::Client

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeClient

Returns a new instance of Client.



47
48
49
50
51
52
53
54
# File 'lib/stastic/client.rb', line 47

def initialize
  if Stastic::Config.exists?
    self.user = Stastic::Config.user
    self.token = Stastic::Config.token
    self.host = Stastic::Config.host
  end
  self.host ||= "https://stastic.com"
end

Instance Attribute Details

#hostObject

Returns the value of attribute host.



7
8
9
# File 'lib/stastic/client.rb', line 7

def host
  @host
end

#tokenObject

Returns the value of attribute token.



7
8
9
# File 'lib/stastic/client.rb', line 7

def token
  @token
end

#userObject

Returns the value of attribute user.



7
8
9
# File 'lib/stastic/client.rb', line 7

def user
  @user
end

Class Method Details

.add_domain(site_id, domain_name) ⇒ Object



39
40
41
# File 'lib/stastic/client.rb', line 39

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

.authenticate(email, password) ⇒ Object



9
10
11
12
# File 'lib/stastic/client.rb', line 9

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

.create(name = nil) ⇒ Object



19
20
21
22
23
24
25
# File 'lib/stastic/client.rb', line 19

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

.listObject



27
28
29
# File 'lib/stastic/client.rb', line 27

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

.publish(site_id, archive_path) ⇒ Object



31
32
33
# File 'lib/stastic/client.rb', line 31

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

.remove_domain(site_id, domain_name) ⇒ Object



43
44
45
# File 'lib/stastic/client.rb', line 43

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

.rename(site_id, name) ⇒ Object



35
36
37
# File 'lib/stastic/client.rb', line 35

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

.request(method, uri, payload = nil, headers = {}) ⇒ Object



14
15
16
17
# File 'lib/stastic/client.rb', line 14

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

Instance Method Details

#request(method, uri, payload = nil, headers = {}) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/stastic/client.rb', line 56

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