auto_tagging 1.0.1 → 1.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -35,9 +35,9 @@ Specify one or many services you want to use, for example:
35
35
  or
36
36
  AutoTagging.services = [{:open_calais => "open_calais_api_key}]
37
37
  or
38
- AutoTagging.services = ["yahoo"]
38
+ AutoTagging.services = [:yahoo]
39
39
  or
40
- AutoTagging.services = ["delicious"]
40
+ AutoTagging.services = [{:delicious => {"delicious_username" => "delicious_password"}}]
41
41
  or
42
42
  AutoTagging.services = [{:open_calais => "open_calais_api_key}, {:alchemy => "alchemy_api_key"}, "yahoo"]
43
43
 
@@ -27,13 +27,24 @@ module AutoTagging
27
27
  def add_service(service)
28
28
  klass = const(service)
29
29
  klass.api_key = api_key(service) if klass.respond_to?("api_key=")
30
+ klass.credentials = credentials(service) if klass.respond_to?("credentials=")
30
31
  ( self.mains ||= [] ) << klass.new
31
32
  end
32
33
 
34
+ #{:alchemy => "api-key"}
33
35
  def api_key(service)
34
36
  service.values[0].to_s if service.instance_of? Hash
35
37
  end
36
38
 
39
+ #{:delicious => {"username" => "password"}}
40
+ def credentials(service)
41
+ if service.instance_of?(Hash) && service.values[0].instance_of?(Hash)
42
+ service.values[0]
43
+ else
44
+ raise AutoTagging::Errors::InvalidCredentialsError
45
+ end
46
+ end
47
+
37
48
  def service_name(service)
38
49
  ( service.instance_of?(Hash) ? service.keys[0] : service ).to_s
39
50
  end
@@ -3,13 +3,19 @@ require 'auto_tagging/search_param'
3
3
  require 'rexml/document'
4
4
 
5
5
  module AutoTagging
6
- class Delicious
7
- USERNAME = 'auto_tagging_gem'
8
- PASSWORD = 'notsecure'
9
-
6
+ class Delicious
10
7
  API_HOST = 'api.del.icio.us'
11
8
  API_PAGE = '/v1/posts/suggest'
12
9
 
10
+ class << self
11
+ attr_accessor :username, :password
12
+
13
+ def credentials=(credentials)
14
+ self.username = credentials.keys[0]
15
+ self.password = credentials.values[0]
16
+ end
17
+ end
18
+
13
19
  def get_tags(opts)
14
20
  AutoTagging::SearchParam.url_search?(opts) ? api_request(url(opts)) : []
15
21
  end
@@ -21,7 +27,7 @@ module AutoTagging
21
27
 
22
28
  prepared_http.start do |http|
23
29
  req = Net::HTTP::Get.new(api_request_url(url), user_agent)
24
- req.basic_auth(USERNAME, PASSWORD)
30
+ req.basic_auth(self.class.username, self.class.password)
25
31
  parse_response(http.request(req))
26
32
  end
27
33
  rescue SocketError => e
@@ -43,10 +49,16 @@ module AutoTagging
43
49
  {"User-Agent" => "auto_tagging ruby gem"}
44
50
  end
45
51
 
46
- def parse_response(response)
47
- REXML::Document.new(response.body).root.elements.map { |e| e.attributes['tag'] }
48
- rescue
49
- []
52
+ def parse_response(response)
53
+ if response.code == "401"
54
+ raise AutoTagging::Errors::InvalidCredentialsError
55
+ else
56
+ begin
57
+ REXML::Document.new(response.body).root.elements.map { |e| e.attributes['tag'] }
58
+ rescue
59
+ []
60
+ end
61
+ end
50
62
  end
51
63
 
52
64
  def wait_for_next_valid_request
@@ -57,22 +69,4 @@ module AutoTagging
57
69
  "#{URI.encode(AutoTagging::SearchParam.to_valid_url(opts[:url]))}"
58
70
  end
59
71
  end
60
- end
61
-
62
- #ssl certificate ( Chung chi ssl ) was created uniquely for each website
63
- #steps
64
- #browser request server for ssl certificate
65
- #server send back its ssl certificate
66
- #browser then verify for the validity of certificate by sending this certificate to GLobalSign or Verisign
67
- #browser then send back digital number to encode and decode
68
-
69
-
70
- #another steps
71
- #browser make request to a secure url (https)
72
- #web server sends its public key with its certificate
73
- #browser then check for its validity (issued by a trusted party, still valid, related to site contacted)
74
- #browser then use public key to encrypt a random symmetric encryption key and sends it to the server
75
- #The web server decrypts the symmetric encryption key using its private key and uses the symmetric key to decrypt the URL and http data.
76
-
77
- #public key => send out to everybody , let them encrypt some data with this public key and send back the encrypted data
78
- #private key => use to decrypt the encrypted data which is encrypted by the sent out public key
72
+ end
@@ -23,5 +23,11 @@ module AutoTagging
23
23
  super(msg)
24
24
  end
25
25
  end
26
+
27
+ class InvalidCredentialsError < StandardError
28
+ def initialize(msg = "Delicious service is configured without credentials")
29
+ super(msg)
30
+ end
31
+ end
26
32
  end
27
33
  end
@@ -1,3 +1,3 @@
1
1
  module AutoTagging
2
- VERSION = "1.0.1"
2
+ VERSION = "1.1.1"
3
3
  end
@@ -2,30 +2,44 @@ require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
2
2
 
3
3
  describe "AutoTagging::Delicious" do
4
4
  let(:main) { AutoTagging::Delicious.new }
5
+
6
+ context "invalid credentials" do
7
+ let(:url) { "www.facebook.com" }
5
8
 
6
- context "valid options" do
7
- context "with valid url" do
8
- let(:url) { "www.facebook.com" }
9
+ it "should raise AutoTagging::Errors::InvalidCredentialsError" do
10
+ expect{main.get_tags(url: url)}.to raise_error AutoTagging::Errors::InvalidCredentialsError
11
+ end
12
+ end
13
+
14
+ context "valid credentials" do
15
+ let(:valid_credentials) { {"auto_tagging_gem" => "notsecure"} }
9
16
 
10
- it "should return an array" do
11
- main.get_tags(url: url).should_not be_empty
17
+ before(:each) { AutoTagging::Delicious.credentials = valid_credentials }
18
+
19
+ context "valid options" do
20
+ context "with valid url" do
21
+ let(:url) { "www.facebook.com" }
22
+
23
+ it "should return an array" do
24
+ main.get_tags(url: url).should_not be_empty
25
+ end
12
26
  end
13
- end
14
27
 
15
- context "with invalid url" do
16
- let(:url) { "www.amustbeinvalidwebsitebb.com"}
28
+ context "with invalid url" do
29
+ let(:url) { "www.amustbeinvalidwebsitebb.com"}
17
30
 
18
- it "should return an empty array" do
19
- main.get_tags(url: url).should be_empty
31
+ it "should return an empty array" do
32
+ main.get_tags(url: url).should be_empty
33
+ end
20
34
  end
21
35
  end
22
- end
23
36
 
24
- context "invalid options" do
25
- let(:opt) { "an invalid opt" }
37
+ context "invalid options" do
38
+ let(:opt) { "an invalid opt" }
26
39
 
27
- it "should return an empty array" do
28
- main.get_tags(opt).should be_empty
40
+ it "should return an empty array" do
41
+ main.get_tags(opt).should be_empty
42
+ end
29
43
  end
30
- end
44
+ end
31
45
  end
@@ -48,6 +48,61 @@ describe "AutoTagging" do
48
48
  AutoTagging.send(:add_service,service)
49
49
  end.to change(AutoTagging.mains,:size).by(1)
50
50
  end
51
+
52
+ it "should not invoke api_key" do
53
+ AutoTagging.should_not_receive(:api_key).with(service)
54
+ AutoTagging.send(:add_service,service)
55
+ end
56
+
57
+ it "should not invoke credentials" do
58
+ AutoTagging.should_not_receive(:credentials).with(service)
59
+ AutoTagging.send(:add_service,service)
60
+ end
61
+
62
+ context "class respond_to api_key=" do
63
+ before(:each) { klass.stub("api_key=") }
64
+
65
+ it "should invoke api_key" do
66
+ AutoTagging.should_receive(:api_key).once.with(service)
67
+ AutoTagging.send(:add_service,service)
68
+ end
69
+ end
70
+
71
+ context "class respond_to credentials=" do
72
+ before(:each) { klass.stub("credentials=") }
73
+
74
+ it "should invoke credentials" do
75
+ AutoTagging.should_receive(:credentials).once.with(service)
76
+ AutoTagging.send(:add_service,service)
77
+ end
78
+ end
79
+ end
80
+
81
+ describe "#credentials" do
82
+ context "service is not hash" do
83
+ let(:service) { "invalid_service" }
84
+ it "should raise AutoTagging::Errors::InvalidCredentialsError" do
85
+ expect{ AutoTagging.send(:credentials, service) }.to raise_error(AutoTagging::Errors::InvalidCredentialsError)
86
+ end
87
+ end
88
+
89
+ context "service is a hash" do
90
+ let(:service) { { :delicious => value } }
91
+
92
+ context "service value is not a hash" do
93
+ let(:value) { "invalid_value" }
94
+ it "should raise AutoTagging::Errors::InvalidCredentialsError" do
95
+ expect{ AutoTagging.send(:credentials, service) }.to raise_error(AutoTagging::Errors::InvalidCredentialsError)
96
+ end
97
+ end
98
+
99
+ context "service value is a hash" do
100
+ let(:value) { {"username" => "password"} }
101
+ it "should return value" do
102
+ AutoTagging.send(:credentials, service).should == value
103
+ end
104
+ end
105
+ end
51
106
  end
52
107
 
53
108
  describe "#get_tags" do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: auto_tagging
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.1.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: