sendgrid_cli 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
data/.travis.yml ADDED
@@ -0,0 +1,6 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.2
4
+ - 1.9.3
5
+ # uncomment this line if your project needs to run something other than `rake`:
6
+ script: bundle exec rspec spec
data/Gemfile ADDED
@@ -0,0 +1,9 @@
1
+ source :rubygems
2
+ gemspec
3
+
4
+ gem 'httparty'
5
+ gem 'json'
6
+
7
+ group :development do
8
+ gem 'rspec'
9
+ end
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 David Ryder
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,48 @@
1
+ # SendgridCli
2
+
3
+ [![Build Status](https://secure.travis-ci.org/libryder/sendgrid_cli.png)](http://travis-ci.org/libryder/sendgrid_cli)
4
+
5
+ This is a basic implementation for a command line interface to the Sendgrid API. It is currently limited
6
+ to the Spam Reports function.
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ gem 'sendgrid_cli'
13
+
14
+ And then execute:
15
+
16
+ $ bundle
17
+
18
+ Or install it yourself as:
19
+
20
+ $ gem install sendgrid_cli
21
+
22
+ ## Usage
23
+
24
+ Sendgrid spam reports utility.
25
+
26
+ Allows you to manage and view your list of spam reports.
27
+
28
+ ```
29
+ sendgrid config user <api_username> :: Configure api username for gem use
30
+ sendgrid config pass <api_secret> :: Configure api password for gem use
31
+ sendgrid get [<email>] :: Get entire list or single blocked recipient.
32
+ sendgrid delete <email> :: Delete single recipient from blocked recipient list.
33
+ ```
34
+
35
+ ####Note
36
+ ```config``` is required to be run before API will work. Config is stored in
37
+
38
+ ```
39
+ ~/.sendgrid_cli.yaml
40
+ ```
41
+
42
+ ## Contributing
43
+
44
+ 1. Fork it
45
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
46
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
47
+ 4. Push to the branch (`git push origin my-new-feature`)
48
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
data/bin/sendgrid ADDED
@@ -0,0 +1,84 @@
1
+ #!/usr/bin/env ruby
2
+ require 'sendgrid_cli'
3
+ require 'fileutils'
4
+ require 'yaml'
5
+
6
+ class SendgridExec
7
+ include SendgridCli
8
+
9
+ def initialize
10
+ begin
11
+ @config = YAML::load( File.open(File.expand_path('~/.sendgrid_cli.yaml' )))
12
+ rescue Errno::ENOENT
13
+ FileUtils.touch(File.expand_path('~/.sendgrid_cli.yaml'))
14
+ @config = {'user' => nil, 'pass' => nil}
15
+ end
16
+ end
17
+
18
+ def build_config
19
+ case ARGV[1]
20
+ when 'user'
21
+ user = ARGV[2]
22
+ pass ||= @config['pass']
23
+ when 'pass'
24
+ pass = ARGV[2]
25
+ user ||= @config['user']
26
+ else
27
+ raise "Invalid config option: '#{ARGV[2]}'. Try 'help' for usage."
28
+ end
29
+
30
+ File.open(File.expand_path('~/.sendgrid_cli.yaml'), "w") do |f|
31
+ f.write({'user' => user, 'pass' => pass}.to_yaml)
32
+ end
33
+ end
34
+
35
+ def run(action, email=nil)
36
+
37
+ if action == 'config'
38
+ build_config
39
+ puts "\nConfiguration successful.\n"
40
+ exit
41
+ end
42
+
43
+ api = SpamReport.new(@config['user'], @config['pass'])
44
+
45
+ case action
46
+ when 'get'
47
+ api.get(email)
48
+ when 'help'
49
+ help
50
+ when 'delete'
51
+ if email
52
+ api.delete(email)
53
+ else
54
+ raise "Please provide an email address to delete."
55
+ end
56
+ else
57
+ raise "Invalid argument: #{action}. Use 'help' for usage."
58
+ end
59
+ end
60
+
61
+ def print_results(result)
62
+ puts result
63
+ puts ''
64
+ puts "-----#{result.count} results------"
65
+ end
66
+
67
+ def help
68
+ puts "\nSendgrid spam reports utility. \n\nAllows you to manage and view your list of spam reports."
69
+
70
+ puts "\nUSAGE:\n"
71
+
72
+ puts "\nconfig user <api_username> :: Configure api username for gem use"
73
+ puts "config pass <api_secret> :: Configure api password for gem use"
74
+ puts "get [<email>] :: Get entire list or single blocked recipient."
75
+ puts "delete <email> :: Delete single recipient from blocked recipient list.\n\n"
76
+ exit
77
+ end
78
+ end
79
+
80
+ cli = SendgridExec.new
81
+ response = cli.run(ARGV[0], ARGV[1])
82
+
83
+ email_list = response.map { |result| result['email'] }
84
+ cli.print_results(email_list)
@@ -0,0 +1,31 @@
1
+ module SendgridCli
2
+
3
+ class Interface
4
+
5
+ def initialize(user, pass)
6
+ @api_user = user
7
+ @api_pass = pass
8
+ if @api_pass.nil? || @api_user.nil?
9
+ raise "Please configure gem before use. Try 'help' for usage."
10
+ end
11
+ end
12
+
13
+ public
14
+ def get(command, email=nil)
15
+ query_api(command, 'get', email)
16
+ end
17
+
18
+ def delete(command, email)
19
+ query_api(command, 'delete', email)
20
+ end
21
+
22
+ private
23
+ def query_api(command, action, email=nil)
24
+ args = "&email=#{email}" unless email.nil?
25
+ send_grid_url = "https://sendgrid.com/api/#{command}.#{action}.json?api_user=#{@api_user}&api_key=#{@api_pass}#{args}"
26
+
27
+ response = HTTParty.get(send_grid_url)
28
+ JSON.parse(response.body)
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,14 @@
1
+ module SendgridCli
2
+ class SpamReport < Interface
3
+
4
+ public
5
+ def get(email=nil)
6
+ super('spamreports', email)
7
+ end
8
+
9
+ def delete(email)
10
+ super('spamreports', email)
11
+ end
12
+
13
+ end
14
+ end
@@ -0,0 +1,3 @@
1
+ module SendgridCli
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,8 @@
1
+ require "sendgrid_cli/version"
2
+ require 'httparty'
3
+ require 'json'
4
+
5
+ module SendgridCli
6
+ require 'sendgrid_cli/interface'
7
+ require 'sendgrid_cli/spam_report'
8
+ end
@@ -0,0 +1,20 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/sendgrid_cli/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["David Ryder"]
6
+ gem.email = ["david@libryder.com"]
7
+ gem.description = %q{Simple command line interface into sendgrid's web API}
8
+ gem.summary = %q{}
9
+ gem.homepage = "http://github.com/libryder/sendgrid_cli"
10
+
11
+ gem.files = `git ls-files`.split($\)
12
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
+ gem.name = "sendgrid_cli"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = SendgridCli::VERSION
17
+
18
+ gem.add_dependency 'httparty'
19
+ gem.add_dependency 'json'
20
+ end
@@ -0,0 +1,38 @@
1
+ require 'spec_helper'
2
+ require 'sendgrid_cli'
3
+
4
+ module SendgridCli
5
+ describe Interface do
6
+ let(:email) { 'test@example.com' }
7
+ let(:api_response) { [{ ip: '127.0.0.1', email: email }] }
8
+
9
+ subject { SendgridCli::Interface }
10
+
11
+ before { subject.stub(query_api: api_response) }
12
+
13
+ context 'when gem is not configured' do
14
+ it 'should raise an exception when not configured' do
15
+ expect { subject.new(nil, nil) }.to raise_error
16
+ end
17
+ end
18
+
19
+ context 'when gem is configured' do
20
+ let(:klass) { subject.new(email, 'password') }
21
+
22
+ describe '#get' do
23
+ it 'call query_api' do
24
+ klass.should_receive(:query_api).with('spamreports', 'get', email)
25
+ klass.get('spamreports', email)
26
+ end
27
+ end
28
+
29
+ describe '#delete' do
30
+ it 'should return a successful status' do
31
+ klass.should_receive(:query_api).with('spamreports', 'delete', email)
32
+ klass.delete('spamreports', email)
33
+ end
34
+ end
35
+ end
36
+
37
+ end
38
+ end
@@ -0,0 +1,25 @@
1
+ require 'spec_helper'
2
+ require 'sendgrid_cli'
3
+
4
+ module SendgridCli
5
+ describe SpamReport do
6
+ let(:email) { 'test@example.com' }
7
+
8
+ subject { SendgridCli::SpamReport.new(email, 'password') }
9
+
10
+ describe '#get' do
11
+ it 'call super with correct arugments' do
12
+ SendgridCli::Interface.any_instance.should_receive(:get).with('spamreports', 'get', email)
13
+ subject.get(email)
14
+ end
15
+ end
16
+
17
+ describe '#delete' do
18
+ it 'should return a successful status' do
19
+ SendgridCli::Interface.any_instance.should_receive(:delete).with('spamreports', 'get', email)
20
+ subject.delete(email)
21
+ end
22
+ end
23
+
24
+ end
25
+ end
@@ -0,0 +1,17 @@
1
+ # This file was generated by the `rspec --init` command. Conventionally, all
2
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
3
+ # Require this file using `require "spec_helper"` to ensure that it is only
4
+ # loaded once.
5
+ #
6
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
7
+ RSpec.configure do |config|
8
+ config.treat_symbols_as_metadata_keys_with_true_values = true
9
+ config.run_all_when_everything_filtered = true
10
+ config.filter_run :focus
11
+
12
+ # Run specs in random order to surface order dependencies. If you find an
13
+ # order dependency and want to debug it, you can fix the order by providing
14
+ # the seed, which is printed after each run.
15
+ # --seed 1234
16
+ config.order = 'random'
17
+ end
metadata ADDED
@@ -0,0 +1,97 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sendgrid_cli
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - David Ryder
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-10-10 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: httparty
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: json
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ description: Simple command line interface into sendgrid's web API
47
+ email:
48
+ - david@libryder.com
49
+ executables:
50
+ - sendgrid
51
+ extensions: []
52
+ extra_rdoc_files: []
53
+ files:
54
+ - .gitignore
55
+ - .rspec
56
+ - .travis.yml
57
+ - Gemfile
58
+ - LICENSE
59
+ - README.md
60
+ - Rakefile
61
+ - bin/sendgrid
62
+ - lib/sendgrid_cli.rb
63
+ - lib/sendgrid_cli/interface.rb
64
+ - lib/sendgrid_cli/spam_report.rb
65
+ - lib/sendgrid_cli/version.rb
66
+ - sendgrid_cli.gemspec
67
+ - spec/lib/interface_spec.rb
68
+ - spec/lib/spam_report_spec.rb
69
+ - spec/spec_helper.rb
70
+ homepage: http://github.com/libryder/sendgrid_cli
71
+ licenses: []
72
+ post_install_message:
73
+ rdoc_options: []
74
+ require_paths:
75
+ - lib
76
+ required_ruby_version: !ruby/object:Gem::Requirement
77
+ none: false
78
+ requirements:
79
+ - - ! '>='
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ required_rubygems_version: !ruby/object:Gem::Requirement
83
+ none: false
84
+ requirements:
85
+ - - ! '>='
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ requirements: []
89
+ rubyforge_project:
90
+ rubygems_version: 1.8.24
91
+ signing_key:
92
+ specification_version: 3
93
+ summary: ''
94
+ test_files:
95
+ - spec/lib/interface_spec.rb
96
+ - spec/lib/spam_report_spec.rb
97
+ - spec/spec_helper.rb