mailqun 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/mailqun +198 -0
- metadata +61 -0
    
        checksums.yaml
    ADDED
    
    | @@ -0,0 +1,7 @@ | |
| 1 | 
            +
            ---
         | 
| 2 | 
            +
            SHA1:
         | 
| 3 | 
            +
              metadata.gz: bd52c4bb04fc6ba851e6ebb6bcf33c8bd798810e
         | 
| 4 | 
            +
              data.tar.gz: a9fa98a5782003419ffda122acc2d83c0c1061e1
         | 
| 5 | 
            +
            SHA512:
         | 
| 6 | 
            +
              metadata.gz: 1f1d853191de686a4a577b26371d72f8e919bfd83af3a8e2f47ed1be91bc86bb4f778417a228a16054f4534673f1c9304ad939b3cc9cae231e35ec3c4f18017d
         | 
| 7 | 
            +
              data.tar.gz: 7a6a4243247dfd60e0a7e1d04ff5a61d2858f08720042ebbffc6cb21b440df98dcd6665a5a634e20f4c89d4e21ea5e7281f8aee7c8b8cce4251ff1f3584f1e8b
         | 
    
        data/mailqun
    ADDED
    
    | @@ -0,0 +1,198 @@ | |
| 1 | 
            +
            #!/usr/bin/env ruby
         | 
| 2 | 
            +
            require 'net/http'
         | 
| 3 | 
            +
            require 'optparse'
         | 
| 4 | 
            +
            require 'ostruct'
         | 
| 5 | 
            +
            require 'json'
         | 
| 6 | 
            +
            require 'colorize'
         | 
| 7 | 
            +
             | 
| 8 | 
            +
            def load_env
         | 
| 9 | 
            +
              @domain = ENV['MAILGUN_DOMAIN']
         | 
| 10 | 
            +
              @secret = ENV['MAILGUN_SECRET']
         | 
| 11 | 
            +
            end
         | 
| 12 | 
            +
             | 
| 13 | 
            +
            def load_uri
         | 
| 14 | 
            +
              if @query_type == 'view'
         | 
| 15 | 
            +
                abort("Error: Where's the url, bro?") if @url.nil?
         | 
| 16 | 
            +
                url = @url
         | 
| 17 | 
            +
              elsif @query_type == 'bounces' and @address
         | 
| 18 | 
            +
                url = "https://api.mailgun.net/v3/#{@domain}/#{@query_type}/#{@address}"
         | 
| 19 | 
            +
              else
         | 
| 20 | 
            +
                query = URI.encode_www_form(@options.to_h)
         | 
| 21 | 
            +
                url = "https://api.mailgun.net/v3/#{@domain}/#{@query_type}?#{query}"
         | 
| 22 | 
            +
              end
         | 
| 23 | 
            +
              return URI(url)
         | 
| 24 | 
            +
            end
         | 
| 25 | 
            +
             | 
| 26 | 
            +
            def request uri
         | 
| 27 | 
            +
              response = nil
         | 
| 28 | 
            +
              Net::HTTP.start(uri.host, uri.port, :use_ssl => uri.scheme == 'https') do |http|
         | 
| 29 | 
            +
                klass = Object.const_get("Net::HTTP::#{@verb.capitalize}")
         | 
| 30 | 
            +
                request = klass.new uri.request_uri
         | 
| 31 | 
            +
                request.basic_auth 'api', @secret
         | 
| 32 | 
            +
                response = http.request request
         | 
| 33 | 
            +
              end
         | 
| 34 | 
            +
              return response
         | 
| 35 | 
            +
            end
         | 
| 36 | 
            +
             | 
| 37 | 
            +
            def get_json_response
         | 
| 38 | 
            +
              response = request load_uri
         | 
| 39 | 
            +
              unless response.kind_of? Net::HTTPSuccess
         | 
| 40 | 
            +
                abort("Error: Response code was #{response.code}\n#{response.body}")
         | 
| 41 | 
            +
              end
         | 
| 42 | 
            +
              JSON.parse(response.body)
         | 
| 43 | 
            +
            end
         | 
| 44 | 
            +
             | 
| 45 | 
            +
            def get_filename filename
         | 
| 46 | 
            +
              filename.gsub!(/^.*(\\|\/)/, '')
         | 
| 47 | 
            +
              filename.gsub!(/[^0-9A-Za-z.\-]/, '_')
         | 
