Class: LaunchrockSync::SyncClient

Inherits:
Object
  • Object
show all
Defined in:
lib/launchrock-sync/models/sync_client.rb

Defined Under Namespace

Classes: MailChimpClient

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes) ⇒ SyncClient

Returns a new instance of SyncClient.



5
6
7
# File 'lib/launchrock-sync/models/sync_client.rb', line 5

def initialize(attributes)
  self.attributes = attributes.deep_symbolize_keys
end

Instance Attribute Details

#attributesObject

Returns the value of attribute attributes.



3
4
5
# File 'lib/launchrock-sync/models/sync_client.rb', line 3

def attributes
  @attributes
end

Instance Method Details

#launchrock_clientObject



24
25
26
27
28
29
# File 'lib/launchrock-sync/models/sync_client.rb', line 24

def launchrock_client
  @launchrock_client ||= Launchrock::Client.find_by_email_and_password(
    attributes[:launchrock][:email], 
    attributes[:launchrock][:password]
  )
end

#launchrock_siteObject



16
17
18
19
20
21
22
# File 'lib/launchrock-sync/models/sync_client.rb', line 16

def launchrock_site
  return @site if @site
  @site = self.launchrock_client.site_named(attributes[:launchrock][:site])
  raise "No site info found" unless @site
  
  @site
end

#launchrock_usersObject



12
13
14
# File 'lib/launchrock-sync/models/sync_client.rb', line 12

def launchrock_users
  self.launchrock_site.users.map(&:to_hash)
end

#provider_classObject



36
37
38
# File 'lib/launchrock-sync/models/sync_client.rb', line 36

def provider_class
  "LaunchrockSync::SyncClient::#{attributes[:sync][:provider]}Client".constantize
end

#provider_clientObject



32
33
34
# File 'lib/launchrock-sync/models/sync_client.rb', line 32

def provider_client
  @provider_client ||= provider_class.new(attributes[:sync][:options])
end

#sync(&block) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/launchrock-sync/models/sync_client.rb', line 41

def sync(&block)
  users = self.launchrock_users
  timestamp = attributes[:last] ? Time.parse(attributes[:last]) : nil
  if timestamp
    users = users.find_all do |user|
      Time.parse(user[:timestamp]) > timestamp
    end
  end

  users.each do |user|
    t = Time.parse(user[:timestamp])
    timestamp = t if timestamp.nil? || timestamp < t

    self.provider_client.add(user[:email])
    block.call(user) if block
  end

  timestamp
end