empireavenue 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (71) hide show
  1. data/LICENSE.md +44 -0
  2. data/README.md +30 -0
  3. data/Rakefile +8 -0
  4. data/empireavenue.gemspec +27 -0
  5. data/lib/empireavenue/action/buy_group_spec.rb +31 -0
  6. data/lib/empireavenue/api/arguments.rb +13 -0
  7. data/lib/empireavenue/api/portfolio_base.rb +13 -0
  8. data/lib/empireavenue/api/portfolio_get.rb +13 -0
  9. data/lib/empireavenue/api/profile_bank_balance.rb +9 -0
  10. data/lib/empireavenue/api/profile_info.rb +11 -0
  11. data/lib/empireavenue/api/profile_shareholders.rb +13 -0
  12. data/lib/empireavenue/api/search_recent.rb +9 -0
  13. data/lib/empireavenue/api/shares_buy.rb +11 -0
  14. data/lib/empireavenue/api/shares_sell.rb +11 -0
  15. data/lib/empireavenue/base.rb +126 -0
  16. data/lib/empireavenue/basic_user.rb +10 -0
  17. data/lib/empireavenue/client.rb +89 -0
  18. data/lib/empireavenue/configurable.rb +84 -0
  19. data/lib/empireavenue/default.rb +97 -0
  20. data/lib/empireavenue/entity.rb +7 -0
  21. data/lib/empireavenue/error/client_error.rb +35 -0
  22. data/lib/empireavenue/error/configuration_error.rb +8 -0
  23. data/lib/empireavenue/error/decode_error.rb +9 -0
  24. data/lib/empireavenue/error/forbidden.rb +10 -0
  25. data/lib/empireavenue/error/identity_map_key_error.rb +9 -0
  26. data/lib/empireavenue/error/internal_server_error.rb +11 -0
  27. data/lib/empireavenue/error/not_found.rb +10 -0
  28. data/lib/empireavenue/error/server_error.rb +28 -0
  29. data/lib/empireavenue/error/service_unavailable.rb +11 -0
  30. data/lib/empireavenue/error.rb +33 -0
  31. data/lib/empireavenue/identity.rb +51 -0
  32. data/lib/empireavenue/identity_map.rb +22 -0
  33. data/lib/empireavenue/response/parse_json.rb +22 -0
  34. data/lib/empireavenue/response/raise_error.rb +31 -0
  35. data/lib/empireavenue/user.rb +9 -0
  36. data/lib/empireavenue/version.rb +18 -0
  37. data/lib/empireavenue.rb +32 -0
  38. data/spec/empireavenue/api/portfolio/portfolio_base_spec.rb +49 -0
  39. data/spec/empireavenue/api/portfolio/portfolio_base_spec.rb~ +49 -0
  40. data/spec/empireavenue/api/portfolio/portfolio_get_spec.rb +49 -0
  41. data/spec/empireavenue/api/portfolio/portfolio_get_spec.rb~ +39 -0
  42. data/spec/empireavenue/api/profile/profile_bank_balance_spec.rb +18 -0
  43. data/spec/empireavenue/api/profile/profile_bank_balance_spec.rb~ +18 -0
  44. data/spec/empireavenue/api/profile/profile_info_spec.rb +38 -0
  45. data/spec/empireavenue/api/profile/profile_info_spec.rb~ +40 -0
  46. data/spec/empireavenue/api/profile/profile_shareholders_spec.rb +50 -0
  47. data/spec/empireavenue/api/profile/profile_shareholders_spec.rb~ +50 -0
  48. data/spec/empireavenue/api/search/search_recent_spec.rb +16 -0
  49. data/spec/empireavenue/api/search/search_recent_spec.rb~ +16 -0
  50. data/spec/empireavenue/api/shares/shares_buy_spec.rb +31 -0
  51. data/spec/empireavenue/api/shares/shares_buy_spec.rb~ +30 -0
  52. data/spec/empireavenue/api/shares/shares_sell_spec.rb +31 -0
  53. data/spec/empireavenue/api/shares/shares_sell_spec.rb~ +31 -0
  54. data/spec/empireavenue/base_spec.rb +111 -0
  55. data/spec/empireavenue/basic_user_spec.rb +23 -0
  56. data/spec/empireavenue/basic_user_spec.rb~ +23 -0
  57. data/spec/empireavenue/client_spec.rb +149 -0
  58. data/spec/empireavenue/client_spec.rb~ +149 -0
  59. data/spec/empireavenue/error/client_error_spec.rb +27 -0
  60. data/spec/empireavenue/error/client_error_spec.rb~ +27 -0
  61. data/spec/empireavenue/error/server_error_spec.rb +20 -0
  62. data/spec/empireavenue/error/server_error_spec.rb~ +20 -0
  63. data/spec/empireavenue/error_spec.rb +20 -0
  64. data/spec/empireavenue/error_spec.rb~ +20 -0
  65. data/spec/empireavenue/identifiable_spec.rb +50 -0
  66. data/spec/empireavenue/identifiable_spec.rb~ +50 -0
  67. data/spec/factory_classes.rb +137 -0
  68. data/spec/factory_classes.rb~ +137 -0
  69. data/spec/helper.rb +53 -0
  70. data/spec/helper.rb~ +51 -0
  71. metadata +219 -0
