amiando 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (104) hide show
  1. data/.gitignore +6 -0
  2. data/Gemfile +17 -0
  3. data/Guardfile +11 -0
  4. data/README.md +26 -0
  5. data/Rakefile +12 -0
  6. data/amiando.gemspec +27 -0
  7. data/lib/amiando.rb +59 -0
  8. data/lib/amiando/api_key.rb +35 -0
  9. data/lib/amiando/boolean.rb +33 -0
  10. data/lib/amiando/event.rb +131 -0
  11. data/lib/amiando/request.rb +38 -0
  12. data/lib/amiando/resource.rb +152 -0
  13. data/lib/amiando/result.rb +33 -0
  14. data/lib/amiando/ticket_category.rb +99 -0
  15. data/lib/amiando/ticket_shop.rb +63 -0
  16. data/lib/amiando/user.rb +148 -0
  17. data/lib/amiando/version.rb +3 -0
  18. data/test/amiando/amiando_test.rb +13 -0
  19. data/test/amiando/api_key_test.rb +58 -0
  20. data/test/amiando/boolean_test.rb +21 -0
  21. data/test/amiando/event_test.rb +141 -0
  22. data/test/amiando/resource_test.rb +68 -0
  23. data/test/amiando/result_test.rb +25 -0
  24. data/test/amiando/ticket_category_test.rb +89 -0
  25. data/test/amiando/ticket_shop_test.rb +42 -0
  26. data/test/amiando/user_test.rb +130 -0
  27. data/test/fixtures/ApiKey/018fa0a2281ec9026bc122bc346366f6.yml +60 -0
  28. data/test/fixtures/ApiKey/1f62195e173114773b3ec2613582ece9.yml +60 -0
  29. data/test/fixtures/ApiKey/76860dbafe66279f25f56cd66b3f6ba1.yml +62 -0
  30. data/test/fixtures/ApiKey/97d82801a10696aaf385bf5873034b60.yml +60 -0
  31. data/test/fixtures/ApiKey/a703d1fcec1576fa8cb4ca1c5f53f545.yml +62 -0
  32. data/test/fixtures/ApiKey/a948a98b153ab66978e37160f352bfbc.yml +60 -0
  33. data/test/fixtures/ApiKey/b7ecc9e79dd856dec661554cd00490ed.yml +62 -0
  34. data/test/fixtures/ApiKey/ded8694c21d2e1c0cec845f72ba452b5.yml +60 -0
  35. data/test/fixtures/ApiKey/fa883db60c0390aa8948098d32497015.yml +60 -0
  36. data/test/fixtures/Event/29a95cd545b2fba8121dce97a3c859e3.yml +60 -0
  37. data/test/fixtures/Event/361940ebc5f074b1d6d96c2dbeb573ab.yml +60 -0
  38. data/test/fixtures/Event/3a90bf0a17eb1d647f1496d04640797a.yml +60 -0
  39. data/test/fixtures/Event/3b8ae154ad13c7152366b79c0d34b053.yml +60 -0
  40. data/test/fixtures/Event/49877df5f8c251e3364a3560ab1ab46c.yml +60 -0
  41. data/test/fixtures/Event/50cb3d76b40032b0fa5731ce7d2ae2f6.yml +62 -0
  42. data/test/fixtures/Event/5d6016557d86e7b7ff83d07d1ef4bf44.yml +60 -0
  43. data/test/fixtures/Event/67e55d2f91fa702eb8dc612cbfa661f9.yml +60 -0
  44. data/test/fixtures/Event/813535fc6ee153c1a6d8e826f1fe04dc.yml +60 -0
  45. data/test/fixtures/Event/8eabb0f839e03cde5818397c7121e6b4.yml +61 -0
  46. data/test/fixtures/Event/96a5ccd0281a67a4bdb276516b77e756.yml +62 -0
  47. data/test/fixtures/Event/9e689bd5495dc898a710cba8e6d1d779.yml +62 -0
  48. data/test/fixtures/Event/a816e5664ff66b8e3d27b74435a0c123.yml +60 -0
  49. data/test/fixtures/Event/c5ad7e1d1c160d7d1132297892a9ec94.yml +60 -0
  50. data/test/fixtures/Event/c88c85889575508f75deee325e16e49d.yml +60 -0
  51. data/test/fixtures/Event/d6a585196f1a2a94d22f22a2140d4c07.yml +60 -0
  52. data/test/fixtures/Event/e00eebd4faf194fe592fa6e7615909b8.yml +60 -0
  53. data/test/fixtures/Event/e753b6f404795c6635856ea43b0c7d35.yml +60 -0
  54. data/test/fixtures/Event/eb341dbccefaad75aad439f10cc7d82d.yml +60 -0
  55. data/test/fixtures/Event/ebab641e40ca57f8e0883d8ea003bd78.yml +60 -0
  56. data/test/fixtures/Event/ef17351e4bbc84eb776fe16dda31e1e4.yml +62 -0
  57. data/test/fixtures/Global/505952258352958b3b59a3812372ed02.yml +60 -0
  58. data/test/fixtures/Global/d41a6ca323b5db99b4e3c06e108c52cc.yml +60 -0
  59. data/test/fixtures/TicketCategory/0412c9d453efd804e171c6ba57fd980a.yml +66 -0
  60. data/test/fixtures/TicketCategory/49877df5f8c251e3364a3560ab1ab46c.yml +60 -0
  61. data/test/fixtures/TicketCategory/4d3f8d9ff3fb728fc37aaa6e40355a00.yml +62 -0
  62. data/test/fixtures/TicketCategory/5d4fdfda01ed8193c6bbc5e089db5a4b.yml +60 -0
  63. data/test/fixtures/TicketCategory/655e7a8089d67b8eeea1176398e4b289.yml +60 -0
  64. data/test/fixtures/TicketCategory/a9b19dc83437d00ffc4603c04f78159f.yml +62 -0
  65. data/test/fixtures/TicketCategory/bee3d6eb4247fec0849d91fb2f768524.yml +60 -0
  66. data/test/fixtures/TicketCategory/c6f8563714904f006b28ac06312b107d.yml +60 -0
  67. data/test/fixtures/TicketCategory/e6076c26b1b75eec091c5b91c9f75ad9.yml +62 -0
  68. data/test/fixtures/TicketCategory/ebfcf07be91b8cb6d05156713592abc9.yml +60 -0
  69. data/test/fixtures/TicketCategory/fe63648cf867613ec93b380e06257349.yml +60 -0
  70. data/test/fixtures/TicketShop/03d35073550910e61201cf425b4ff73e.yml +60 -0
  71. data/test/fixtures/TicketShop/0524d80dc6e3fad88d49adf71ffa2ab9.yml +60 -0
  72. data/test/fixtures/TicketShop/09729fb0d9f1490bec3acfa0e6742190.yml +60 -0
  73. data/test/fixtures/TicketShop/15c36c73e9f1d3255a61a559e3b98562.yml +60 -0
  74. data/test/fixtures/TicketShop/4605dd240202cd7510bfecaf474ac615.yml +62 -0
  75. data/test/fixtures/TicketShop/5c1f5488a196f44f2c18cc186816fd42.yml +60 -0
  76. data/test/fixtures/TicketShop/71f80402a72be32fdd34074d4426b765.yml +62 -0
  77. data/test/fixtures/TicketShop/bc24338e918c6edb07a55f4d580c5af3.yml +60 -0
  78. data/test/fixtures/TicketShop/d095062bf22d235f277c979b025e5cac.yml +60 -0
  79. data/test/fixtures/TicketShop/ece5150c24e5000bf4eff8dcdb6311bb.yml +60 -0
  80. data/test/fixtures/User/0b4a60c6c962b4b3af9ab21832cd76b2.yml +60 -0
  81. data/test/fixtures/User/0f635f4bb82fdc84af205efe76f43ae0.yml +60 -0
  82. data/test/fixtures/User/1c2f2c8a5372e998f38b9d387107cee2.yml +60 -0
  83. data/test/fixtures/User/1e3ab0fb92b5b1992f35256b4be7497b.yml +71 -0
  84. data/test/fixtures/User/3bac6d29256056f2b9a1ea6fad26966d.yml +60 -0
  85. data/test/fixtures/User/3ffaba64188557400a8678dbefd75990.yml +60 -0
  86. data/test/fixtures/User/5a26c7f9edf6d6027b19b57feda4a96f.yml +60 -0
  87. data/test/fixtures/User/5c17b4b5287f5d0ba390e25e99cf2270.yml +49 -0
  88. data/test/fixtures/User/6501a4db53a97afe6f7b9afeeb996888.yml +60 -0
  89. data/test/fixtures/User/6ad0dd77081f413e3df25d6a625d38cd.yml +60 -0
  90. data/test/fixtures/User/7d493f7758d9795ad522bc827cb51b77.yml +60 -0
  91. data/test/fixtures/User/89e6cb6ae39e29a154c99e0e33bb1b91.yml +60 -0
  92. data/test/fixtures/User/90a3bfe214f17305e356ad3f1a9c9ce7.yml +60 -0
  93. data/test/fixtures/User/92ad6537c8be5aab7cbd778d424f4944.yml +60 -0
  94. data/test/fixtures/User/9485b68940997e23e3721a413d4fbdd5.yml +49 -0
  95. data/test/fixtures/User/98348fe6fbecb7916faadd835916d94d.yml +60 -0
  96. data/test/fixtures/User/b63d7aa4703a3140b4d9884e9f302362.yml +60 -0
  97. data/test/fixtures/User/b8c3fd04d7bd0925c3ccd3a3978ab8e6.yml +49 -0
  98. data/test/fixtures/User/baa28f50439b903e423dbdf918fff20b.yml +62 -0
  99. data/test/fixtures/User/e51278f9f771be0c6c3ac0e4ee1b6459.yml +59 -0
  100. data/test/fixtures/User/eb30a195ac7829d506954f472e9e6dea.yml +60 -0
  101. data/test/support/factory.rb +45 -0
  102. data/test/support/hydra_cache.rb +59 -0
  103. data/test/test_helper.rb +36 -0
  104. metadata +330 -0
