tipjoy 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.1
1
+ 0.0.2
data/init.rb CHANGED
@@ -3,6 +3,7 @@ dir = File.expand_path(File.dirname(__FILE__))
3
3
  require 'net/http'
4
4
  require 'json'
5
5
 
6
+ require "#{dir}/lib/tipjoy/bulk_initializer.rb"
6
7
  require "#{dir}/lib/tipjoy/tipjoy_requestor.rb"
7
8
  Dir["#{dir}/lib/*.rb"].each do |file|
8
9
  require file
@@ -1,8 +1,10 @@
1
1
  module Tipjoy
2
2
  class Account
3
3
  include Tipjoy::Requestor
4
-
4
+ include Tipjoy::BulkInitializer
5
+
5
6
  EXISTS_PATH = "/api/user/exists/"
7
+ CREATE_PATH = "/api/createTwitterAccount/"
6
8
 
7
9
  attr_accessor :username
8
10
  attr_accessor :user_id
@@ -13,12 +15,21 @@ module Tipjoy
13
15
  attr_accessor :twitter_user_id
14
16
  attr_accessor :profile_image_url
15
17
 
16
- def initialize(options)
18
+ def initialize(options={})
17
19
  assign_attributes(options)
18
20
  end
19
21
 
20
- def self.fetch_account_by_twitter_username(twitter_username)
22
+ def self.create_account(client, twitter_username)
23
+ header = client.oauth_signature_header("http://twitter.com/account/verify_credentials.json")
24
+ json = tipjoy_request(:post, TIPJOY_BASE_URL + EXISTS_PATH, {:twitter_username=> twitter_username, :twitter_oauth_password => header})
25
+ if json['result'] == "success"
26
+ Account.new(json.merge(:twitter_username=> twitter_username))
27
+ else
28
+ nil
29
+ end
30
+ end
21
31
 
32
+ def self.fetch_account_by_twitter_username(twitter_username)
22
33
  json = tipjoy_request(:get, TIPJOY_BASE_URL + EXISTS_PATH + "?twitter_username=" + twitter_username)
23
34
 
24
35
  if json['exists']
@@ -28,12 +39,10 @@ module Tipjoy
28
39
  end
29
40
  end
30
41
 
31
- private
32
-
33
- def assign_attributes(options)
34
- options.each_key do |k|
35
- self.send("#{k}=", options[k]) if respond_to?(k)
36
- end
42
+ def self.fetch_or_create_account_by_twitter_username(client, username)
43
+ existing = fetch_account_by_twitter_username(username)
44
+ return existing unless existing.nil?
45
+ return create_account(client, username)
37
46
  end
38
47
  end
39
48
  end
@@ -0,0 +1,21 @@
1
+ module Tipjoy
2
+ module BulkInitializer
3
+ protected
4
+
5
+ def assign_attributes(options)
6
+ options.each_key do |k|
7
+ method = underscore(k)
8
+ self.send("#{method}=", options[k]) if respond_to?("#{method}=")
9
+ end
10
+ end
11
+
12
+ #from rails
13
+ def underscore(camel_cased_word)
14
+ camel_cased_word.to_s.gsub(/::/, '/').
15
+ gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2').
16
+ gsub(/([a-z\d])([A-Z])/, '\1_\2').
17
+ tr("-", "_").
18
+ downcase
19
+ end
20
+ end
21
+ end
@@ -37,6 +37,8 @@ module Tipjoy
37
37
 
38
38
  def notify_tipjoy
39
39
  json = tipjoy_request(:post,"http://tipjoy.com/api/tweetpayment/notify/", { "tweet_id" => self.tweet_id })
40
+ p json
41
+ Transaction.new(json)
40
42
  end
41
43
 
42
44
  end
@@ -0,0 +1,27 @@
1
+ module Tipjoy
2
+ class Transaction
3
+ # include Tipjoy::Requestor
4
+ include Tipjoy::BulkInitializer
5
+
6
+ attr_accessor :transaction_id,
7
+ :username,
8
+ :userid,
9
+ :date_joined,
10
+ :isLoan,
11
+ :verified,
12
+ :permalink,
13
+ :time,
14
+ :pretty_time,
15
+ :amount,
16
+ :twitter_username,
17
+ :twitter_user_id,
18
+ :profile_image_url,
19
+ :tweet_id,
20
+ :tweet_message,
21
+ :tweet_message_html
22
+
23
+ def initialize(options={})
24
+ assign_attributes(options)
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,11 @@
1
+ module TwitterOAuth
2
+ class Client
3
+ def oauth_signature_header(uri)
4
+ ERB::Util.u(
5
+ consumer.sign!(
6
+ Net::HTTP::Get.new(uri)
7
+ )
8
+ )
9
+ end
10
+ end
11
+ end
@@ -46,6 +46,10 @@ module Test
46
46
  return @props[:users_tweets]