| 48 | 
            +
              "#{filename.strip}_#{Time.now.to_i}.html"
         | 
| 49 | 
            +
            end
         | 
| 50 | 
            +
             | 
| 51 | 
            +
            def pretty_print_event_item item
         | 
| 52 | 
            +
              event = item['event'].upcase
         | 
| 53 | 
            +
              pretty_line = "#{Time.at(item['timestamp'])}: [#{event}]"
         | 
| 54 | 
            +
              pretty_line = "#{pretty_line}\t[#{item['tags'].join(',')}]";
         | 
| 55 | 
            +
              pretty_line = "#{pretty_line} #{item['message']['headers']['to']}"
         | 
| 56 | 
            +
             | 
| 57 | 
            +
              if event == 'FAILED'
         | 
| 58 | 
            +
                pretty_line = "#{pretty_line}\t- #{item['delivery-status']['code']}"\
         | 
| 59 | 
            +
                              " - #{item['delivery-status']['description']}"
         | 
| 60 | 
            +
                puts "#{pretty_line}".red
         | 
| 61 | 
            +
              elsif ['DELIVERED', 'OPENED'].include? event
         | 
| 62 | 
            +
                puts "#{pretty_line}".green
         | 
| 63 | 
            +
              else
         | 
| 64 | 
            +
                puts "#{pretty_line}"
         | 
| 65 | 
            +
              end
         | 
| 66 | 
            +
            end
         | 
| 67 | 
            +
             | 
| 68 | 
            +
            def pretty_print_bounce_item item
         | 
| 69 | 
            +
              pretty_line = "#{item['created_at']}: #{item['address']}"
         | 
| 70 | 
            +
              pretty_line = "#{pretty_line}\t- #{item['error'].red}";
         | 
| 71 | 
            +
             | 
| 72 | 
            +
              puts "#{pretty_line}"
         | 
| 73 | 
            +
            end
         | 
| 74 | 
            +
             | 
| 75 | 
            +
            def pretty_print_item item
         | 
| 76 | 
            +
              if @query_type == 'bounces'
         | 
| 77 | 
            +
                pretty_print_bounce_item item
         | 
| 78 | 
            +
              else
         | 
| 79 | 
            +
                pretty_print_event_item item
         | 
| 80 | 
            +
              end
         | 
| 81 | 
            +
            end
         | 
| 82 | 
            +
             | 
| 83 | 
            +
            def pretty_print json_response
         | 
| 84 | 
            +
              json_response['items'].each do |item|
         | 
| 85 | 
            +
                pretty_print_item item
         | 
| 86 | 
            +
              end
         | 
| 87 | 
            +
            end
         | 
| 88 | 
            +
             | 
| 89 | 
            +
            def handle_fetch json_response
         | 
| 90 | 
            +
              if @options.pretty and !json_response['items'].nil?
         | 
| 91 | 
            +
                pretty_print json_response
         | 
| 92 | 
            +
              else
         | 
| 93 | 
            +
                puts JSON.pretty_generate(json_response)
         | 
| 94 | 
            +
              end
         | 
| 95 | 
            +
              p @options.to_h
         | 
| 96 | 
            +
            end
         | 
| 97 | 
            +
             | 
| 98 | 
            +
            def handle_view json_response
         | 
| 99 | 
            +
              filename = get_filename json_response['subject']
         | 
| 100 | 
            +
              open("#{filename}", 'w') do |f|
         | 
| 101 | 
            +
                f.puts json_response['body-html']
         | 
| 102 | 
            +
              end
         | 
| 103 | 
            +
              system "open #{filename}"
         | 
| 104 | 
            +
            end
         | 
| 105 | 
            +
             | 
| 106 | 
            +
            def build_output json_response
         | 
| 107 | 
            +
              if @query_type == 'view'
         | 
| 108 | 
            +
                handle_view json_response
         | 
| 109 | 
            +
              else
         | 
| 110 | 
            +
                handle_fetch json_response
         | 
| 111 | 
            +
              end
         | 
| 112 | 
            +
            end
         | 
| 113 | 
            +
             | 
| 114 | 
            +
            @options = OpenStruct.new
         | 
| 115 | 
            +
            @options.limit = 10
         | 
| 116 | 
            +
            @options.pretty = true
         | 
| 117 | 
            +
            @query_type = 'events'
         | 
