defender 1.0.0beta1 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -22,6 +22,13 @@ spam and malicious content in the documents.
22
22
  A document contains content to be analyzed by Defensio, or that has been
23
23
  analyzed.
24
24
 
25
+ Before starting to use Defender, you need to retrieve an API key from
26
+ [Defensio][4]. After getting an API key, you need to let Defender know
27
+ what it is by doing something like this somewhere in your code (before
28
+ doing anything like saving documents):
29
+
30
+ Defender.api_key = 'my-api-key'
31
+
25
32
  Submitting documents to Defensio is really easy. Here's a barebones
26
33
  example:
27
34
 
@@ -122,11 +129,13 @@ This project uses [Semantic Versioning][sv].
122
129
  Author
123
130
  ------
124
131
 
125
- Henrik Hodne :: dvyjones@binaryhex.com :: @dvyjones
132
+ Henrik Hodne :: <dvyjones@binaryhex.com> :: @[dvyjones][5]
126
133
 
127
134
  [0]: http://defensio.com
128
135
  [1]: http://help.github.com/forking/
129
136
  [2]: http://github.com/dvyjones/defender/issues
130
137
  [3]: http://defensio.com/api
138
+ [4]: http://defensio.com/signup/
139
+ [5]: http://twitter.com/dvyjones
131
140
  [sv]: http://semver.org
132
141
  [cb]: http://wiki.github.com/dvyjones/defender/contributing
data/lib/defender.rb CHANGED
@@ -21,4 +21,20 @@ module Defender
21
21
  require 'defensio'
22
22
  @defensio ||= Defensio.new(Defender.api_key, "Defender | #{VERSION} | Henrik Hodne | dvyjones@binaryhex.com")
23
23
  end
24
+
25
+ ##
26
+ # Calls a defensio method and wraps in error handling.
27
+ #
28
+ # Returns false if the method failed, otherwise returns whatever the method returns
29
+ #
30
+ # @param [Symbol] method Which method to call.
31
+ # @return [false, Array(Fixnum, Hash)]
32
+ def self.call(method, *args)
33
+ code, data = defensio.send(method, *args)
34
+ if code == 200 && data['status'] == 'success'
35
+ [code, data]
36
+ else
37
+ false
38
+ end
39
+ end
24
40
  end
@@ -1,4 +1,7 @@
1
1
  module Defender
2
+ ##
3
+ # A document contains data to be analyzed by Defensio, or that has been
4
+ # analyzed.
2
5
  class Document
3
6
  ##
4
7
  # Whether the document should be published on your Web site or not.
@@ -38,15 +41,19 @@ module Defender
38
41
  # content of the request (all of the data in the {#data} hash).
39
42
  #
40
43
  # @param [String] signature The signature of the document to retrieve
41
- # @return [Document] The document to retrieve
44
+ # @return [Document,nil] The document to retrieve, or nil
42
45
  def self.find(signature)
43
46
  document = new
44
- _code, data = Defender.defensio.get_document(signature)
45
- document.instance_variable_set(:@saved, true)
46
- document.instance_variable_set(:@allow, data['allow'])
47
- document.instance_variable_set(:@signature, signature)
47
+ ret = Defender.call(:get_document, signature)
48
+ if ret
49
+ document.instance_variable_set(:@saved, true)
50
+ document.instance_variable_set(:@allow, ret.last['allow'])
51
+ document.instance_variable_set(:@signature, signature)
48
52
 
49
- document
53
+ document
54
+ else
55
+ nil
56
+ end
50
57
  end
51
58
 
52
59
  ##
@@ -71,19 +78,39 @@ module Defender
71
78
  # this).
72
79
  #
73
80
  # @see #saved?
81
+ # @return [Boolean] Whether the save succeded or not.
74
82
  def save
75
83
  if saved?
76
- _code, data = Defender.defensio.put_document(@signature, {:allow => @allow})
84
+ ret = Defender.call(:put_document, @signature, {:allow => @allow})
77
85
  else
78
- data = {}
79
- @data.each { |k,v|
80
- data[k.to_s.gsub('_','-')] = v.to_s
81
- }
82
- _code, data = Defender.defensio.post_document(@data)
83
- @allow = data['allow']
84
- @signature = data['signature']
85
- @saved = true
86
+ ret = Defender.call(:post_document, self.class.normalize_data(@data))
86
87
  end
88
+ return false if ret == false
89
+ return true if saved?
90
+
91
+ data = ret.last
92
+ @allow = data['allow']
93
+ @signature = data['signature']
94
+
95
+ @saved = true # This will also return true, since nothing failed as we got here
96
+ end
97
+
98
+
99
+ ##
100
+ # Normalizes a data hash to submit to defensio.
101
+ #
102
+ # @param [Hash] hsh The hash to be normalized
103
+ # @return [Hash{String => String}] The normalized hash
104
+ def self.normalize_data(data)
105
+ normalized = {}
106
+ data.each { |key, value|
107
+ if value.respond_to?(:strftime)
108
+ value = value.strftime('%Y-%m-%d')
109
+ end
110
+ normalized[key.to_s.gsub('_','-')] = value.to_s
111
+ }
112
+
113
+ normalized
87
114
  end
88
115
  end
89
116
  end
@@ -1,3 +1,3 @@
1
1
  module Defender
2
- VERSION = '1.0.0beta1'
2
+ VERSION = '1.0.0'
3
3
  end
metadata CHANGED
@@ -1,12 +1,12 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: defender
3
3
  version: !ruby/object:Gem::Version
4
- prerelease: true
4
+ prerelease: false
5
5
  segments:
6
6
  - 1
7
7
  - 0
8
- - 0beta1
9
- version: 1.0.0beta1
8
+ - 0
9
+ version: 1.0.0
10
10
  platform: ruby
11
11
  authors:
12
12
  - Henrik Hodne
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-04-06 00:00:00 +02:00
17
+ date: 2010-04-13 00:00:00 +02:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -59,6 +59,20 @@ dependencies:
59
59
  version: 0.5.0
60
60
  type: :development
61
61
  version_requirements: *id003
62
+ - !ruby/object:Gem::Dependency
63
+ name: cucumber
64
+ prerelease: false
65
+ requirement: &id004 !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ~>
68
+ - !ruby/object:Gem::Version
69
+ segments:
70
+ - 0
71
+ - 6
72
+ - 0
73
+ version: 0.6.0
74
+ type: :development
75
+ version_requirements: *id004
62
76
  description: A wrapper of the Defensio spam filtering service.
63
77
  email:
64
78
  - henrik.hodne@binaryhex.com