47
47
  end
48
48
 
49
+ def oauth_signature_header(arg)
50
+ "OAuth xxxx"
51
+ end
52
+
49
53
  private
50
54
 
51
55
  def defaults
@@ -2,33 +2,79 @@ require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
2
2
 
3
3
  describe Tipjoy::Account do
4
4
 
5
- describe "integration" do
5
+ before do
6
+ @twitter = Test::TwitterOAuth::Client.new
7
+ end
6
8
 
7
- describe ".fetch_account_by_twitter_username" do
8
- it "should return an account when requested" do
9
- account = Tipjoy::Account.fetch_account_by_twitter_username(EXISTING_ACCOUNT_USERNAME)
10
- account.should_not be_nil
11
- account.twitter_username.should == EXISTING_ACCOUNT_USERNAME
12
- end
9
+ describe ".fetch_account_by_twitter_username" do
10
+ it "should return an account when requested" do
11
+ account = Tipjoy::Account.fetch_account_by_twitter_username(EXISTING_ACCOUNT_USERNAME)
12
+ account.should_not be_nil
13
+ account.twitter_username.should == EXISTING_ACCOUNT_USERNAME
14
+ end
15
+
16
+ it "should return nil if the account is not found" do
17
+ account = Tipjoy::Account.fetch_account_by_twitter_username(NON_EXISTANT_ACCOUNT_USERNAME)
18
+ account.should be_nil
19
+ end
13
20
 
14
- it "should return nil if the account is not found" do
15
- account = Tipjoy::Account.fetch_account_by_twitter_username(NON_EXISTANT_ACCOUNT_USERNAME)
16
- account.should be_nil
21
+ describe "failure" do
22
+ before do
23
+ @requestor = Tipjoy::Account
24
+ @request = Proc.new do
25
+ Tipjoy::Account.fetch_account_by_twitter_username(EXISTING_ACCOUNT_USERNAME)
26
+ end
17
27
  end
18
28
 
19
- # it "should ?? if the account is not specified at all" do
20
- # account = Tipjoy::Account.fetch_account_by_twitter_username(Test::TwitterOAuth::Client.new, '')
21
- # account.should be_nil
22
- # end
29
+ it_should_behave_like "tipjoy request maker that might fail"
23
30
  end
24
31
  end
25
32
 
26
- describe "failure" do
27
- describe ".fetch_account_by_twitter_username" do
33
+ describe ".fetch_or_create_account_by_twitter_username" do
34
+ it "should find existing account" do
35
+ Tipjoy::Account.should_receive(:fetch_account_by_twitter_username).once.and_return(Tipjoy::Account.new)
36
+ Tipjoy::Account.should_receive(:create_account).exactly(0).times
37
+ existing_account = Tipjoy::Account.fetch_or_create_account_by_twitter_username(@twitter, EXISTING_ACCOUNT_USERNAME)
38
+ end
39
+
40
+ it "should create an account if none is found" do
41
+ Tipjoy::Account.should_receive(:fetch_account_by_twitter_username).once.and_return(nil)
42
+ Tipjoy::Account.should_receive(:create_account).once.and_return(Tipjoy::Account.new)
43
+ Tipjoy::Account.fetch_or_create_account_by_twitter_username(@twitter, "new_user")
44
+ end
45
+
46
+ describe "failures" do
28
47
  before do
29
48
  @requestor = Tipjoy::Account
30
49
  @request = Proc.new do
31
- Tipjoy::Account.fetch_account_by_twitter_username(EXISTING_ACCOUNT_USERNAME)
50
+ Tipjoy::Account.fetch_or_create_account_by_twitter_username(@twitter, "new_user")
51
+ end
52
+
53
+ it_should_behave_like "tipjoy request maker that might fail"
54
+ end
55
+ end
56
+ end
57
+
58
+ describe ".create_account" do
59
+ it "should return an account on success" do
60
+ Tipjoy::Account.should_receive(:tipjoy_request).and_return({
61
+ 'result' => 'success',
62
+ 'username' => 'newuser1',
63
+ 'userId' => '1234'
64
+ })
65
+
66
+ account = Tipjoy::Account.create_account(@twitter, 'newuser')
67
+
68
+ account.twitter_username.should == 'newuser'
69
+ account.username.should_not be_nil
70
+ account.user_id.should_not be_nil
71
+ end
72
+
73
+ describe "tipjoy failures" do
74
+ before do
75
+ @requestor = Tipjoy::Account
76
+ @request = Proc.new do
77
+ Tipjoy::Account.create_account(@twitter, 'newuser')
32
78
  end
