Class: Pressy::Site

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

Overview

Site is a high-level interface for working with a WordPress site using Pressy.

Instance Attribute Summary collapse

Actions collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(store) ⇒ Site

Creates a new Site backed by store. Uses the configuration provided by store to create a WordPress client when needed.



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

def initialize(store)
  @store = store
end

Instance Attribute Details

#storeStore (readonly)

The Store that is managing the local storage for this site.

Returns:



7
8
9
# File 'lib/pressy/site.rb', line 7

def store
  @store
end

Class Method Details

.currentObject



82
83
84
# File 'lib/pressy/site.rb', line 82

def self.current
  self.new(Pressy::Store::FileStore.current)
end

Instance Method Details

#clientPressy::Client

The client that is being used to interact with the remote WordPress site.

Returns:

  • (Pressy::Client)


18
19
20
# File 'lib/pressy/site.rb', line 18

def client
  @client ||= create_client
end

#create(params = {}) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/pressy/site.rb', line 62

def create(params = {})
  uri = URI.parse(params.fetch(:url))

  site_config = {
    "host" => uri.host,
    "username" => params.fetch(:username),
    "password" => params.fetch(:password)
  }

  if uri.path != "" && uri.path != "/"
    # using File.join for this is sketchy but URI is very bad at joining
    site_config["path"] = File.join(uri.path, "xmlrpc.php")
  end

  new_store = store.create(params[:path] || uri.host, { "site" => site_config })
  Pressy::Site.new(new_store)
end

#create_post(post) ⇒ Object



56
57
58
59
60
# File 'lib/pressy/site.rb', line 56

def create_post(post)
  saved_post = client.create_post(post)
  rendered_post = Pressy::PostRenderer.render(saved_post)
  store.write(saved_post.id, rendered_post)
end

#pullObject

Checks for changes on the server and updates the files on disk to match.



29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/pressy/site.rb', line 29

def pull
  pull = Pressy::Action::Pull.new(
    local: fetch_local_posts,
    server: fetch_server_posts
  )

  pull.changeset.changes.each do |change|
    change.execute(store)
  end

  pull
end

#pushObject

Checks for local changes and sends them to the server.



43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/pressy/site.rb', line 43

def push
  push = Pressy::Action::Push.new(
    local: fetch_local_posts,
    server: fetch_server_posts
  )

  push.changeset.changes.each do |change|
    change.execute(store, client)
  end

  push
end

#rootObject



22
23
24
# File 'lib/pressy/site.rb', line 22

def root
  store.root
end