Sha256: 31d9a82cc3ec64814cb5116663c76d0467336bd480f3b497a4c8e60801709812

Contents?: true

Size: 1.07 KB

Versions: 1

Compression:

Stored size: 1.07 KB

Contents

module Sendicate
  
  class Import
    include HTTParty
    base_uri 'https://api.sendicate.net/v1'
    headers 'Accept' => 'application/json', 'Content-Type' => 'application/json', "Authorization" => "token #{Sendicate.api_token}"
    
    attr_reader :list_id, :data, :response, :errors
    
    def initialize(attributes)
      @list_id = attributes[:list_id]
      @data = attributes[:data]
    end
    
    def save
      @response = Sendicate::Request.post("/lists/#{list_id}/subscribers", body: MultiJson.dump(data))
      success?
    end
    
    def success?
      response && response.success?
    end
    
    def imported
      parsed_response["imported"]
    end
    
    def updated
      parsed_response["updated"]
    end
    
    def failed
      parsed_response["failed"]
    end
    
    def parsed_response
      response && response.parsed_response
    end
    
    def errors
      parsed_response['errors']
    end
    
    def error_messages
      errors.map do |error|
        error['errors'].map do |e|
          e['message']
        end
      end.flatten
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
sendicate-0.1.0 lib/sendicate/import.rb