Class: Lettr::Base
- Inherits:
-
Object
- Object
- Lettr::Base
- Defined in:
- lib/lettr/base.rb
Constant Summary collapse
- DEFAULT_HEADERS =
{ :accept => :json }
Instance Attribute Summary collapse
-
#client ⇒ Object
readonly
Returns the value of attribute client.
Class Method Summary collapse
- ._check_options_for_rendered_mail!(options) ⇒ Object
- ._create_rendered_mail(*args) ⇒ Object
- .api_mailings ⇒ Object
- .load_api_mailing_or_fail_loud(*args) ⇒ Object
- .site_url ⇒ Object
- .subscribe(recipient) ⇒ Object
- .unsubscribe(email) ⇒ Object
Instance Method Summary collapse
- #[](path) ⇒ Object
- #destroy(object) ⇒ Object
- #find(path) ⇒ Object
- #headers ⇒ Object
-
#initialize ⇒ Base
constructor
A new instance of Base.
- #save(object) ⇒ Object
Constructor Details
Instance Attribute Details
#client ⇒ Object (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. [:subject, :recipient].each do |opt| raise ArgumentError.new ":#{opt} is required" unless .has_key?( opt ) end raise ArgumentError.new ":html or :text is required" unless (.has_key?( :text ) || .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 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_mailings ⇒ Object
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) = args[1] api_mailing. = return api_mailing rescue RestClient::ResourceNotFound => e _create_rendered_mail( *args ) end |
.site_url ⇒ Object
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 |
#headers ⇒ Object
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 |