@@ -0,0 +1,40 @@
1
+ require 'helper'
2
+
3
+ describe EmpireAvenue::API::Profile do
4
+
5
+ subject do
6
+ EmpireAvenue::Client.new(:client_id => "CK", :client_secret => "CS", :oauth_token => "OT", :oauth_token_secret => "OS")
7
+ end
8
+
9
+ before(:all) do
10
+ @txwikinger_info = FactoryGirl.build(:profile_info_data, ticker: "TXWIKINGER")
11
+ @avidbeaver_info = FactoryGirl.build(:profile_info_data, ticker: "AVIDBEAVER")
12
+ end
13
+
14
+ it "gets profile info without ticker" do
15
+ stub_get("/profile/info").with(:query => {:client_id => subject.client_id, :access_token => subject.oauth_token}).to_return(:body => FactoryGirl.build(:response, data: [@txwikinger_info]).to_json, :headers => {:content_type => "application/json; charset=utf-8"})
16
+ info = subject.profile_info
17
+ expect(a_get("/profile/info").with(:query => {:client_id => subject.client_id, :access_token => subject.oauth_token})).to have_been_made
18
+ expect(info[:body][:meta][:total_rows]).to eq 1
19
+ expect(info[:body][:data][0][:ticker]).to eq "TXWIKINGER"
20
+ end
21
+
22
+ it "gets profile info with ticker" do
23
+ stub_get("/profile/info").with(:query => {:client_id => subject.client_id, :access_token => subject.oauth_token, :ticker => "AVIDBEAVER"}).to_return(:body => FactoryGirl.build(:response, data: [@avidbeaver_info]).to_json, :headers => {:content_type => "application/json; charset=utf-8"})
24
+ info = subject.profile_info("AVIDBEAVER")
25
+ expect(a_get("/profile/info").with(:query => {:ticker => "AVIDBEAVER", :client_id => subject.client_id, :access_token => subject.oauth_token})).to have_been_made
26
+ expect(info[:body][:meta][:total_rows]).to eq 1
27
+ expect(info[:body][:data][0][:ticker]).to eq "AVIDBEAVER"
28
+ end
29
+
30
+ it "gets profile info with multiple tickers" do
31
+ stub_get("/profile/info").with(:query => {:ticker => "TXWIKINGER,AVIDBEAVER", :client_id => subject.client_id, :access_token => subject.oauth_token}).to_return(:body => FactoryGirl.build(:response, data: [@txwikinger_info, @avidbeaver_info]).to_json, :headers => {:content_type => "application/json; charset=utf-8"})
32
+ info = subject.profile_info(["TXWIKINGER", "AVIDBEAVER"])
33
+ expect(a_get("/profile/info").with(:query => {:ticker => "TXWIKINGER,AVIDBEAVER", :client_id => subject.client_id, :access_token => subject.oauth_token})).to have_been_made
34
+ expect(info[:body][:meta][:total_rows]).to eq 2
35
+ expect(info[:body][:data][0][:ticker]).to eq "TXWIKINGER"
36
+ expect(info[:body][:data][1][:ticker]).to eq "AVIDBEAVER"
37
+ end
38
+
39
+
40
+ end
@@ -0,0 +1,50 @@
1
+ require 'helper'
2
+
3
+ describe EmpireAvenue::API::Profile do
4
+
5
+ subject do
6
+ EmpireAvenue::Client.new(:client_id => "CK", :client_secret => "CS", :oauth_token => "OT", :oauth_token_secret => "OS")
7
+ end
8
+
9
+ before(:all) do
10
+ @txwikinger_shareholders = FactoryGirl.build(:profile_shareholders_data, ticker: "TXWIKINGER")
11
+ @avidbeaver_shareholders = FactoryGirl.build(:profile_shareholders_data, ticker: "AVIDBEAVER")
12
+ end
13
+
14
+ it "gets profile shareholders without ticker" do
15
+ stub_get("/profile/shareholders").with(:query => {:client_id => subject.client_id, :access_token => subject.oauth_token}).to_return(:body => FactoryGirl.build(:response, data: [@txwikinger_shareholders]).to_json, :headers => {:content_type => "application/json; charset=utf-8"})
16
+ shareholders = subject.profile_shareholders
17
+ expect(a_get("/profile/shareholders").with(:query => {:client_id => subject.client_id, :access_token => subject.oauth_token})).to have_been_made
18
+ expect(shareholders[:body][:meta][:total_rows]).to eq 1
19
+ expect(shareholders[:body][:data][0][:ticker]).to eq "TXWIKINGER"
20
+ end
21
+
22
+ it "gets profile shareholders with ticker" do
23
+ stub_get("/profile/shareholders").with(:query => {:client_id => subject.client_id, :access_token => subject.oauth_token, :ticker => "AVIDBEAVER"}).to_return(:body => FactoryGirl.build(:response, data: [@avidbeaver_shareholders]).to_json, :headers => {:content_type => "application/json; charset=utf-8"})
24
+ shareholders = subject.profile_shareholders("AVIDBEAVER")
25
+ expect(a_get("/profile/shareholders").with(:query => {:ticker => "AVIDBEAVER", :client_id => subject.client_id, :access_token => subject.oauth_token})).to have_been_made
26
+ expect(shareholders[:body][:meta][:total_rows]).to eq 1
27
+ expect(shareholders[:body][:data][0][:ticker]).to eq "AVIDBEAVER"
28
+ end
29
+
30
+ it "gets profile shareholders page 2" do
31
+ stub_get("/profile/shareholders").with(:query => {:page => 2, :client_id => subject.client_id, :access_token => subject.oauth_token}).to_return(:body => FactoryGirl.build(:response, requested_user: "AVIDBEAVER", requested_page: 2, data: [@txwikinger_shareholders]).to_json, :headers => {:content_type => "application/json; charset=utf-8"})
32
+ shareholders = subject.profile_shareholders(nil, 2)
33
+ expect(a_get("/profile/shareholders").with(:query => {:page => 2, :client_id => subject.client_id, :access_token => subject.oauth_token})).to have_been_made
34
+ expect(shareholders[:body][:meta][:total_rows]).to eq 1
35
+ expect(shareholders[:body][:meta][:requested_user]).to eq "AVIDBEAVER"
36
+ expect(shareholders[:body][:meta][:requested_page]).to eq 2
37
+ expect(shareholders[:body][:data][0][:ticker]).to eq "TXWIKINGER"
38
+ end
39
+
40
+ it "gets profile shareholders limit to 1 result" do
41
+ stub_get("/profile/shareholders").with(:query => {:maxresults => 1, :client_id => subject.client_id, :access_token => subject.oauth_token}).to_return(:body => FactoryGirl.build(:response, requested_user: "AVIDBEAVER", requested_page: 1, data: [@txwikinger_shareholders]).to_json, :headers => {:content_type => "application/json; charset=utf-8"})
42
+ shareholders = subject.profile_shareholders(nil, nil, 1)
43
+ expect(a_get("/profile/shareholders").with(:query => {:maxresults => 1, :client_id => subject.client_id, :access_token => subject.oauth_token})).to have_been_made
44
+ expect(shareholders[:body][:meta][:total_rows]).to eq 1
45
+ expect(shareholders[:body][:meta][:requested_user]).to eq "AVIDBEAVER"
46
+ expect(shareholders[:body][:meta][:requested_page]).to eq 1
47
+ expect(shareholders[:body][:data][0][:ticker]).to eq "TXWIKINGER"
48
+ end
49
+
50
+ end
@@ -0,0 +1,50 @@
1
+ require 'helper'
2
+
3
+ describe EmpireAvenue::API::Profile do
4
+
5
+ subject do
6
+ EmpireAvenue::Client.new(:client_id => "CK", :client_secret => "CS", :oauth_token => "OT", :oauth_token_secret => "OS")
7
+ end
8
+
9
+ before(:all) do
10
+ @txwikinger_shareholders = FactoryGirl.build(:profile_shareholders_data, ticker: "TXWIKINGER")
11
+ @avidbeaver_shareholders = FactoryGirl.build(:profile_shareholders_data, ticker: "AVIDBEAVER")
12
+ end
13
+
14
+ it "gets profile shareholders without ticker" do
15
+ stub_get("/profile/shareholders").with(:query => {:client_id => subject.client_id, :access_token => subject.oauth_token}).to_return(:body => FactoryGirl.build(:response, data: [@txwikinger_shareholders]).to_json, :headers => {:content_type => "application/json; charset=utf-8"})
16
+ shareholders = subject.profile_shareholders
17
+ expect(a_get("/profile/shareholders").with(:query => {:client_id => subject.client_id, :access_token => subject.oauth_token})).to have_been_made
18
+ expect(shareholders[:body][:meta][:total_rows]).to eq 1
19
+ expect(shareholders[:body][:data][0][:ticker]).to eq "TXWIKINGER"
20
+ end
21
+
22
+ it "gets profile shareholders with ticker" do
23
+ stub_get("/profile/shareholders").with(:query => {:client_id => subject.client_id, :access_token => subject.oauth_token, :ticker => "AVIDBEAVER"}).to_return(:body => FactoryGirl.build(:response, data: [@avidbeaver_shareholders]).to_json, :headers => {:content_type => "application/json; charset=utf-8"})
24
+ shareholders = subject.profile_shareholders("AVIDBEAVER")
25
+ expect(a_get("/profile/shareholders").with(:query => {:ticker => "AVIDBEAVER", :client_id => subject.client_id, :access_token => subject.oauth_token})).to have_been_made
26
+ expect(shareholders[:body][:meta][:total_rows]).to eq 1
27
+ expect(shareholders[:body][:data][0][:ticker]).to eq "AVIDBEAVER"
28
+ end
29
+
30
+ it "gets profile shareholders page 2" do
31
+ stub_get("/profile/shareholders").with(:query => {:page => 2, :client_id => subject.client_id, :access_token => subject.oauth_token}).to_return(:body => FactoryGirl.build(:response, requested_user: "AVIDBEAVER", requested_page: 2, data: [@txwikinger_shareholders]).to_json, :headers => {:content_type => "application/json; charset=utf-8"})
32
+ shareholders = subject.profile_shareholders(nil, 2)
33
+ expect(a_get("/profile/shareholders").with(:query => {:page => 2, :client_id => subject.client_id, :access_token => subject.oauth_token})).to have_been_made
34
+ expect(shareholders[:body][:meta][:total_rows]).to eq 1
35
+ expect(shareholders[:body][:meta][:requested_user]).to eq "AVIDBEAVER"
36
+ expect(shareholders[:body][:meta][:requested_page]).to eq 2
37
+ expect(shareholders[:body][:data][0][:ticker]).to eq "TXWIKINGER"
38
+ end
39
+
40
+ it "gets profile shareholders limit to 1 result" do
41
+ stub_get("/profile/shareholders").with(:query => {:maxresults => 1, :client_id => subject.client_id, :access_token => subject.oauth_token}).to_return(:body => FactoryGirl.build(:response, requested_user: "AVIDBEAVER", requested_page: 2, data: [@txwikinger_shareholders]).to_json, :headers => {:content_type => "application/json; charset=utf-8"})
42
+ shareholders = subject.profile_shareholders(nil, nil, 1)
43
+ expect(a_get("/profile/shareholders").with(:query => {:maxresults => 1, :client_id => subject.client_id, :access_token => subject.oauth_token})).to have_been_made
44
+ expect(shareholders[:body][:meta][:total_rows]).to eq 1
45
+ expect(shareholders[:body][:meta][:requested_user]).to eq "AVIDBEAVER"
46
+ expect(shareholders[:body][:meta][:requested_page]).to eq 1
47
+ expect(shareholders[:body][:data][0][:ticker]).to eq "TXWIKINGER"
48
+ end
49
+
50
+ end
@@ -0,0 +1,16 @@
1
+ require 'helper'
2
+
3
+ describe EmpireAvenue::API::Search do
4
+
5
+ subject do
6
+ EmpireAvenue::Client.new(:client_id => "CK", :client_secret => "CS", :oauth_token => "OT", :oauth_token_secret => "OS")
7
+ end
8
+
9
+ it "gets recent new users without ticker" do
10
+ search_recent_info = FactoryGirl.build(:search_recent_data)
11
+ stub_get("/search/recent").with(:query => {:client_id => subject.client_id, :access_token => subject.oauth_token}).to_return(:body => FactoryGirl.build(:response, total_rows: 100, data: [search_recent_info]).to_json, :headers => {:content_type => "application/json; charset=utf-8"})
12
+ info = subject.search_recent
13
+ expect(a_get("/search/recent").with(:query => {:client_id => subject.client_id, :access_token => subject.oauth_token})).to have_been_made
14
+ expect(info[:body][:meta][:total_rows]).to eq 100
15
+ end
16
+ end
@@ -0,0 +1,16 @@
1
+ require 'helper'
2
+
3
+ describe EmpireAvenue::API::Search do
4
+
5
+ subject do
6
+ EmpireAvenue::Client.new(:client_id => "CK", :client_secret => "CS", :oauth_token => "OT", :oauth_token_secret => "OS")
7
+ end
8
+
9
+ it "gets profile info without ticker" do
10
+ search_recent_info = FactoryGirl.build(:search_recent_data)
11
+ stub_get("/search/recent").with(:query => {:client_id => subject.client_id, :access_token => subject.oauth_token}).to_return(:body => FactoryGirl.build(:response, total_rows: 100, data: [search_recent_info]).to_json, :headers => {:content_type => "application/json; charset=utf-8"})
12
+ info = subject.search_recent
13
+ expect(a_get("/search/recent").with(:query => {:client_id => subject.client_id, :access_token => subject.oauth_token})).to have_been_made
14
+ expect(info[:body][:meta][:total_rows]).to eq 100
15
+ end
16
+ end
@@ -0,0 +1,31 @@
1
+ require 'helper'
2
+
3
+ describe EmpireAvenue::API::Profile do
4
+
5
+ subject do
6
+ EmpireAvenue::Client.new(:client_id => "CK", :client_secret => "CS", :oauth_token => "OT", :oauth_token_secret => "OS")
7
+ end
8
+
9
+ it "buys shares with last trade price provided" do
10
+ avidbeaver_buy_info = FactoryGirl.build(:shares_buy_data)
11
+ stub_post("/shares/buy").with(:query => {:client_id => subject.client_id, :access_token => subject.oauth_token}, :body => {:ticker => "AVIDBEAVER", :shares => "5", :last_trade => "150.50"}).to_return(:body => FactoryGirl.build(:response, requested_user: "AVIDBEAVER", data: [avidbeaver_buy_info]).to_json, :headers => {:content_type => "application/json; charset=utf-8"})
12
+ info = subject.shares_buy("AVIDBEAVER", 5, "150.50")
13
+ expect(a_post("/shares/buy").with(:query => {:client_id => subject.client_id, :access_token => subject.oauth_token}, :body => {:ticker => "AVIDBEAVER", :shares => "5", :last_trade => "150.50"})).to have_been_made
14
+ expect(info[:body][:meta][:total_rows]).to eq 1
15
+ expect(info[:body][:data][0][:success]).to eq true
16
+ end
17
+
18
+ it "buys shares without last trade price provided" do
19
+ avidbeaver_info = FactoryGirl.build(:profile_info_data, ticker: "AVIDBEAVER")
20
+ avidbeaver_buy_info = FactoryGirl.build(:shares_buy_data)
21
+ stub_get("/profile/info").with(:query => {:client_id => subject.client_id, :access_token => subject.oauth_token, :ticker => "AVIDBEAVER"}).to_return(:body => FactoryGirl.build(:response, data: [avidbeaver_info]).to_json, :headers => {:content_type => "application/json; charset=utf-8"})
22
+ stub_post("/shares/buy").with(:query => {:client_id => subject.client_id, :access_token => subject.oauth_token}, :body => {:ticker => "AVIDBEAVER", :shares => "5", :last_trade => avidbeaver_info.last_trade}).to_return(:body => FactoryGirl.build(:response, requested_user: "AVIDBEAVER", data: [avidbeaver_buy_info]).to_json, :headers => {:content_type => "application/json; charset=utf-8"})
23
+ info = subject.shares_buy("AVIDBEAVER", 5)
24
+ expect(a_get("/profile/info").with(:query => {:ticker => "AVIDBEAVER", :client_id => subject.client_id, :access_token => subject.oauth_token})).to have_been_made
25
+ expect(a_post("/shares/buy").with(:query => {:client_id => subject.client_id, :access_token => subject.oauth_token}, :body => {:ticker => "AVIDBEAVER", :shares => "5", :last_trade => avidbeaver_info.last_trade})).to have_been_made
26
+ expect(info[:body][:meta][:total_rows]).to eq 1
27
+ expect(info[:body][:data][0][:success]).to eq true
28
+ end
29
+
30
+
31
+ end
@@ -0,0 +1,30 @@
1
+ require 'helper'
2
+
3
+ describe EmpireAvenue::API::Profile do
4
+
5
+ subject do
6
+ EmpireAvenue::Client.new(:client_id => "CK", :client_secret => "CS", :oauth_token => "OT", :oauth_token_secret => "OS")
7
+ end
8
+
9
+ it "buys shares with last trade price provided" do
10
+ stub_post("/shares/buy").with(:query => {:client_id => subject.client_id, :access_token => subject.oauth_token}, :body => {:ticker => "AVIDBEAVER", :shares => "5", :last_trade => "150.50"}).to_return(:body => fixture("buy_avidbeaver.json"), :headers => {:content_type => "application/json; charset=utf-8"})
11
+ info = subject.shares_buy("AVIDBEAVER", 5, "150.50")
12
+ expect(a_post("/shares/buy").with(:query => {:client_id => subject.client_id, :access_token => subject.oauth_token}, :body => {:ticker => "AVIDBEAVER", :shares => "5", :last_trade => "150.50"})).to have_been_made
13
+ expect(info[:body][:meta][:total_rows]).to eq 1
14
+ expect(info[:body][:data][0][:success]).to eq true
15
+ end
16
+
17
+ it "buys shares without last trade price provided" do
18
+ avidbeaver_info = FactoryGirl.build(:profile_info_data, ticker: "AVIDBEAVER")
19
+ avidbeaver_buy_info = FactoryGirl.build(:shares_buy_data)
20
+ stub_get("/profile/info").with(:query => {:client_id => subject.client_id, :access_token => subject.oauth_token, :ticker => "AVIDBEAVER"}).to_return(:body => FactoryGirl.build(:response, data: [avidbeaver_info]).to_json, :headers => {:content_type => "application/json; charset=utf-8"})
21
+ stub_post("/shares/buy").with(:query => {:client_id => subject.client_id, :access_token => subject.oauth_token}, :body => {:ticker => "AVIDBEAVER", :shares => "5", :last_trade => avidbeaver_info.last_trade}).to_return(:body => FactoryGirl.build(:response, requested_user: "AVIDBEAVER", data: [avidbeaver_buy_info]).to_json, :headers => {:content_type => "application/json; charset=utf-8"})
22
+ info = subject.shares_buy("AVIDBEAVER", 5)
23
+ expect(a_get("/profile/info").with(:query => {:ticker => "AVIDBEAVER", :client_id => subject.client_id, :access_token => subject.oauth_token})).to have_been_made
24
+ expect(a_post("/shares/buy").with(:query => {:client_id => subject.client_id, :access_token => subject.oauth_token}, :body => {:ticker => "AVIDBEAVER", :shares => "5", :last_trade => avidbeaver_info.last_trade})).to have_been_made
25
+ expect(info[:body][:meta][:total_rows]).to eq 1
26
+ expect(info[:body][:data][0][:success]).to eq true
27
+ end
28
+
29
+
30
+ end
@@ -0,0 +1,31 @@
1
+ require 'helper'
2
+
3
+ describe EmpireAvenue::API::Profile do
4
+
5
+ subject do
6
+ EmpireAvenue::Client.new(:client_id => "CK", :client_secret => "CS", :oauth_token => "OT", :oauth_token_secret => "OS")
7
+ end
8
+
9
+ it "sells shares with last trade price provided" do
10
+ avidbeaver_sell_info = FactoryGirl.build(:shares_sell_data)
11
+ stub_post("/shares/sell").with(:query => {:client_id => subject.client_id, :access_token => subject.oauth_token}, :body => {:ticker => "AVIDBEAVER", :shares => "5", :last_trade => "150.50"}).to_return(:body => FactoryGirl.build(:response, requested_user: "AVIDBEAVER", data: [avidbeaver_sell_info]).to_json, :headers => {:content_type => "application/json; charset=utf-8"})
12
+ info = subject.shares_sell("AVIDBEAVER", 5, "150.50")
13
+ expect(a_post("/shares/sell").with(:query => {:client_id => subject.client_id, :access_token => subject.oauth_token}, :body => {:ticker => "AVIDBEAVER", :shares => "5", :last_trade => "150.50"})).to have_been_made
14
+ expect(info[:body][:meta][:total_rows]).to eq 1
15
+ expect(info[:body][:data][0][:success]).to eq true
16
+ end
17
+
18
+ it "sells shares without last trade price provided" do
19
+ avidbeaver_info = FactoryGirl.build(:profile_info_data, ticker: "AVIDBEAVER")
20
+ avidbeaver_sell_info = FactoryGirl.build(:shares_sell_data)
21
+ stub_get("/profile/info").with(:query => {:client_id => subject.client_id, :access_token => subject.oauth_token, :ticker => "AVIDBEAVER"}).to_return(:body => FactoryGirl.build(:response, data: [avidbeaver_info]).to_json, :headers => {:content_type => "application/json; charset=utf-8"})
22
+ stub_post("/shares/sell").with(:query => {:client_id => subject.client_id, :access_token => subject.oauth_token}, :body => {:ticker => "AVIDBEAVER", :shares => "5", :last_trade => avidbeaver_info.last_trade}).to_return(:body => FactoryGirl.build(:response, requested_user: "AVIDBEAVER", data: [avidbeaver_sell_info]).to_json, :headers => {:content_type => "application/json; charset=utf-8"})
23
+ info = subject.shares_sell("AVIDBEAVER", 5)
24
+ expect(a_get("/profile/info").with(:query => {:ticker => "AVIDBEAVER", :client_id => subject.client_id, :access_token => subject.oauth_token})).to have_been_made
25
+ expect(a_post("/shares/sell").with(:query => {:client_id => subject.client_id, :access_token => subject.oauth_token}, :body => {:ticker => "AVIDBEAVER", :shares => "5", :last_trade => avidbeaver_info.last_trade})).to have_been_made
26
+ expect(info[:body][:meta][:total_rows]).to eq 1
27
+ expect(info[:body][:data][0][:success]).to eq true
28
+ end
29
+
30
+
31
+ end
@@ -0,0 +1,31 @@
1
+ require 'helper'
2
+
3
+ describe EmpireAvenue::API::Profile do
4
+
5
+ subject do
6
+ EmpireAvenue::Client.new(:client_id => "CK", :client_secret => "CS", :oauth_token => "OT", :oauth_token_secret => "OS")
7
+ end
8
+
9
+ it "sells shares with last trade price provided" do
10
+ avidbeaver_sell_info = FactoryGirl.build(:shares_sell_data)
11
+ stub_post("/shares/sell").with(:query => {:client_id => subject.client_id, :access_token => subject.oauth_token}, :body => {:ticker => "AVIDBEAVER", :shares => "5", :last_trade => "150.50"}).to_return(:body => FactoryGirl.build(:response, requested_user: "AVIDBEAVER", data: [avidbeaver_sell_info]).to_json, :headers => {:content_type => "application/json; charset=utf-8"})
12
+ info = subject.shares_sell("AVIDBEAVER", 5, "150.50")
13
+ expect(a_post("/shares/sell").with(:query => {:client_id => subject.client_id, :access_token => subject.oauth_token}, :body => {:ticker => "AVIDBEAVER", :shares => "5", :last_trade => "150.50"})).to have_been_made
14
+ expect(info[:body][:meta][:total_rows]).to eq 1
15
+ expect(info[:body][:data][0][:success]).to eq true
16
+ end
17
+
18
+ it "sells shares without last trade price provided" do
19
+ avidbeaver_info = FactoryGirl.build(:profile_info_data, ticker: "AVIDBEAVER")
20
+ avidbeaver_sell_info = FactoryGirl.build(:shares_sell_data)
21
+ stub_get("/profile/info").with(:query => {:client_id => subject.client_id, :access_token => subject.oauth_token, :ticker => "AVIDBEAVER"}).to_return(:body => FactoryGirl.build(:response, data: [avidbeaver_info]).to_json, :headers => {:content_type => "application/json; charset=utf-8"})
22
+ stub_post("/shares/sell").with(:query => {:client_id => subject.client_id, :access_token => subject.oauth_token}, :body => {:ticker => "AVIDBEAVER", :shares => "5", :last_trade => avidbeaver_info.last_trade}).to_return(:body => FactoryGirl.build(:response, requested_user: "AVIDBEAVER", data: [avidbeaver_sell_info]).to_json, :headers => {:content_type => "application/json; charset=utf-8"})
23
+ info = subject.shares_sell("AVIDBEAVER", 5)
24
+ expect(a_get("/profile/info").with(:query => {:ticker => "AVIDBEAVER", :client_id => subject.client_id, :access_token => subject.oauth_token})).to have_been_made
25
+ expect(a_post("/shares/sell").with(:query => {:client_id => subject.client_id, :access_token => subject.oauth_token}, :body => {:ticker => "AVIDBEAVER", :shares => "5", :last_trade => avidbeaver_info.last_trade})).to have_been_made
26
+ expect(info[:body][:meta][:total_rows]).to eq 1
27
+ expect(info[:body][:data][0][:success]).to eq true
28
+ end
29
+
30
+
31
+ end
@@ -0,0 +1,111 @@
1
+ require 'helper'
2
+
3
+ describe EmpireAvenue::Base do
4
+
5
+ context "identity map enabled" do
6
+ before do
7
+ EmpireAvenue.identity_map = EmpireAvenue::IdentityMap
8
+ object = EmpireAvenue::Base.new(:id => 1)
9
+ @base = EmpireAvenue::Base.store(object)
10
+ end
11
+
12
+ after do
13
+ EmpireAvenue.identity_map = false
14
+ end
15
+
16
+ describe ".identity_map" do
17
+ it "returns an instance of the identity map" do
18
+ expect(EmpireAvenue::Base.identity_map).to be_a EmpireAvenue::IdentityMap
19
+ end
20
+ end
21
+
22
+ describe ".fetch" do
23
+ it "returns existing objects" do
24
+ expect(EmpireAvenue::Base.fetch(:id => 1)).to be
25
+ end
26
+
27
+ it "raises an error on objects that don't exist" do
28
+ expect{EmpireAvenue::Base.fetch(:id => 6)}.to raise_error EmpireAvenue::Error::IdentityMapKeyError
29
+ end
30
+ end
31
+
32
+ describe ".store" do
33
+ it "stores EmpireAvenue::Base objects" do
34
+ object = EmpireAvenue::Base.new(:id => 4)
35
+ expect(EmpireAvenue::Base.store(object)).to be_a EmpireAvenue::Base
36
+ end
37
+ end
38
+
39
+ describe ".fetch_or_new" do
40
+ it "returns existing objects" do
41
+ expect(EmpireAvenue::Base.fetch_or_new(:id => 1)).to be
42
+ end
43
+ it "creates new objects and stores them" do
44
+ expect(EmpireAvenue::Base.fetch_or_new(:id => 2)).to be
45
+ expect(EmpireAvenue::Base.fetch(:id => 2)).to be
46
+ end
47
+ end
48
+
49
+ describe "#[]" do
50
+ it "calls methods using [] with symbol" do
51
+ expect(@base[:object_id]).to be_an Integer
52
+ end
53
+ it "calls methods using [] with string" do
54
+ expect(@base['object_id']).to be_an Integer
55
+ end
56
+ it "returns nil for missing method" do
57
+ expect(@base[:foo]).to be_nil
58
+ expect(@base['foo']).to be_nil
59
+ end
60
+ end
61
+
62
+ describe "#to_hash" do
63
+ it "returns a hash" do
64
+ expect(@base.to_hash).to be_a Hash
65
+ expect(@base.to_hash[:id]).to eq 1
66
+ end
67
+ end
68
+
69
+ describe "identical objects" do
70
+ it "have the same object_id" do
71
+ expect(@base.object_id).to eq EmpireAvenue::Base.fetch(:id => 1).object_id
72
+ end
73
+ end
74
+
75
+ end
76
+
77
+ context "identity map disabled" do
78
+ before(:all) do
79
+ EmpireAvenue.identity_map = false
80
+ end
81
+ after(:all) do
82
+ EmpireAvenue.identity_map = EmpireAvenue::IdentityMap
83
+ end
84
+
85
+ describe ".identity_map" do
86
+ it "returns nil" do
87
+ expect(EmpireAvenue::Base.identity_map).to be_nil
88
+ end
89
+ end
90
+
91
+ describe ".fetch" do
92
+ it "returns nil" do
93
+ expect(EmpireAvenue::Base.fetch(:id => 1)).to be_nil
94
+ end
95
+ end
96
+
97
+ describe ".store" do
98
+ it "returns an instance of the object" do
99
+ expect(EmpireAvenue::Base.store(EmpireAvenue::Base.new(:id => 1))).to be_a EmpireAvenue::Base
100
+ end
101
+ end
102
+
103
+ describe ".fetch_or_new" do
104
+ it "creates new objects" do
105
+ expect(EmpireAvenue::Base.fetch_or_new(:id => 2)).to be
106
+ expect(EmpireAvenue.identity_map).to be_false
107
+ end
108
+ end
109
+ end
110
+
111
+ end
@@ -0,0 +1,23 @@
1
+ require 'helper'
2
+
3
+ describe EmpireAvenue::BasicUser do
4
+
5
+ describe "#==" do
6
+ it "returns true when objects IDs are the same" do
7
+ saved_search = EmpireAvenue::BasicUser.new(:ticker => 1, :name => "foo")
8
+ other = EmpireAvenue::BasicUser.new(:ticker => 1, :name => "bar")
9
+ expect(saved_search == other).to be_true
10
+ end
11
+ it "returns false when objects IDs are different" do
12
+ saved_search = EmpireAvenue::BasicUser.new(:ticker => 1)
13
+ other = EmpireAvenue::BasicUser.new(:ticker => 2)
14
+ expect(saved_search == other).to be_false
15
+ end
16
+ it "returns false when classes are different" do
17
+ saved_search = EmpireAvenue::BasicUser.new(:ticker => 1)
18
+ other = EmpireAvenue::Identity.new(:ticker => 1)
19
+ expect(saved_search == other).to be_false
20
+ end
21
+ end
22
+
23
+ end
@@ -0,0 +1,23 @@
1
+ require 'helper'
2
+
3
+ describe EmpireAvenue::BasicUser do
4
+
5
+ describe "#==" do
6
+ it "returns true when objects IDs are the same" do
7
+ saved_search = EmpireAvenue::BasicUser.new(:id => 1, :name => "foo")
8
+ other = EmpireAvenue::BasicUser.new(:id => 1, :name => "bar")
9
+ expect(saved_search == other).to be_true
10
+ end
11
+ it "returns false when objects IDs are different" do
12
+ saved_search = EmpireAvenue::BasicUser.new(:id => 1)
13
+ other = EmpireAvenue::BasicUser.new(:id => 2)
14
+ expect(saved_search == other).to be_false
15
+ end
16
+ it "returns false when classes are different" do
17
+ saved_search = EmpireAvenue::BasicUser.new(:id => 1)
18
+ other = EmpireAvenue::Identity.new(:id => 1)
19
+ expect(saved_search == other).to be_false
20
+ end
21
+ end
22
+
23
+ end
@@ -0,0 +1,149 @@
1
+ require 'helper'
2
+
3
+ describe EmpireAvenue::Client do
4
+
5
+ subject do
6
+ EmpireAvenue::Client.new(:client_id => "CK", :client_secret => "CS", :oauth_token => "OT", :oauth_token_secret => "OS")
7
+ end
8
+
9
+ context "with module configuration" do
10
+
11
+ before do
12
+ EmpireAvenue.configure do |config|
13
+ EmpireAvenue::Configurable.keys.each do |key|
14
+ config.send("#{key}=", key)
15
+ end
16
+ end
17
+ end
18
+
19
+ after do
20
+ EmpireAvenue.reset!
21
+ end
22
+
23
+ it "inherits the module configuration" do
24
+ client = EmpireAvenue::Client.new
25
+ EmpireAvenue::Configurable.keys.each do |key|
26
+ expect(client.instance_variable_get(:"@#{key}")).to eq key
27
+ end
28
+ end
29
+
30
+ context "with class configuration" do
31
+
32
+ before do
33
+ @configuration = {
34
+ :connection_options => {:timeout => 10},
35
+ :client_id => 'CK',
36
+ :client_secret => 'CS',
37
+ :endpoint => 'http://tumblr.com/',
38
+ :middleware => Proc.new{},
39
+ :oauth_token => 'OT',
40
+ :oauth_token_secret => 'OS',
41
+ :identity_map => ::Hash
42
+ }
43
+ end
44
+
45
+ context "during initialization" do
46
+ it "overrides the module configuration" do
47
+ client = EmpireAvenue::Client.new(@configuration)
48
+ EmpireAvenue::Configurable.keys.each do |key|
49
+ expect(client.instance_variable_get(:"@#{key}")).to eq @configuration[key]
50
+ end
51
+ end
52
+ end
53
+
54
+ context "after initialization" do
55
+ it "overrides the module configuration after initialization" do
56
+ client = EmpireAvenue::Client.new
57
+ client.configure do |config|
58
+ @configuration.each do |key, value|
59
+ config.send("#{key}=", value)
60
+ end
61
+ end
62
+ EmpireAvenue::Configurable.keys.each do |key|
63
+ expect(client.instance_variable_get(:"@#{key}")).to eq @configuration[key]
64
+ end
65
+ end
66
+ end
67
+
68
+ end
69
+ end
70
+
71
+ it "does not cache the ticker across clients" do
72
+ txwikinger_info = FactoryGirl.build(:profile_info_data, ticker: "TXWIKINGER")
73
+ avidbeaver_info = FactoryGirl.build(:profile_info_data, ticker: "AVIDBEAVER")
74
+
75
+ stub_get("/profile/info").with(:query => {:client_id => subject.client_id, :access_token => subject.oauth_token}).to_return(:body => FactoryGirl.build(:response, data: [txwikinger_info]).to_json, :headers => {:content_type => "application/json; charset=utf-8"})
76
+ client1 = EmpireAvenue::Client.new(:client_id => "CK", :client_secret => "CS", :oauth_token => "OT", :oauth_token_secret => "OS")
77
+ expect(client1.profile_info[:body][:data][0][:ticker]).to eq "TXWIKINGER"
78
+ stub_get("/profile/info").with(:query => {:client_id => subject.client_id, :access_token => subject.oauth_token}).to_return(:body => FactoryGirl.build(:response, data: [avidbeaver_info]).to_json, :headers => {:content_type => "application/json; charset=utf-8"})
79
+ client2 = EmpireAvenue::Client.new(:client_id => "CK", :client_secret => "CS", :oauth_token => "OT", :oauth_token_secret => "OS")
80
+ expect(client2.profile_info[:body][:data][0][:ticker]).to eq "AVIDBEAVER"
81
+ end
82
+
83
+ describe "#post" do
84
+ before do
85
+ #@client = EmpireAvenue::Client.new
86
+ stub_post("/shares/buy").with(:query => {:client_id => subject.client_id, :access_token => subject.oauth_token}, :body => {"shares" => "1", "ticker" => "AVIDBEAVER", "last_trade" => "150.445"})
87
+ end
88
+ it "allows custom post requests" do
89
+ subject.post("/shares/buy", {:shares => 1, :ticker => "AVIDBEAVER", :last_trade => 150.445})
90
+ expect(a_post("/shares/buy").with(:query => {:client_id => subject.client_id, :access_token => subject.oauth_token}, :body => 'shares=1&ticker=AVIDBEAVER&last_trade=150.445')).to have_been_made
91
+ end
92
+ end
93
+
94
+ describe "#delete" do
95
+ before do
96
+ #@client = EmpireAvenue::Client.new
97
+ stub_delete("/custom/delete").with(:query => {:client_id => subject.client_id, :access_token => subject.oauth_token}.merge({:deleted => "object"}))
98
+ end
99
+ it "allows custom delete requests" do
100
+ subject.delete("/custom/delete", {:deleted => "object"})
101
+ expect(a_delete("/custom/delete").with(:query => {:client_id => subject.client_id, :access_token => subject.oauth_token}.merge({:deleted => "object"}))).to have_been_made
102
+ end
103
+ end
104
+
105
+ #describe "#put" do
106
+ # before do
107
+ # stub_put("/custom/put").with({:query => {:client_id => subject.client_id, :access_token => subject.oauth_token}, :body => {:updated => "object"}})
108
+ # end
109
+ # it "allows custom put requests" do
110
+ # pending "odd split method not found error"
111
+ # subject.put("/custom/put", {:updated => "object"})
112
+ # expect(a_put("/custom/put").with({:query => {:client_id => subject.client_id, :access_token => subject.oauth_token}, :body => {:updated => "object"}})).to have_been_made
113
+ # end
114
+ #end
115
+
116
+ describe "#credentials?" do
117
+ it "returns true if all credentials are present" do
118
+ client = EmpireAvenue::Client.new(:client_id => 'CK', :client_secret => 'CS', :oauth_token => 'OT', :oauth_token_secret => 'OS')
119
+ expect(client.credentials?).to be_true
120
+ end
121
+ it "returns false if any credentials are missing" do
122
+ client = EmpireAvenue::Client.new(:client_id => 'CK', :client_secret => 'CS', :oauth_token => 'OT')
123
+ expect(client.credentials?).to be_false
124
+ end
125
+ end
126
+
127
+ describe "#connection" do
128
+ it "looks like Faraday connection" do
129
+ expect(subject.send(:connection)).to respond_to(:run_request)
130
+ end
131
+ it "memoizes the connection" do
132
+ c1, c2 = subject.send(:connection), subject.send(:connection)
133
+ expect(c1.object_id).to eq c2.object_id
134
+ end
135
+ end
136
+
137
+ describe "#request" do
138
+ it "catches Faraday errors" do
139
+ subject.stub!(:connection).and_raise(Faraday::Error::ClientError.new("Oops"))
140
+ expect{subject.send(:request, :get, "/path")}.to raise_error EmpireAvenue::Error::ClientError
141
+ end
142
+ it "catches MultiJson::DecodeError errors" do
143
+ subject.stub!(:connection).and_raise(MultiJson::DecodeError.new("unexpected token", [], "<!DOCTYPE html>"))
144
+ expect{subject.send(:request, :get, "/path")}.to raise_error EmpireAvenue::Error::DecodeError
145
+ end
146
+ end
147
+
148
+
149
+ end