| 118 | 
            +
            @verb = 'GET'
         | 
| 119 | 
            +
             | 
| 120 | 
            +
            OptionParser.new do |opts|
         | 
| 121 | 
            +
              opts.banner = "Usage: mailqun [options]"
         | 
| 122 | 
            +
             | 
| 123 | 
            +
              opts.separator ""
         | 
| 124 | 
            +
              opts.separator "Common options:"
         | 
| 125 | 
            +
             | 
| 126 | 
            +
              opts.on("-f [FROM]", "--from [FROM]", String,
         | 
| 127 | 
            +
                      "Address of the sender") do |f|
         | 
| 128 | 
            +
                @options.from = f
         | 
| 129 | 
            +
              end
         | 
| 130 | 
            +
             | 
| 131 | 
            +
              opts.on("-t [TO]", "--to [TO]", String,
         | 
| 132 | 
            +
                      "Address of the receiver") do |t|
         | 
| 133 | 
            +
                @options.to = t
         | 
| 134 | 
            +
              end
         | 
| 135 | 
            +
             | 
| 136 | 
            +
              opts.on("-s [SUBJECT]", "--subject [SUBJECT]", String,
         | 
| 137 | 
            +
                      "Subject of mail") do |s|
         | 
| 138 | 
            +
                @options.subject = s
         | 
| 139 | 
            +
              end
         | 
| 140 | 
            +
             | 
| 141 | 
            +
              opts.on("-l [LIMIT]", "--limit [LIMIT]", Integer,
         | 
| 142 | 
            +
                      "Limit on number of results") do |l|
         | 
| 143 | 
            +
                @options.limit = l
         | 
| 144 | 
            +
              end
         | 
| 145 | 
            +
             | 
| 146 | 
            +
              opts.on("--tags [TAG]", String,
         | 
| 147 | 
            +
                      "Value in X-Mailgun-Tag header") do |t|
         | 
| 148 | 
            +
                @options.tags = t
         | 
| 149 | 
            +
              end
         | 
| 150 | 
            +
             | 
| 151 | 
            +
              opts.on("-e [EVENT]", "--event [EVENT]", String,
         | 
| 152 | 
            +
                      "Event being queried") do |e|
         | 
| 153 | 
            +
                @options.event = e
         | 
| 154 | 
            +
              end
         | 
| 155 | 
            +
             | 
| 156 | 
            +
              opts.on("--raw",
         | 
| 157 | 
            +
                      "Gives raw json response, instead of summary") do |e|
         | 
| 158 | 
            +
                @options.pretty = false
         | 
| 159 | 
            +
              end
         | 
| 160 | 
            +
             | 
| 161 | 
            +
              opts.separator ""
         | 
| 162 | 
            +
              opts.separator "Bounce options:"
         | 
| 163 | 
            +
             | 
| 164 | 
            +
              opts.on("--bounces",
         | 
| 165 | 
            +
                      "Add this option to query bounces instead of events") do |e|
         | 
| 166 | 
            +
                @query_type = 'bounces'
         | 
| 167 | 
            +
              end
         | 
| 168 | 
            +
             | 
| 169 | 
            +
              opts.on("-a [ADDRESS]", "--address [ADDRESS]", String,
         | 
| 170 | 
            +
                      "Bounced address, used with --bounces") do |a|
         | 
| 171 | 
            +
                if @query_type == 'bounces'
         | 
| 172 | 
            +
                  @address = a
         | 
| 173 | 
            +
                end
         | 
| 174 | 
            +
              end
         | 
| 175 | 
            +
             | 
| 176 | 
            +
              opts.on("--delete [ADDRESS]", String,
         | 
| 177 | 
            +
                      "Delete address from bounces, used with --bounces") do |a|
         | 
| 178 | 
            +
                if @query_type == 'bounces'
         | 
| 179 | 
            +
                  @address = a
         | 
| 180 | 
            +
                  @verb = 'DELETE'
         | 
| 181 | 
            +
                end
         | 
| 182 | 
            +
              end
         | 
| 183 | 
            +
             | 
| 184 | 
            +
              opts.separator ""
         | 
| 185 | 
            +
              opts.separator "View options:"
         | 
| 186 | 
            +
             | 
| 187 | 
            +
              opts.on("--view-body [URL]",
         | 
| 188 | 
            +
                      "Mailgun storage URL to fetch email body from") do |u|
         | 
