Class: Lettr::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/lettr/base.rb

Constant Summary collapse

DEFAULT_HEADERS =
{ :accept => :json }

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeBase

Returns a new instance of Base.



11
12
13
14
15
# File 'lib/lettr/base.rb', line 11

def initialize
  resource_args = [ self.class.site_url ]
  resource_args += [self.class.user, self.class.pass] unless Lettr.api_key
  @client = RestClient::Resource.new *resource_args
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



7
8
9
# File 'lib/lettr/base.rb', line 7

def client
  @client
end

Class Method Details

._check_options_for_rendered_mail!(options) ⇒ Object



75
76
77
78
79
80
# File 'lib/lettr/base.rb', line 75

def self._check_options_for_rendered_mail! options
  [:subject, :recipient].each do |opt|
    raise ArgumentError.new ":#{opt} is required" unless options.has_key?( opt )
  end
  raise ArgumentError.new ":html or :text is required" unless (options.has_key?( :text ) || options.has_key?( :html ))
end

._create_rendered_mail(*args) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/lettr/base.rb', line 82

def self._create_rendered_mail *args
  _check_options_for_rendered_mail! args[1]
  mailing = Lettr::RenderedMailing.find args[0].to_s
  mailing.attributes = args[1].merge(:identifier => args[0].to_s)
  mailing
rescue RestClient::ResourceNotFound
  mailing = Lettr::RenderedMailing.new args[1].merge(:identifier => args[0].to_s)
  unless mailing.save
    raise ArgumentError.new mailing.errors.join(' ')
  end
  mailing
end

.api_mailingsObject



95
96
97
# File 'lib/lettr/base.rb', line 95

def self.api_mailings
  @@api_mailings
end

.load_api_mailing_or_fail_loud(*args) ⇒ Object



65
66
67
68
69
70
71
72
73
# File 'lib/lettr/base.rb', line 65

def self.load_api_mailing_or_fail_loud *args
  identifier = args[0]
  api_mailing = self.api_mailings[identifier] ||= Lettr::ApiMailing.find(identifier)
  options = args[1]
  api_mailing.delivery_options = options
  return api_mailing
rescue RestClient::ResourceNotFound => e
  _create_rendered_mail( *args )
end

.site_urlObject



17
18
19
# File 'lib/lettr/base.rb', line 17

def self.site_url
  "#{Lettr.protocol}://#{Lettr.host}/"
end

.subscribe(recipient) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/lettr/base.rb', line 46

def self.subscribe(recipient)
  raise 'Object muss über das Attribut :email verfügen.' unless recipient.respond_to? :email
  rec = Lettr::Recipient.new recipient.email
  Lettr.attributes.each do |attribute|
    if recipient.respond_to? attribute
      rec.send("#{attribute}=", recipient.send(attribute))
    end
  end
  rec.approved = true
  unless rec.save
    raise rec.errors.join(' ')
  end
  rec
end

.unsubscribe(email) ⇒ Object



61
62
63
# File 'lib/lettr/base.rb', line 61

def self.unsubscribe(email)
  Lettr::Recipient.delete_by_email(email)
end

Instance Method Details

#[](path) ⇒ Object



36
37
38
# File 'lib/lettr/base.rb', line 36

def [] path
  client[path]
end

#destroy(object) ⇒ Object



28
29
30
# File 'lib/lettr/base.rb', line 28

def destroy object
  client[object.path].delete headers
end

#find(path) ⇒ Object



32
33
34
# File 'lib/lettr/base.rb', line 32

def find path
  ActiveSupport::JSON.decode(client[path].get headers)
end

#headersObject



40
41
42
43
44
# File 'lib/lettr/base.rb', line 40

def headers
  headers = DEFAULT_HEADERS
  headers.merge! 'X-Lettr-API-key' => Lettr.api_key if Lettr.api_key
  headers
end

#save(object) ⇒ Object



21
22
23
24
25
26
# File 'lib/lettr/base.rb', line 21

def save object
  path = object.collection_path
  payload = object.to_payload
  payload.merge! :files => object.files if object.respond_to?(:files) && object.files
  client[path].post(payload, headers)
end