one40_proof 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -89,7 +89,22 @@ Queries are created using a hash and then placed in an array
89
89
  ad.action_urls.friendship_url
90
90
  ad.action_urls.reply_url
91
91
  ad.action_urls.retweet_url
92
+
93
+ # Status
94
+ ad.status.id
92
95
  end
96
+
97
+ # You can also specify what happens if a request fails
98
+ a = One40Proof::Multi::Base.new(queries, :on_fail => "Fail!")
99
+
100
+ # It can also take anything that responds to #call
101
+ # e.g. One40Proof::Multi::Base.new(queries, :on_fail => Proc.new {raise "fail"})
102
+
103
+ # If all our requests fail then:
104
+ a.ads
105
+ #=> ["Fail!", "Fail!", "Fail!"]
106
+
107
+ # If nothing is specified on_fail then a nil object is just placed inside the array
93
108
 
94
109
 
95
110
  <b>Making Single Requests</b>
@@ -120,11 +135,13 @@ Testing ad placement while in development
120
135
  ad.action_urls.reply_url
121
136
  ad.action_urls.retweet_url
122
137
 
138
+ # Status
139
+ ad.status.id
123
140
 
124
141
  To get an ad for a specific user
125
142
 
126
143
  require 'rubygems'
127
- require 'one40_proof'
144
+ require 'one40_proof/simple'
128
145
 
129
146
  ad = One40Proof::UserAd.new(:user_id => 'reddavis', :app_id => 'your app_id')
130
147
 
@@ -132,10 +149,14 @@ To get an ad for a specific user
132
149
  To get an ad for a specific query
133
150
 
134
151
  require 'rubygems'
135
- require 'one40_proof'
152
+ require 'one40_proof/simple'
136
153
 
137
154
  ad = One40Proof::Search.new(:user_id => 'reddavis', :app_id => 'your app_id', :q => 'magic hats')