| 189 | 
            +
                @url = u
         | 
| 190 | 
            +
                @query_type = 'view'
         | 
| 191 | 
            +
              end
         | 
| 192 | 
            +
            end.parse!
         | 
| 193 | 
            +
             | 
| 194 | 
            +
            load_env
         | 
| 195 | 
            +
             | 
| 196 | 
            +
            json_response = get_json_response
         | 
| 197 | 
            +
             | 
| 198 | 
            +
            build_output json_response
         | 
    
        metadata
    ADDED
    
    | @@ -0,0 +1,61 @@ | |
| 1 | 
            +
            --- !ruby/object:Gem::Specification
         | 
| 2 | 
            +
            name: mailqun
         | 
| 3 | 
            +
            version: !ruby/object:Gem::Version
         | 
| 4 | 
            +
              version: 0.1.0
         | 
| 5 | 
            +
            platform: ruby
         | 
| 6 | 
            +
            authors:
         | 
| 7 | 
            +
            - Harman Singh
         | 
| 8 | 
            +
            autorequire: 
         | 
| 9 | 
            +
            bindir: "."
         | 
| 10 | 
            +
            cert_chain: []
         | 
| 11 | 
            +
            date: 2017-11-10 00:00:00.000000000 Z
         | 
| 12 | 
            +
            dependencies:
         | 
| 13 | 
            +
            - !ruby/object:Gem::Dependency
         | 
| 14 | 
            +
              name: colorize
         | 
| 15 | 
            +
              requirement: !ruby/object:Gem::Requirement
         | 
| 16 | 
            +
                requirements:
         | 
| 17 | 
            +
                - - "~>"
         | 
| 18 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 19 | 
            +
                    version: 0.8.1
         | 
| 20 | 
            +
              type: :runtime
         | 
| 21 | 
            +
              prerelease: false
         | 
| 22 | 
            +
              version_requirements: !ruby/object:Gem::Requirement
         | 
| 23 | 
            +
                requirements:
         | 
| 24 | 
            +
                - - "~>"
         | 
| 25 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 26 | 
            +
                    version: 0.8.1
         | 
| 27 | 
            +
            description: Mailgun's website is stupid, so this script lets you query events from
         | 
| 28 | 
            +
              commandline.
         | 
| 29 | 
            +
            email:
         | 
| 30 | 
            +
            - harman28@gmail.com
         | 
| 31 | 
            +
            executables:
         | 
| 32 | 
            +
            - mailqun
         | 
| 33 | 
            +
            extensions: []
         | 
| 34 | 
            +
            extra_rdoc_files: []
         | 
| 35 | 
            +
            files:
         | 
| 36 | 
            +
            - "./mailqun"
         | 
| 37 | 
            +
            homepage: https://github.com/harman28/mailqun
         | 
| 38 | 
            +
            licenses:
         | 
| 39 | 
            +
            - MIT
         | 
| 40 | 
            +
            metadata: {}
         | 
| 41 | 
            +
            post_install_message: 
         | 
| 42 | 
            +
            rdoc_options: []
         | 
| 43 | 
            +
            require_paths:
         | 
| 44 | 
            +
            - "."
         | 
| 45 | 
            +
            required_ruby_version: !ruby/object:Gem::Requirement
         | 
| 46 | 
            +
              requirements:
         | 
| 47 | 
            +
              - - ">="
         | 
| 48 | 
            +
                - !ruby/object:Gem::Version
         | 
| 49 | 
            +
                  version: '0'
         | 
| 50 | 
            +
            required_rubygems_version: !ruby/object:Gem::Requirement
         | 
| 51 | 
            +
              requirements:
         | 
| 52 | 
            +
              - - ">="
         | 
| 53 | 
            +
                - !ruby/object:Gem::Version
         | 
| 54 | 
            +
                  version: '0'
         | 
| 55 | 
            +
            requirements: []
         | 
| 56 | 
            +
            rubyforge_project: 
         | 
| 57 | 
            +
            rubygems_version: 2.5.1
         | 
| 58 | 
            +
            signing_key: 
         | 
| 59 | 
            +
            specification_version: 4
         | 
| 60 | 
            +
            summary: Command line query tool for Mailgun, cos their website is rubbish.
         | 
| 61 | 
            +
            test_files: []
         |