@@ -0,0 +1,33 @@
1
+ module Amiando
2
+
3
+ ##
4
+ # This object is intended to be used when an api will return value that is
5
+ # not a resource object (a User, an Event, etc), but due to the delayed
6
+ # nature of doing requests in parallel, can't be initialized from the beginning.
7
+ #
8
+ # After the object is populated, you can ask the result with the result
9
+ # method.
10
+ class Result
11
+ attr_accessor :request, :response, :errors
12
+
13
+ def initialize(&block)
14
+ @populator = block
15
+ end
16
+
17
+ def populate(response_body)
18
+ if @populator.arity == 1
19
+ @result = @populator.call(response_body)
20
+ elsif @populator.arity == 2
21
+ @result = @populator.call(response_body, self)
22
+ end
23
+ end
24
+
25
+ def result
26
+ if defined?(@result)
27
+ @result
28
+ else
29
+ raise Error::NotInitialized.new('Called result before the query was run')
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,99 @@
1
+ module Amiando
2
+
3
+ ##
4
+ # http://developers.amiando.com/index.php/REST_API_TicketCategories
5
+ class TicketCategory < Resource
6
+ map :sale_start, :saleStart, :type => :time
7
+ map :sale_end, :saleEnd, :type => :time
8
+ map :reserved_count, :reservedCount
9
+ map :min_sell, :minSell
10
+ map :max_sell, :maxSell
11
+ map :default_value, :defaultValue
12
+ map :min_sell_required, :minSellRequired
13
+ map :ticket_description, :ticketDescription
14
+ map :display_price, :displayPrice
15
+ map :display_quantity, :displayQuantity
16
+ map :price_editable, :priceEditable
17
+
18
+ ##
19
+ # Creates a ticket category for an event
20
+ # The returned object only contains the id or errors.
21
+ #
22
+ # @param event_id
23
+ # @param [Hash] attributes
24
+ def self.create(event_id, attributes)
25
+ object = new
26
+ post object, "api/event/#{event_id}/ticketCategory/create",
27
+ :params => attributes,
28
+ :populate_method => :populate_create
29
+
30
+ object
31
+ end
32
+
33
+ ##
34
+ # Update a ticket_category
35
+ #
36
+ # @param [Hash] attributes
37
+ def self.update(ticket_category_id, attributes)
38
+ object = Result.new do |response_body, result|
39
+ if response_body['success']
40
+ true
41
+ else
42
+ result.errors = response_body['errors']
43
+ false
44
+ end
45
+ end
46
+
47
+ post object, "api/ticketCategory/#{ticket_category_id}", :params => attributes
48
+
49
+ object
50
+ end
51
+
52
+ ##
53
+ # Find a ticket category.
54
+ #
55
+ # @param ticket category id
56
+ #
57
+ # @return [TicketCategory]
58
+ def self.find(ticket_category_id)
59
+ object = new
60
+ get object, "api/ticketCategory/#{ticket_category_id}"
61
+
62
+ object
63
+ end
64
+
65
+ ##
66
+ # @param event id
67
+ # @param [Symbol]
68
+ # :ids if you only want to fetch the ids.
69
+ # :full if you want the whole objects
70
+ #
71
+ # @return [Result] with all the ticket category ids for this event.
72
+ def self.find_all_by_event_id(event_id, type = :ids)
73
+ object = Result.new do |response_body, result|
74
+ if response_body['success']
75
+ if type == :ids
76
+ response_body['ticketCategories']
77
+ else
78
+ response_body['ticketCategories'].map do |category|
79
+ new(category)
80
+ end
81
+ end
82
+ else
83
+ result.errors = response_body['errors']
84
+ false
85
+ end
86
+ end
87
+
88
+ get object, "/api/event/#{event_id}/ticketCategories", :params => {:resultType => type}
89
+
90
+ object
91
+ end
92
+
93
+ private
94
+
95
+ def populate(response_body)
96
+ extract_attributes_from(response_body, 'ticketCategory')
97
+ end
98
+ end
99
+ end
@@ -0,0 +1,63 @@
1
+ module Amiando
2
+
3
+ ##
4
+ # From the documentation:
5
+ #
6
+ # http://developers.amiando.com/index.php/REST_API_TicketShops
7
+ #
8
+ # A ticketshop will automatically be created when you create an event.
9
+ # That's why you only have read and update access, but no create access.
10
+ # If you delete the related event, the ticketshop will also be deleted.
11
+ #
12
+ class TicketShop < Resource
13
+
14
+ map :ticket_base_fee, :ticketBaseFee
15
+ map :product_disagio, :productDisagio
16
+ map :fee_inclusive, :feeInclusive
17
+ map :shipment_fee_inclusive, :shipmentFeeInclusive
18
+ map :sales_tax, :salesTax
19
+ map :available_limit, :availableLimit
20
+ map :show_available_tickets, :showAvailableTickets
21
+ map :cancelation_possible, :cancelationPossible
22
+ map :num_tickets_sold, :numTicketsSold
23
+ map :total_income, :totalIncome
24
+ map :total_fees, :totalFees
25
+ map :collect_user_data, :collectUserData
26
+ map :max_possible_number_of_participants, :maxPossibleNumberOfParticipants
27
+ map :num_tickets_checked, :numTicketsChecked
28
+
29
+
30
+ ##
31
+ # Get the ticket shop of an event.
32
+ #
33
+ # @param event id
34
+ #
35
+ # @return [TicektShop] the event's ticketshop
36
+ def self.find(event_id)
37
+ object = new
38
+ request = get object, "api/event/#{event_id}/ticketShop"
39
+
40
+ object
41
+ end
42
+
43
+ ##
44
+ # Updates the ticket shop.
45
+ #
46
+ # @param [String] username
47
+ #
48
+ # @return [Boolean] deferred object indicating the result of the update.
49
+ def self.update(event_id, attributes)
50
+ object = Boolean.new('success')
51
+ request = post object, "/api/event/#{event_id}/ticketShop", :params => map_params(attributes)
52
+
53
+ object
54
+ end
55
+
56
+
57
+ private
58
+
59
+ def populate(response_body)
60
+ extract_attributes_from(response_body, 'ticketShop')
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,148 @@
1
+ module Amiando
2
+
3
+ ##
4
+ # http://developers.amiando.com/index.php/REST_API_Users
5
+ class User < Resource
6
+ map :first_name, :firstName
7
+ map :last_name, :lastName
8
+
9
+ ##
10
+ # Will return a {Boolean} deferred object containing the result
11
+ #
12
+ # @param [String] username
13
+ def self.exists?(username)
14
+ object = Boolean.new('exists')
15
+ request = get object, "api/user/exists",
16
+ :params => { :username => username }
17
+
18
+ object
19
+ end
20
+
21
+ ##
22
+ # Find a user given the username (email)
23
+ #
24
+ # @param [String] username
25
+ # @return [Result] with the user's id
26
+ def self.find_by_username(username)
27
+ object = Result.new do |response_body|
28
+ response_body['ids'].first
29
+ end
30
+
31
+ request = get object, "api/user/find",
32
+ :params => { :username => username }
33
+
34
+ object
35
+ end
36
+
37
+ ##
38
+ # Creates a user. It will not return the full user and only the id
39
+ # attribute will be available.
40
+ #
41
+ # @param [Hash] attributes
42
+ def self.create(attributes)
43
+ object = new
44
+ request = post object, '/api/user/create',
45
+ :params => map_params(attributes),
46
+ :populate_method => :populate_create
47
+
48
+ object
49
+ end
50
+
51
+ ##
52
+ # Updates a user.
53
+ # Will return a {Boolean} deferred object indicating
54
+ # the result of the update
55
+ #
56
+ # @param [String] username
57
+ def self.update(user_id, attributes)
58
+ object = Boolean.new('success')
59
+ request = post object, "/api/user/#{user_id}", :params => map_params(attributes)
60
+
61
+ object
62
+ end
63
+
64
+ ##
65
+ # Find a user.
66
+ # Will return a user
67
+ #
68
+ # @param user_id
69
+ def self.find(user_id)
70
+ object = new
71
+ request = get object, "api/user/#{user_id}"
72
+
73
+ object
74
+ end
75
+
76
+ ##
77
+ # Deletes a user
78
+ # Returns a {Boolean} with the result of the operation
79
+ #
80
+ # @param user_id
81
+ def self.delete(user_id)
82
+ object = Boolean.new('deleted')
83
+ request = do_request object, :delete, "/api/user/#{user_id}"
84
+
85
+ object
86
+ end
87
+
88
+ ## Request permission to use and api key on behalf of a user
89
+ #
90
+ # @param user_id
91
+ # @param [String] password
92
+ def self.request_permission(user_id, password)
93
+ object = Result.new do |response_body, result|
94
+ if response_body['success']
95
+ true
96
+ else
97
+ result.errors = response_body['errors']
98
+ false
99
+ end
100
+ end
101
+
102
+ request = post object, "api/user/#{user_id}/requestPermission", :params => { :password => password }
103
+
104
+ object
105
+ end
106
+
107
+ ##
108
+ # Tries to log out the user_id. Will raise Error::NotAuthorized if
109
+ # trying to logout a user you don't have permission for.
110
+ #
111
+ # @param user_id
112
+ def self.logout(user_id)
113
+ object = Boolean.new('success')
114
+ request = post object, "/api/user/#{user_id}/logout"
115
+
116
+ object
117
+ end
118
+
119
+ def ==(user)
120
+ id == user.id
121
+ end
122
+
123
+ private
124
+
125
+ def populate(response_body)
126
+ extract_attributes_from(response_body, 'user')
127
+ end
128
+
129
+ # def bank_account(id)
130
+ # request :get, "api/user/#{id}/bankAccount"
131
+ # end
132
+
133
+ # def address_billing(id)
134
+ # request :get, "api/user/#{id}/address/billing"
135
+ # end
136
+
137
+ ##
138
+ # Tries to get permission to work on user_id.
139
+ # def request_permission(user_id, password)
140
+ # post "api/user/#{user_id}/requestPermission", :password => password
141
+ # end
142
+
143
+ # def events(user_id)
144
+ # get "api/user/#{user_id}/events"
145
+ # end
146
+ end
147
+
148
+ end
@@ -0,0 +1,3 @@
1
+ module Amiando
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,13 @@
1
+ require 'test_helper'
2
+
3
+ describe Amiando do
4
+ it 'should return the right url for the test environment' do
5
+ Amiando.base_url.must_equal 'https://test.amiando.com'
6
+ end
7
+
8
+ it 'should return the right url for the production environment' do
9
+ Amiando.production!
10
+ Amiando.base_url.must_equal 'https://amiando.com'
11
+ Amiando.development!
12
+ end
13
+ end
@@ -0,0 +1,58 @@
1
+ require 'test_helper'
2
+
3
+ describe Amiando::ApiKey do
4
+ before do
5
+ Amiando.api_key = nil
6
+ HydraCache.prefix = 'ApiKey'
7
+ end
8
+
9
+ after do
10
+ Amiando.api_key = nil
11
+ HydraCache.prefix = nil
12
+ end
13
+
14
+ describe 'create' do
15
+ it 'creates an api key' do
16
+ key = Amiando::ApiKey.create :name => 'wadus'
17
+ Amiando.run
18
+
19
+ key.id.wont_be_nil
20
+ key.name.must_equal 'wadus'
21
+ key.identifier.wont_be_nil
22
+ key.key.wont_be_nil
23
+ key.enabled.wont_be_nil
24
+ end
25
+
26
+ it 'fails when creating with empty parameters' do
27
+ lambda { key = Amiando::ApiKey.create({}) }.must_raise ArgumentError
28
+ end
29
+
30
+ end
31
+
32
+ describe 'update' do
33
+ it 'can update its enabled attribute' do
34
+ key = Amiando::ApiKey.create :name => 'wadus'
35
+ Amiando.run
36
+
37
+ Amiando.api_key = key.key
38
+
39
+ update = Amiando::ApiKey.update(key.id, :enabled => false)
40
+ Amiando.run
41
+
42
+ update.result.must_equal true
43
+ end
44
+
45
+ it 'update raises NotAuthorized if apikey doesnt have permission to update the apikey' do
46
+ key1 = Amiando::ApiKey.create :name => 'wadus'
47
+ key2 = Amiando::ApiKey.create :name => 'wadus1'
48
+ Amiando.run
49
+
50
+ Amiando.api_key = key1.key
51
+
52
+ lambda {
53
+ result = Amiando::ApiKey.update(key2.id, :enabled => false)
54
+ Amiando.run
55
+ }.must_raise Amiando::Error::NotAuthorized
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,21 @@
1
+ require 'test_helper'
2
+
3
+ describe Amiando::Boolean do
4
+ it "raises error if you try to get the result before it's been calculated" do
5
+ lambda { Amiando::Boolean.new(:wadus).result }.must_raise Amiando::Error::NotInitialized
6
+ end
7
+
8
+ it "raises error if you try to get the result before it's been calculated" do
9
+ lambda {
10
+ boolean = Amiando::Boolean.new(:wadus)
11
+ boolean.populate('not_wadus' => true)
12
+ }.must_raise Amiando::Error::NotInitialized
13
+ end
14
+
15
+ it "uses the passed attribute to know its result on populate" do
16
+ boolean = Amiando::Boolean.new(:wadus)
17
+ boolean.populate('wadus' => false)
18
+
19
+ boolean.result.must_equal false
20
+ end
21
+ end