138
-
155
+
156
+ == Issues
157
+
158
+ Please report any problems or feature requests here[http://github.com/reddavis/One40Proof/issues].
159
+
139
160
  == Copyright
140
161
 
141
162
  Copyright (c) 2010 Red Davis. See LICENSE for details.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.2
1
+ 0.0.3
@@ -0,0 +1,17 @@
1
+ module One40Proof
2
+
3
+ # Raised:
4
+ # * When a user does not exists
5
+ # * When a publisher does not exist
6
+ # * If a targetted ad cannot be found
7
+ class NotFound < Exception; end
8
+
9
+ # Raised
10
+ # * When a user passes an API method that doesnt exist
11
+ class InvalidAPICall < Exception
12
+ def initialize
13
+ super("Check your query hash. Only :search, :test and :user methods allows")
14
+ end
15
+ end
16
+
17
+ end
@@ -1,11 +1,15 @@
1
1
  require 'typhoeus'
2
2
  require 'attributes/ad'
3
+ require 'exceptions'
3
4
 
4
5
  module One40Proof
5
6
  module Multi
6
7
  class Base
7
-
8
- def initialize(ad_requests)
8
+
9
+ # Takes an array of hashs that build the requests.
10
+ # User can also specify an :on_fail option.
11
+ def initialize(ad_requests, options={})
12
+ on_fail = options[:on_fail]
9
13
  hydra = Typhoeus::Hydra.new
10
14
 
11
15
  ad_requests.each do |ad_request|
@@ -15,7 +19,7 @@ module One40Proof
15
19
  request = Typhoeus::Request.new(url, params)
16
20
 
17
21
  request.on_complete do |response|
18
- ads << Ad.new(response.body)
22
+ handle_response(response, on_fail)
19
23
  end
20
24
 
21
25
  hydra.queue(request)
@@ -29,6 +33,15 @@ module One40Proof
29
33
  end
30
34
 
31
35
  private
36
+
37
+ # The Thyphoeus response class gets sent here
38
+ def handle_response(response, on_fail)
39
+ if response.code == 200
40
+ ads << Ad.new(response.body)
41
+ else
42
+ ads << (on_fail.respond_to?(:call) ? on_fail.call : on_fail)
43
+ end
44
+ end
32
45
 
33
46
  # I get undefined method `<=>' for :user_id:Symbol in Ruby 1.8.6
34
47
  def turn_keys_to_strings(hash)
@@ -45,6 +58,8 @@ module One40Proof
45
58
  path = '/ads/user.json'
46
59
  when :search
47
60
  path = '/ads/search.json'
61
+ else
62
+ raise InvalidAPICall.new
48
63
  end
49
64
 
50
65
  base_uri + path
@@ -1,6 +1,6 @@
1
1
  require 'httparty'
2
2
  require 'attributes/ad'
3
- require 'simple/exceptions'
3
+ require 'exceptions'
4
4
 
5
5
  module One40Proof
6
6
  class Base
data/one40_proof.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{one40_proof}
8
- s.version = "0.0.2"
8
+ s.version = "0.0.3"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["reddavis"]
12
- s.date = %q{2010-03-07}
12
+ s.date = %q{2010-03-14}
13
13
  s.description = %q{A Ruby wrapper around the 140Proof API. Documentation can be found here - http://developers.140proof.com/docs/}
14
14
  s.email = %q{reddavis@gmail.com}
15
15
  s.extra_rdoc_files = [
@@ -28,11 +28,11 @@ Gem::Specification.new do |s|
28
28
  "lib/one40_proof/attributes/ad.rb",
29
29
  "lib/one40_proof/attributes/status.rb",
30
30
  "lib/one40_proof/attributes/user.rb",
31
+ "lib/one40_proof/exceptions.rb",
31
32
  "lib/one40_proof/multi.rb",
32
33
  "lib/one40_proof/multi/base.rb",
33
34
  "lib/one40_proof/simple.rb",
34
35
  "lib/one40_proof/simple/base.rb",
35
- "lib/one40_proof/simple/exceptions.rb",
36
36
  "lib/one40_proof/simple/search.rb",
37
37
  "lib/one40_proof/simple/test.rb",
38
38
  "lib/one40_proof/simple/user_ad.rb",
@@ -1,32 +1,49 @@
1
1
  require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
2
2
  require 'one40_proof/multi'
3
+ require 'one40_proof/simple/exceptions'
3
4
 
4
5
  describe "MultiBase" do
5
- before(:all) do
6
- hydra = Typhoeus::Hydra.new
7
- test_response = Response.new(:code => 200, :body => test_ad_data)
8
- user_response = Response.new(:code => 200, :body => user_ad_data)
6
+ describe "Requests" do
9
7
 
10
- # I receive undefined method `request=' for #<WebMock::Response:0x7fc658> without these:
11
- test_response.stub!(:request=)
12
- user_response.stub!(:request=)
8
+ describe "Good Response" do
9
+ before do
10
+ http_stubbing(200)
11
+ @response = One40Proof::Multi::Base.new(queries)
12
+ end
13
13
 
14
- hydra.stub(:get, "http://api.140proof.com/test/ads.json").and_return(test_response)
15
- hydra.stub(:get, "http://api.140proof.com/ads/user.json?app_id=test&user_id=sferik").and_return(user_response)
16
- hydra.stub(:get, "http://api.140proof.com/ads/search.json?app_id=test&q=New+York+Mets&user_id=sferik").and_return(user_response)
14
+ it "should return an Array of Ads" do
15
+ @response.ads.should be_an(Array)
16
+ end
17
+
18
+ it "should create 3 ads" do
19
+ @response.ads.size.should == 3
20
+ end
21
+ end
22
+
23
+ describe "Bad Response" do
24
+ before do
25
+ http_stubbing(404)
26
+ end
17
27
 
18
- Typhoeus::Hydra.stub!(:new).and_return(hydra)
28
+ it "should raise an error" do
29
+ lambda do
30
+ One40Proof::Multi::Base.new(queries, :on_fail => Proc.new {raise One40Proof::NotFound.new})
31
+ end.should raise_error(One40Proof::NotFound)
32
+ end
19
33
 
20
- @a = One40Proof::Multi::Base.new(queries)
21
- @a.ads.should be_an(Array)
22
- end
34
+ it "should include 'hello' in ads array" do
35
+ One40Proof::Multi::Base.new(queries, :on_fail => "Hello").ads.should include('Hello')
36
+ end
37
+ end
23
38
 
24
- it "should return an Array of Ads" do
25
- @a.ads.should be_an(Array)
26
39
  end
27
40
 
28
- it "should create 3 ads" do
29
- @a.ads.size.should == 3
41
+ describe "Querying an invalid 140Proof method" do
42
+ it "should raise One40Proof::InvalidAPICall" do
43
+ lambda do
44
+ One40Proof::Multi::Base.new({:method => :error})
45
+ end.should raise_error(One40Proof::InvalidAPICall)
46
+ end
30
47
  end
31
48
 
32
49
  def queries
@@ -36,4 +53,16 @@ describe "MultiBase" do
36
53
  {:method => :search, :user_id => 'sferik', :app_id => 'test', :q => 'New York Mets'}
37
54
  ]
38
55
  end
56
+
57
+ def http_stubbing(code, data=test_ad_data)
58
+ hydra = Typhoeus::Hydra.new
59
+ response = Response.new(:code => code, :body => data)
60
+ # I receive undefined method 'request=' for WebMock (0.9.1)
61
+ response.stub!(:request=)
62
+ # I receive undefined method 'code' for WebMock (0.9.1)
63
+ response.stub!(:code).and_return(404)
64
+
65
+ hydra.stub(:get, /http:\/\/api.140proof.com/).and_return(response)
66
+ Typhoeus::Hydra.stub!(:new).and_return(hydra)
67
+ end
39
68
  end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 0
8
- - 2
9
- version: 0.0.2
8
+ - 3
9
+ version: 0.0.3
10
10
  platform: ruby
11
11
  authors:
12
12
  - reddavis
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-03-07 00:00:00 +00:00
17
+ date: 2010-03-14 00:00:00 +00:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -76,11 +76,11 @@ files:
76
76
  - lib/one40_proof/attributes/ad.rb
77
77
  - lib/one40_proof/attributes/status.rb
78
78
  - lib/one40_proof/attributes/user.rb
79
+ - lib/one40_proof/exceptions.rb
79
80
  - lib/one40_proof/multi.rb
80
81
  - lib/one40_proof/multi/base.rb
81
82
  - lib/one40_proof/simple.rb
82
83
  - lib/one40_proof/simple/base.rb
83
- - lib/one40_proof/simple/exceptions.rb
84
84
  - lib/one40_proof/simple/search.rb
85
85
  - lib/one40_proof/simple/test.rb
86
86
  - lib/one40_proof/simple/user_ad.rb
@@ -1,8 +0,0 @@
1
- module One40Proof
2
-
3
- # Raised:
4
- # * When a user does not exists
5
- # * When a publisher does not exist
6
- # * If a targetted ad cannot be found
7
- class NotFound < Exception; end
8
- end