33
79
  end
34
80
 
@@ -83,11 +83,26 @@ describe Tipjoy::Payment do
83
83
  @payment.pay
84
84
  end
85
85
 
86
- it "should fetch tipjoy payment id" do
86
+ #todo remove mock
87
+ it "should construct a transaction" do
87
88
  @payment.transaction_id.should be_nil
88
89
  @client.should_receive(:update).with("p $1.25 @poopr112 for api test").and_return(@payment_tweet)
89
- @payment.pay
90
- @payment.transaction_id.should_not be_nil
90
+ @payment.should_receive(:tipjoy_request).and_return({:transaction_id => 1})
91
+
92
+ transaction = @payment.pay
93
+
94
+ transaction.should_not be_nil
95
+ transaction.transaction_id.should == 1
96
+ end
97
+
98
+ it "should construct a transaction" do
99
+ @payment.transaction_id.should be_nil
100
+ @client.should_receive(:update).with("p $1.25 @poopr112 for api test").and_return(@payment_tweet)
101
+
102
+ transaction = @payment.pay
103
+
104
+ transaction.should_not be_nil
105
+ transaction.transaction_id.should == 1
91
106
  end
92
107
 
93
108
  describe "failure" do
@@ -0,0 +1,5 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
2
+
3
+ describe Tipjoy::Transaction do
4
+
5
+ end
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tipjoy
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Parker Thompson
@@ -30,14 +30,18 @@ files:
30
30
  - init.rb
31
31
  - lib/tipjoy.rb
32
32
  - lib/tipjoy/account.rb
33
+ - lib/tipjoy/bulk_initializer.rb
33
34
  - lib/tipjoy/invalid_payment_error.rb
34
35
  - lib/tipjoy/payment.rb
35
36
  - lib/tipjoy/request_error.rb
36
37
  - lib/tipjoy/result_error.rb
37
38
  - lib/tipjoy/tipjoy_requestor.rb
39
+ - lib/tipjoy/transaction.rb
40
+ - lib/twitter_oauth_client_extensions.rb
38
41
  - spec/doubles/fake_oauth_client.rb
39
42
  - spec/lib/tipjoy/account_spec.rb
40
43
  - spec/lib/tipjoy/payment_spec.rb
44
+ - spec/lib/tipjoy/transaction_spec.rb
41
45
  - spec/spec_helper.rb
42
46
  - spec/spec_suite.rb
43
47
  - tipjoy.gemspec
@@ -72,5 +76,6 @@ test_files:
72
76
  - spec/doubles/fake_oauth_client.rb
73
77
  - spec/lib/tipjoy/account_spec.rb
74
78
  - spec/lib/tipjoy/payment_spec.rb
79
+ - spec/lib/tipjoy/transaction_spec.rb
75
80
  - spec/spec_helper.rb
76
81
  - spec/spec_suite.rb
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tipjoy
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Parker Thompson
@@ -30,14 +30,18 @@ files:
30
30
  - init.rb
31
31
  - lib/tipjoy.rb
32
32
  - lib/tipjoy/account.rb
33
+ - lib/tipjoy/bulk_initializer.rb
33
34
  - lib/tipjoy/invalid_payment_error.rb
34
35
  - lib/tipjoy/payment.rb
35
36
  - lib/tipjoy/request_error.rb
36
37
  - lib/tipjoy/result_error.rb
37
38
  - lib/tipjoy/tipjoy_requestor.rb
39
+ - lib/tipjoy/transaction.rb
40
+ - lib/twitter_oauth_client_extensions.rb
38
41
  - spec/doubles/fake_oauth_client.rb
39
42
  - spec/lib/tipjoy/account_spec.rb
40
43
  - spec/lib/tipjoy/payment_spec.rb
44
+ - spec/lib/tipjoy/transaction_spec.rb
41
45
  - spec/spec_helper.rb
42
46
  - spec/spec_suite.rb
43
47
  - tipjoy.gemspec
@@ -73,5 +77,6 @@ test_files:
73
77
  - spec/doubles/fake_oauth_client.rb
74
78
  - spec/lib/tipjoy/account_spec.rb
75
79
  - spec/lib/tipjoy/payment_spec.rb
80
+ - spec/lib/tipjoy/transaction_spec.rb
76
81
  - spec/spec_helper.rb
77
82
  - spec/spec_suite.rb