infomeme_client 0.2.1 → 0.2.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (50) hide show
  1. data/lib/infomeme_client.rb +22 -0
  2. data/lib/infomeme_client/base.rb +77 -0
  3. data/lib/infomeme_client/communication.rb +75 -0
  4. data/lib/infomeme_client/entity_hash.rb +66 -0
  5. data/lib/infomeme_client/entity_hash/comment.rb +2 -0
  6. data/lib/infomeme_client/entity_hash/invoice.rb +2 -0
  7. data/lib/infomeme_client/entity_hash/meme.rb +45 -0
  8. data/lib/infomeme_client/entity_hash/meme_type.rb +2 -0
  9. data/lib/infomeme_client/entity_hash/transaction.rb +2 -0
  10. data/lib/infomeme_client/entity_hash/user.rb +2 -0
  11. data/lib/infomeme_client/errors.rb +22 -0
  12. data/lib/infomeme_client/functions.rb +16 -0
  13. data/lib/infomeme_client/functions/meme.rb +23 -0
  14. data/lib/infomeme_client/functions/user.rb +79 -0
  15. data/lib/infomeme_client/functions/user_meme.rb +127 -0
  16. data/lib/infomeme_client/functions/user_order.rb +31 -0
  17. data/lib/infomeme_client/meme_application.rb +18 -0
  18. data/lib/infomeme_client/permissions.rb +22 -0
  19. data/lib/infomeme_client/response.rb +52 -0
  20. data/test/fixtures/memes/comments.json +1 -0
  21. data/test/fixtures/memes/meme.json +1 -0
  22. data/test/fixtures/memes/memes.json +1 -0
  23. data/test/fixtures/memes/types.json +1 -0
  24. data/test/fixtures/oauth/access_token.json +1 -0
  25. data/test/fixtures/oauth/authorize.json +1 -0
  26. data/test/fixtures/oauth/permissions.json +1 -0
  27. data/test/fixtures/oauth/request_token.json +1 -0
  28. data/test/fixtures/oauth/verify_access.json +1 -0
  29. data/test/fixtures/response/error.json +1 -0
  30. data/test/fixtures/response/ok.json +1 -0
  31. data/test/fixtures/response/test.json +1 -0
  32. data/test/fixtures/response/test_extract.json +1 -0
  33. data/test/fixtures/users/invoice.json +1 -0
  34. data/test/fixtures/users/invoices.json +1 -0
  35. data/test/fixtures/users/memes.json +1 -0
  36. data/test/fixtures/users/order_paypal.json +1 -0
  37. data/test/fixtures/users/permissions.json +1 -0
  38. data/test/fixtures/users/transactions.json +1 -0
  39. data/test/fixtures/users/user.json +1 -0
  40. data/test/fixtures/users/user_public.json +1 -0
  41. data/test/helper.rb +69 -0
  42. data/test/test_authorization.rb +56 -0
  43. data/test/test_communication.rb +46 -0
  44. data/test/test_entity_hash.rb +15 -0
  45. data/test/test_meme_functions.rb +46 -0
  46. data/test/test_permissions.rb +25 -0
  47. data/test/test_user_functions.rb +15 -0
  48. data/test/test_user_meme_functions.rb +47 -0
  49. data/test/test_user_order_functions.rb +45 -0
  50. metadata +51 -2
@@ -0,0 +1,22 @@
1
+ require "oauth"
2
+ require "json"
3
+ require "net/http/post/multipart"
4
+ require "mime/types"
5
+
6
+ class InfomemeClient
7
+ require "infomeme_client/errors"
8
+
9
+ autoload :EntityHash, "infomeme_client/entity_hash"
10
+ autoload :Response, "infomeme_client/response"
11
+ autoload :Base, "infomeme_client/base"
12
+ autoload :Communication, "infomeme_client/communication"
13
+ autoload :Permissions, "infomeme_client/permissions"
14
+ autoload :Functions, "infomeme_client/functions"
15
+
16
+ autoload :MemeApplication, "infomeme_client/meme_application"
17
+
18
+ include Base
19
+ include Communication
20
+ include Permissions
21
+ include Functions
22
+ end
@@ -0,0 +1,77 @@
1
+ module InfomemeClient::Base
2
+ def initialize(options = {})
3
+ self.consumer = OAuth::Consumer.new(options[:api_key] || ::INFOMEME_API_KEY, options[:api_secret] || ::INFOMEME_API_SECRET, :site => options[:site] || ::INFOMEME_API_SITE)
4
+ self.default_language = options[:language] || "en"
5
+ case
6
+ when options.key?(:token) && options.key?(:secret) then authorize_with_token(options[:token], options[:secret])
7
+ when options.key?(:login) && options.key?(:password) then authorize_with_credentials(options[:login], options[:password])
8
+ else deauthorize
9
+ end
10
+ end
11
+
12
+ def request_authorization
13
+ response = handle_response :get, consumer.request_token_url
14
+ response.request_token
15
+ end
16
+
17
+ def authorize_url(token, permissions = [])
18
+ "https://infomeme.com/authorize?token=#{token}&permissions=#{permissions.join(',')}"
19
+ end
20
+
21
+ def authorize_with_verifier(token, secret, verifier)
22
+ req_token = OAuth::RequestToken.new(consumer, token, secret)
23
+ response = token_request(req_token, :get, finalize_get_url(consumer.access_token_url, {:oauth_verifier => verifier})) #do request with request_token
24
+ return false if response.error?
25
+ authorize_with_token(response.access_token.token, response.access_token.secret)
26
+ end
27
+
28
+ def authorize_with_token(token, secret)
29
+ access_token = OAuth::AccessToken.new(consumer, token, secret)
30
+ response = token_request(access_token, :get, "/oauth/verify_access")
31
+ return false if response.error?
32
+ self.verified_user = response.user_id
33
+ self.token = access_token
34
+ refresh_user_data
35
+ self
36
+ end
37
+
38
+ def authorize_with_credentials(login, password, permissions = nil)
39
+ req_token = request_authorization
40
+ response = put(consumer.authorize_url, {:oauth_token => req_token.token, :login => login, :password => password, :permissions => permissions || all_permissions.collect {|perm| perm.id}})
41
+ return false if response.error?
42
+ authorize_with_verifier(req_token.token, req_token.secret, response.verifier)
43
+ end
44
+
45
+ def deauthorize
46
+ self.verified_user = nil
47
+ self.token = OAuth::AccessToken.new(consumer)
48
+ refresh_user_data
49
+ self
50
+ end
51
+
52
+ def authorized?
53
+ ! verified_user.nil?
54
+ end
55
+
56
+ def credentials(just = nil)
57
+ creds = {
58
+ :login => verified_user,
59
+ :token => token.token,
60
+ :secret => token.secret,
61
+ }
62
+ creds.key?(just) ? creds[just] : creds
63
+ end
64
+
65
+ def inspect
66
+ @all_permissions ||= all_permissions
67
+ "#<#{self.class.name}: #{authorized? ? credentials.merge({:permissions => (@all_permissions || []).select {|perm| has_permission?(perm.id)}.collect {|perm| perm.name}}).inspect : 'not authorized'}>"
68
+ end
69
+
70
+ private
71
+ attr_accessor :consumer, :token, :verified_user
72
+
73
+ def refresh_user_data
74
+ refresh_settings
75
+ refresh_permissions
76
+ end
77
+ end
@@ -0,0 +1,75 @@
1
+ module InfomemeClient::Communication
2
+ def get(path, headers = {})
3
+ request(:get, path, headers)
4
+ end
5
+
6
+ def post(path, body = "", headers = {})
7
+ request(:post, path, body, headers)
8
+ end
9
+
10
+ def put(path, body = "", headers = {})
11
+ request(:put, path, body, headers)
12
+ end
13
+
14
+ def request(method, path, *args)
15
+ token_request(token, method, path, *args)
16
+ end
17
+
18
+ def handle_response(method, path, *args, &block)
19
+ response = request(method, path, *args)
20
+ response.raise_error
21
+ if block_given?
22
+ if block.arity == 1
23
+ yield response
24
+ else
25
+ yield unless response.error?
26
+ end
27
+ else
28
+ response
29
+ end
30
+ end
31
+
32
+ def extract_from_response(extract, method, path, *args)
33
+ handle_response(method, path, *args) do |resp|
34
+ resp.extract(extract) unless resp.error?
35
+ end
36
+ end
37
+
38
+ def extract_or_response(extract, method, path, options, headers = {})
39
+ no_extract = options[:no_extract] || false
40
+ options.delete(:no_extract)
41
+ if [:put, :post].include?(method.to_sym)
42
+ args = [method, path, options, headers]
43
+ else
44
+ args = [method, finalize_get_url(path, options), headers]
45
+ end
46
+ if no_extract
47
+ handle_response *args
48
+ else
49
+ extract_from_response *args.unshift(extract)
50
+ end
51
+ end
52
+
53
+ private
54
+ def token_request(token, method, path, *args)
55
+ body = [:post, :put].include?(method.to_sym) ? (args.shift || {}) : {}
56
+ headers = args.shift || {}
57
+ headers.store("Accept", "application/json") #override to use json
58
+ if [:post, :put].include?(method.to_sym) && (! headers.key?("Content-Type") || headers["Content-Type"] == "application/json")
59
+ body = body.to_json unless body.is_a?(String)
60
+ headers.store("Content-Type", "application/json")
61
+ end
62
+ args.unshift headers
63
+ args.unshift body if [:post, :put].include?(method.to_sym)
64
+ begin
65
+ resp = token.request(method, path, *args).body
66
+ InfomemeClient::Response.new JSON.parse(resp)
67
+ rescue => e
68
+ InfomemeClient::Response.new e
69
+ end
70
+ end
71
+
72
+ def finalize_get_url(url, params)
73
+ url + (url.include?("?") ? "&" : "?") + params.collect {|key, val| "#{key.to_s}=#{val.to_s}"}.join("&")
74
+ end
75
+ end
@@ -0,0 +1,66 @@
1
+ class InfomemeClient::EntityHash
2
+ autoload :Comment, "infomeme_client/entity_hash/comment"
3
+ autoload :Invoice, "infomeme_client/entity_hash/invoice"
4
+ autoload :Meme, "infomeme_client/entity_hash/meme"
5
+ autoload :MemeType, "infomeme_client/entity_hash/meme_type"
6
+ autoload :Transaction, "infomeme_client/entity_hash/transaction"
7
+ autoload :User, "infomeme_client/entity_hash/user"
8
+
9
+ def initialize(hsh, no_freeze = false)
10
+ @hsh = hsh.inject({}) {|memo, keyvalue| #ripped from ActiveSupport, http://api.rubyonrails.org/classes/ActiveSupport/CoreExtensions/Hash/Keys.html#M000904
11
+ key, value = keyvalue
12
+ memo[(key.to_sym rescue key) || key] = map_values(value)
13
+ memo
14
+ }
15
+ freeze unless no_freeze
16
+ end
17
+
18
+ def method_missing(sym)
19
+ @hsh.key?(sym) ? @hsh[sym] : super
20
+ end
21
+
22
+ def respond_to?(sym, include_private = false)
23
+ @hsh.key?(sym) ? true : super
24
+ end
25
+
26
+ def to_hash
27
+ @hsh.inject({}) do |memo, (key, value)|
28
+ memo[key] = extract_values(value)
29
+ memo
30
+ end
31
+ end
32
+
33
+ def freeze
34
+ @hsh.each do |key, val|
35
+ deep_freeze(val)
36
+ end
37
+ @hsh.freeze
38
+ super
39
+ end
40
+
41
+ def id
42
+ @hsh.key?(:id) ? @hsh[:id] : super
43
+ end
44
+
45
+ private
46
+ def map_values(val)
47
+ case
48
+ when val.is_a?(Hash) then InfomemeClient::EntityHash.new(val)
49
+ when val.is_a?(Array) then val.collect {|obj| map_values(obj)}
50
+ else val
51
+ end
52
+ end
53
+
54
+ def extract_values(val)
55
+ case
56
+ when val.is_a?(InfomemeClient::EntityHash) then val.to_hash
57
+ when val.is_a?(Array) then val.collect {|obj| extract_values(obj)}
58
+ else val
59
+ end
60
+ end
61
+
62
+ def deep_freeze(val)
63
+ val.each {|obj| deep_freeze(obj)} if val.is_a?(Array)
64
+ val.freeze
65
+ end
66
+ end
@@ -0,0 +1,2 @@
1
+ class InfomemeClient::EntityHash::Comment < InfomemeClient::EntityHash
2
+ end
@@ -0,0 +1,2 @@
1
+ class InfomemeClient::EntityHash::Invoice < InfomemeClient::EntityHash
2
+ end
@@ -0,0 +1,45 @@
1
+ class InfomemeClient::EntityHash::Meme < InfomemeClient::EntityHash
2
+ def initialize(meme, client)
3
+ super(meme, true)
4
+ @client = client
5
+ freeze
6
+ end
7
+
8
+ #todo: have_access?, published_by_me?, confirmed?, inactive?, incomplete?, has_upload?, etc.
9
+
10
+ def update(options = {})
11
+ @client.update_meme(id, options)
12
+ end
13
+
14
+ def delete
15
+ @client.delete_meme(id)
16
+ end
17
+
18
+ def confirm
19
+ @client.confirm_meme(id)
20
+ end
21
+
22
+ def activate
23
+ @client.activate_meme(id)
24
+ end
25
+
26
+ def deactivate
27
+ @client.deactivate_meme(id)
28
+ end
29
+
30
+ def upload_url(raw = false)
31
+ @client.upload_for_meme(id, raw)
32
+ end
33
+
34
+ def upload(file)
35
+ @client.upload(id, file)
36
+ end
37
+
38
+ def rate(rating)
39
+ @client.rate(id, rating)
40
+ end
41
+
42
+ def comment(comment)
43
+ @client.comment(id, comment)
44
+ end
45
+ end
@@ -0,0 +1,2 @@
1
+ class InfomemeClient::EntityHash::MemeType < InfomemeClient::EntityHash
2
+ end
@@ -0,0 +1,2 @@
1
+ class InfomemeClient::EntityHash::Transaction < InfomemeClient::EntityHash
2
+ end
@@ -0,0 +1,2 @@
1
+ class InfomemeClient::EntityHash::User < InfomemeClient::EntityHash
2
+ end
@@ -0,0 +1,22 @@
1
+ class InfomemeClient::Error < StandardError
2
+ class RequestFailed < self ; end
3
+ class InternalError < self ; end
4
+ class NoAccess < self ; end
5
+ class NotFound < self ; end
6
+
7
+ attr_reader :errors
8
+
9
+ def initialize(args)
10
+ @errors = args.shift
11
+ msg = args.shift
12
+ super(msg)
13
+ end
14
+
15
+ def message
16
+ "#{super} Errors: #{errors.inspect}"
17
+ end
18
+
19
+ def inspect
20
+ "<#{self.class.inspect} #{self.message}>"
21
+ end
22
+ end
@@ -0,0 +1,16 @@
1
+ module InfomemeClient::Functions
2
+ autoload :Meme, "infomeme_client/functions/meme"
3
+ autoload :User, "infomeme_client/functions/user"
4
+ autoload :UserMeme, "infomeme_client/functions/user_meme"
5
+ autoload :UserOrder, "infomeme_client/functions/user_order"
6
+
7
+ include Meme
8
+ include User
9
+ include UserMeme
10
+ include UserOrder
11
+
12
+ private
13
+ def get_memes(path, options = {}, headers = {})
14
+ extract_or_response :memes, :get, path, options, headers
15
+ end
16
+ end
@@ -0,0 +1,23 @@
1
+ module InfomemeClient::Functions::Meme
2
+ def browse(options = {})
3
+ get_memes "/memes", options
4
+ end
5
+
6
+ def search(search_token, options = {})
7
+ get_memes "/memes/search", options.merge({:search_token => search_token})
8
+ end
9
+
10
+ def meme_types
11
+ extract_from_response :meme_types, :get, "/memes/types"
12
+ end
13
+
14
+ def meme(meme_id)
15
+ handle_response :get, "/memes/#{meme_id}" do |resp|
16
+ resp.extract(:meme, self) unless resp.error?
17
+ end
18
+ end
19
+
20
+ def meme_comments(meme_id, options = {})
21
+ extract_or_response :comments, :get, "/memes/#{meme_id}/comments", options
22
+ end
23
+ end
@@ -0,0 +1,79 @@
1
+ module InfomemeClient::Functions::User
2
+ attr_accessor :default_language
3
+
4
+ def register(email, username, password, firstname, lastname, optional_fields = {})
5
+ optional_fields.store(:language_code, optional_fields.key?(:language) ? optional_fields.delete(:language) : default_language) #language is mandatory
6
+ handle_response :post, "/users/register", optional_fields.merge({:email => email, :username => username, :password => password, :firstname => firstname, :lastname => lastname}) do
7
+ authorize_with_credentials username, password and return true
8
+ end
9
+ end
10
+
11
+ def user(user_id = nil)
12
+ return false if ! authorized? && user_id.nil?
13
+ extract_from_response :user, :get, "/users/#{user_id || verified_user}"
14
+ end
15
+
16
+ def update_settings(attribs = {})
17
+ return false unless authorized?
18
+ handle_response :put, "/users/#{verified_user}", attribs do
19
+ refresh_settings and return true
20
+ end
21
+ end
22
+
23
+ def confirm(confirm_code)
24
+ return false unless authorized?
25
+ handle_response :put, "/users/#{verified_user}/confirm", {:confirm_code => confirm_code} do
26
+ refresh_settings and return true
27
+ end
28
+ end
29
+
30
+ def confirmed?
31
+ authorized? && settings && settings.confirmed
32
+ end
33
+
34
+ def reset_password(user_id = nil)
35
+ return false if ! authorized? && user_id.nil?
36
+ handle_response :get, "/users/#{user_id || verified_user}/reset_password"
37
+ end
38
+
39
+ def new_password(new_password, user_id = nil, confirm_code = "")
40
+ return false if ! authorized? && user_id.nil?
41
+ handle_response :put, "/users/#{user_id || verified_user}/new_password", {:new_password => new_password}.merge(user_id.nil? ? {} : {:confirm_code => confirm_code})
42
+ end
43
+
44
+ def accept_tos
45
+ return false unless authorized?
46
+ handle_response :put, "/users/#{verified_user}/accept_tos" do
47
+ refresh_settings and return true
48
+ end
49
+ end
50
+
51
+ def countries
52
+ handle_response :get, "/users/countries" do |resp|
53
+ resp.countries unless resp.error?
54
+ end
55
+ end
56
+
57
+ def currencies
58
+ handle_response :get, "/users/currencies" do |resp|
59
+ resp.currencies unless resp.error?
60
+ end
61
+ end
62
+
63
+ def languages
64
+ handle_response :get, "/users/languages" do |resp|
65
+ resp.languages unless resp.error?
66
+ end
67
+ end
68
+
69
+ private
70
+ attr_accessor :settings
71
+
72
+ def refresh_settings
73
+ self.settings = nil
74
+ return false unless authorized?
75
+ response = get("/users/#{verified_user}")
76
+ return false if response.error?
77
+ self.settings = response.user
78
+ end
79
+ end
@@ -0,0 +1,127 @@
1
+ module InfomemeClient::Functions::UserMeme
2
+ def library(options = {})
3
+ get_own_memes options
4
+ end
5
+
6
+ def published(options = {})
7
+ get_own_memes options, "published"
8
+ end
9
+
10
+ def inactive(options = {})
11
+ get_own_memes options, "inactive"
12
+ end
13
+
14
+ def incomplete(options = {})
15
+ get_own_memes options, "incomplete"
16
+ end
17
+
18
+ def can_publish?
19
+ authorized? && settings && settings.can_publish
20
+ end
21
+
22
+ def publish(meme_type, options = {})
23
+ return false unless can_publish?
24
+ skip_upload = options.key?(:skip_upload) && options[:skip_upload]
25
+ options[:image] = upload_image(options[:image]) if ! skip_upload && options.key?(:image) && ! options[:image].nil? && options[:image] != ""
26
+ meme_type = meme_type.id if meme_type.is_a?(InfomemeClient::EntityHash::MemeType)
27
+ meme_id = handle_response :post, "/users/#{verified_user}/memes", options.reject {|k,v| k == :file}.merge({:type => meme_type}) do |resp|
28
+ resp.meme_id unless resp.error?
29
+ end
30
+ upload(meme_id, options[:file]) if options.key?(:file)
31
+ meme_id
32
+ end
33
+
34
+ def published_meme(meme_id)
35
+ return false unless authorized?
36
+ extract_from_response :meme, :get, "/users/#{verified_user}/memes/#{meme_id}"
37
+ end
38
+
39
+ def update_meme(meme_id, options = {})
40
+ options[:image] = upload_image(options[:image]) if options.key?(:image)
41
+ meme_function meme_id, :put, nil, options
42
+ end
43
+
44
+ def delete_meme(meme_id)
45
+ meme_function meme_id, :delete
46
+ end
47
+
48
+ def confirm_meme(meme_id)
49
+ meme_function meme_id, :put, "confirm"
50
+ end
51
+
52
+ def activate_meme(meme_id)
53
+ meme_function meme_id, :put, "activate"
54
+ end
55
+
56
+ def deactivate_meme(meme_id)
57
+ meme_function meme_id, :put, "deactivate"
58
+ end
59
+
60
+ def upload_for_meme(meme_id, raw = false)
61
+ return false unless authorized?
62
+ handle_response :get, "/users/#{verified_user}/memes/#{meme_id}/upload_url" do |resp|
63
+ (raw ? resp.upload_url : finalize_get_url(resp.upload_url, generate_signature.merge(:meme_id => meme_id))) unless resp.error?
64
+ end
65
+ end
66
+
67
+ def image_upload_for_memes
68
+ return false unless authorized?
69
+ handle_response :get, "/users/#{verified_user}/memes/image_upload_parameters" do |resp|
70
+ resp.image_upload_parameters unless resp.error?
71
+ end
72
+ end
73
+
74
+ def upload(meme_id, file)
75
+ file = File.new(file) if file.is_a?(String)
76
+ upload_url = upload_for_meme(meme_id, true)
77
+ http_multipart(upload_url, generate_signature.merge({:meme_id => meme_id, :file => uploadio(file)})) if upload_url
78
+ end
79
+
80
+ def upload_image(file)
81
+ file = File.new(file) if file.is_a?(String)
82
+ upload_params = image_upload_for_memes
83
+ File.basename(file.path) if upload_params && http_multipart("https://#{upload_params.bucket}.s3.amazonaws.com/", upload_params.to_hash.reject {|k,v| k == :bucket}.merge(:success_action_status => 200).to_a.push([:file, uploadio(file)])) #ensure file comes last (amazon requires the order)
84
+ end
85
+
86
+ def rate(meme_id, rating)
87
+ return false unless authorized?
88
+ handle_response :post, "/users/#{verified_user}/memes/#{meme_id}/rates", {:rating => rating}
89
+ end
90
+
91
+ def comment(meme_id, comment)
92
+ return false unless authorized?
93
+ handle_response :post, "/users/#{verified_user}/memes/#{meme_id}/comments", {:comment => comment}
94
+ end
95
+
96
+ private
97
+ def generate_signature
98
+ nonce = OAuth::Helper.generate_nonce
99
+ timestamp = Time.now.to_i
100
+ req_proxy = OAuth::RequestProxy.proxy("method" => "GET", "uri" => "https://api.infomeme.com", "parameters" => {"oauth_consumer_key" => consumer.key, "oauth_token" => token.token, "oauth_nonce" => nonce, "oauth_timestamp" => timestamp, "oauth_signature_method" => "HMAC-SHA1"})
101
+ signature = req_proxy.sign({:consumer_secret => consumer.secret, :token_secret => token.secret})
102
+ {:oauth_token => token.token, :oauth_nonce => nonce, :oauth_timestamp => timestamp, :oauth_signature => signature}
103
+ end
104
+
105
+ def get_own_memes(options = {}, cmd = nil, headers = {})
106
+ return false unless authorized?
107
+ get_memes("/users/#{verified_user}/memes" + (cmd.nil? ? "" : "/#{cmd}"), options, headers)
108
+ end
109
+
110
+ def meme_function(meme_id, method, cmd = nil, params = {}, headers = {})
111
+ return false unless authorized?
112
+ handle_response method, "/users/#{verified_user}/memes/#{meme_id}" + (cmd.nil? ? "" : "/#{cmd}"), params, headers
113
+ end
114
+
115
+ def http_multipart(url, params)
116
+ url = URI.parse(url)
117
+ req = Net::HTTP::Post::Multipart.new url.path, params
118
+ http = Net::HTTP.new(url.host, url.port)
119
+ http.use_ssl = url.scheme == "https"
120
+ resp = http.start {|http| http.request(req)}
121
+ resp.is_a?(Net::HTTPSuccess)
122
+ end
123
+
124
+ def uploadio(file)
125
+ UploadIO.new(file, MIME::Types.type_for(file.path), file.path)
126
+ end
127
+ end
@@ -0,0 +1,31 @@
1
+ module InfomemeClient::Functions::UserOrder
2
+ def order(meme_id, source, options = {})
3
+ return false unless authorized?
4
+ handle_response :post, "/users/#{verified_user}/memes/#{meme_id}/order", options.merge(:source => source)
5
+ end
6
+
7
+ def order_with_balance(meme_id, options = {})
8
+ order(meme_id, 1, options)
9
+ refresh_settings
10
+ true
11
+ end
12
+
13
+ def order_with_paypal(meme_id, options = {})
14
+ order(meme_id, 3, options).paypal_url
15
+ end
16
+
17
+ def transactions(options = {})
18
+ return false unless authorized?
19
+ extract_or_response :transactions, :get, "/users/#{verified_user}/transactions", options
20
+ end
21
+
22
+ def invoices(options = {})
23
+ return false unless authorized?
24
+ extract_or_response :invoices, :get, "/users/#{verified_user}/invoices", options
25
+ end
26
+
27
+ def invoice(invoice_id)
28
+ return false unless authorized?
29
+ extract_from_response :invoice, :get, "/users/#{verified_user}/invoices/#{invoice_id}"
30
+ end
31
+ end
@@ -0,0 +1,18 @@
1
+ class InfomemeClient::MemeApplication
2
+ def initialize(options = {})
3
+ self.consumer = OAuth::Consumer.new(options[:api_key] || ::INFOMEME_API_KEY, options[:api_secret] || ::INFOMEME_API_SECRET, :site => options[:site] || ::INFOMEME_API_SITE)
4
+ self.token = OAuth::AccessToken.new(consumer)
5
+ end
6
+
7
+ def verify_access(access_type, meme_id, oauth_token, oauth_nonce, oauth_timestamp, oauth_signature)
8
+ handle_response :post, "/meme_app/verify_access", {:access_type => access_type, :meme_id => meme_id, :verify_token => oauth_token, :verify_nonce => oauth_nonce, :verify_timestamp => oauth_timestamp, :verify_signature => oauth_signature}
9
+ end
10
+
11
+ def update_meme(meme_id, description, has_upload = true)
12
+ handle_response :put, "/meme_app/memes/#{meme_id}", {:description => description, :has_upload => has_upload}
13
+ end
14
+
15
+ private
16
+ attr_accessor :consumer, :token
17
+ include InfomemeClient::Communication
18
+ end
@@ -0,0 +1,22 @@
1
+ module InfomemeClient::Permissions
2
+ def all_permissions
3
+ handle_response :get, "/oauth/permissions" do |resp|
4
+ resp.permissions unless resp.error?
5
+ end
6
+ end
7
+
8
+ def has_permission?(perm)
9
+ authorized? && permissions && permissions.include?(perm)
10
+ end
11
+
12
+ private
13
+ attr_accessor :permissions
14
+
15
+ def refresh_permissions
16
+ self.permissions = nil
17
+ return false unless authorized?
18
+ response = get "/users/#{verified_user}/permissions"
19
+ return false if response.error?
20
+ self.permissions = response.permissions
21
+ end
22
+ end
@@ -0,0 +1,52 @@
1
+ class InfomemeClient::Response < InfomemeClient::EntityHash
2
+ STATUS_FAILED = -1
3
+ STATUS_OK = 0
4
+ STATUS_ERROR = 1
5
+
6
+ ERRORS = {
7
+ "request_failed" => InfomemeClient::Error::RequestFailed,
8
+ "error" => InfomemeClient::Error,
9
+ "internal_error" => InfomemeClient::Error::InternalError,
10
+ "no_access" => InfomemeClient::Error::NoAccess,
11
+ "not_found" => InfomemeClient::Error::NotFound,
12
+ }.freeze
13
+
14
+ def initialize(response)
15
+ super(response.is_a?(Exception) ? {:status => STATUS_FAILED, :message => "Request failed.", :error_code => "request_failed", :errors => {:base => {response.class.name.to_sym => response.message}}} : response, true)
16
+ ERRORS.each do |key, val|
17
+ instance_eval "def #{val.name.split('::').last.sub(/\b\w/) { $&.downcase }}Error?; error_code == #{key}; end"
18
+ end
19
+ freeze
20
+ end
21
+
22
+ def failed?
23
+ status == STATUS_FAILED
24
+ end
25
+
26
+ def error?
27
+ status != STATUS_OK
28
+ end
29
+
30
+ def raise_error(options = {})
31
+ return false unless error?
32
+ return false if options.key?(:only) && ! options[:only].include?(error_code)
33
+ return false if options.key?(:except) && options[:except].include?(error_code)
34
+ err = ERRORS.key?(error_code) ? ERRORS[error_code] : InfomemeClient::Error
35
+ raise err, [(respond_to?(:errors) ? errors.to_hash : nil), message]
36
+ end
37
+
38
+ def extract(key, *extra_args)
39
+ return nil unless @hsh.key?(key)
40
+ klass = case key
41
+ when :comments then InfomemeClient::EntityHash::Comment
42
+ when :meme, :memes then InfomemeClient::EntityHash::Meme
43
+ when :meme_types then InfomemeClient::EntityHash::MemeType
44
+ when :user then InfomemeClient::EntityHash::User
45
+ when :transactions then InfomemeClient::EntityHash::Transaction
46
+ when :invoice, :invoices then InfomemeClient::EntityHash::Invoice
47
+ else InfomemeClient::EntityHash
48
+ end
49
+ stored = to_hash[key]
50
+ stored.is_a?(Array) ? stored.collect {|val| klass.new *[val, extra_args]} : klass.new(*[stored, extra_args])
51
+ end
52
+ end
@@ -0,0 +1 @@
1
+ {"status":0,"message":"Success.","comments":[{"comment":"test comment 1","user":{"username":"test_user"},"created_at":"2010-01-01 12:00:00"},{"comment":"test comment 2","user":{"username":"test_user"},"created_at":"2010-01-01 11:00:00"},{"comment":"test comment 3","user":{"username":"test_user"},"created_at":"2010-01-01 10:00:00"}],"pages":1,"count":3}
@@ -0,0 +1 @@
1
+ {"status":0,"message":"Success.","meme":{"id":1,"name":"test meme 1"}}
@@ -0,0 +1 @@
1
+ {"status":0,"message":"Success.","memes":[{"id":1,"name":"test meme 1"},{"id":2,"name":"test meme 2"},{"id":3,"name":"test meme 3"},{"id":4,"name":"test meme 4"}],"pages":1,"count":4}
@@ -0,0 +1 @@
1
+ {"status":0,"message":"Success.","meme_types":[{"id":1,"name":"TEST","url":"http://test.url","summary":"test summary","description":"test description"}]}
@@ -0,0 +1 @@
1
+ {"status":0,"message":"Success.","access_token":{"token":"test_token","secret":"test_secret"}}
@@ -0,0 +1 @@
1
+ {"status":0,"message":"Success.","verifier":"test_verifier"}
@@ -0,0 +1 @@
1
+ {"status":0,"message":"Success.","permissions":[{"id":0,"name":"memes","description":"Meme functionality (comment, rate, publish, etc.)."},{"id":1,"name":"private_data","description":"Access to private data (firstname, lastname, cleared and pending balance)."},{"id":2,"name":"order","description":"Order functionality (order, transactions, invoices, etc.)."}]}
@@ -0,0 +1 @@
1
+ {"status":0,"message":"Success.","request_token":{"token":"test_token","secret":"test_secret"}}
@@ -0,0 +1 @@
1
+ {"status":0,"message":"Success.","user_id":"test_user"}
@@ -0,0 +1 @@
1
+ {"status":1,"message":"Error.","error_code":"error","errors":{"test":{"test":"test error"}}}
@@ -0,0 +1 @@
1
+ {"status":0,"message":"Success."}
@@ -0,0 +1 @@
1
+ {"status":0,"message":"Success.","test_message":"test message"}
@@ -0,0 +1 @@
1
+ {"status":0,"message":"Success.","user":{"username":"test_user","email":"test@email.tld"}}
@@ -0,0 +1 @@
1
+ {"status":0,"message":"Success.","invoice":{"amount_user":10.0,"meme":{"name":"test meme 1"}}}
@@ -0,0 +1 @@
1
+ {"status":0,"message":"Success.","invoices":[{"amount_user":10.0,"meme":{"name":"test meme 1"}},{"amount_user":2.0,"meme":{"name":"test meme 2"}},{"amount_user":1.3,"meme":{"name":"test meme 3"}},{"amount_user":14.3,"meme":{"name":"test meme 4"}}],"pages":1,"count":4}
@@ -0,0 +1 @@
1
+ {"status":0,"message":"Success.","memes":[{"id":1,"name":"test meme 1"},{"id":2,"name":"test meme 2"},{"id":3,"name":"test meme 3"},{"id":4,"name":"test meme 4"}],"pages":1,"count":4}
@@ -0,0 +1 @@
1
+ {"status":0,"message":"Success.","paypal_url":"test_paypal_url"}
@@ -0,0 +1 @@
1
+ {"status":0,"message":"Success.","permissions":[0,1,2]}
@@ -0,0 +1 @@
1
+ {"status":0,"message":"Success.","transactions":[{"amount_user":10.0,"meme":{"name":"test meme 1"}},{"amount_user":2.0,"meme":{"name":"test meme 2"}},{"amount_user":1.3,"meme":{"name":"test meme 3"}},{"amount_user":14.3,"meme":{"name":"test meme 4"}}],"pages":1,"count":4}
@@ -0,0 +1 @@
1
+ {"status":0,"message":"Success.","user":{"username":"test_user","comments":3,"ratings":3,"language":"en","confirmed":true,"can_publish":true,"language":"en"}}
@@ -0,0 +1 @@
1
+ {"status":0,"message":"Success.","user":{"username":"test_user","comments":3,"ratings":3}}
data/test/helper.rb ADDED
@@ -0,0 +1,69 @@
1
+ require "rubygems"
2
+ require "test/unit"
3
+ require "fakeweb"
4
+
5
+ FakeWeb.allow_net_connect = false
6
+
7
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
8
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), "..", "lib"))
9
+ require "infomeme_client"
10
+
11
+ class Test::Unit::TestCase
12
+ def setup
13
+ @test_site = "https://test.api.site"
14
+ @test_api_key = "TEST_API_KEY"
15
+ @test_api_secret = "TEST_API_KEY"
16
+ @im_client = InfomemeClient.new :api_key => @test_api_key, :api_secret => @test_api_secret, :site => @test_site
17
+ end
18
+
19
+ private
20
+ def fake_request(method, url, options)
21
+ FakeWeb.register_uri(method, infomeme_url(url), options)
22
+ end
23
+
24
+ def fake_authorize_requests
25
+ fake_request(:get, "/oauth/verify_access", :body => fixture("oauth/verify_access"), :content_type => "application/json")
26
+ fake_request(:get, "/users/test_user", :body => fixture("users/user"), :content_type => "application/json")
27
+ fake_request(:get, "/users/test_user/permissions", :body => fixture("users/permissions"), :content_type => "application/json")
28
+ end
29
+
30
+ def authorize
31
+ fake_authorize_requests
32
+ assert_nothing_raised do
33
+ @im_client.authorize_with_token("test_token", "test_secret")
34
+ end
35
+ assert @im_client.authorized?
36
+ end
37
+
38
+ def fixture(filename)
39
+ File.read(File.dirname(__FILE__) + "/fixtures/#{filename}.json")
40
+ end
41
+
42
+ def infomeme_url(url)
43
+ url =~ /^http/ ? url : "#{@test_site}#{url}"
44
+ end
45
+
46
+ def check_paged(type, resp, options = {})
47
+ assert_instance_of InfomemeClient::Response, resp
48
+ assert_respond_to resp, type.to_sym
49
+ assert_respond_to resp, :pages
50
+ assert_respond_to resp, :count
51
+ check_list(type, resp.extract(type.to_sym))
52
+ assert_kind_of Integer, resp.pages
53
+ assert_kind_of Integer, resp.count
54
+ end
55
+
56
+ def check_list(type, list)
57
+ assert_instance_of Array, list
58
+ type = type.to_s.sub(/s\z/, "").to_sym
59
+ list.each do |elm|
60
+ check_eh(type, elm)
61
+ end
62
+ end
63
+
64
+ def check_eh(type, obj)
65
+ klass = InfomemeClient::EntityHash.const_get(type.to_s.split("_").map {|t| t.capitalize}.join.to_sym)
66
+ assert_instance_of Class, klass, "couldn't determine class from #{type.inspect} (#{klass.inspect})"
67
+ assert_instance_of klass, obj
68
+ end
69
+ end
@@ -0,0 +1,56 @@
1
+ require "helper"
2
+
3
+ class TestAuthorization < Test::Unit::TestCase
4
+ def test_request_authorization
5
+ fake_request(:get, "/oauth/request_token", :body => fixture("oauth/request_token"), :content_type => "application/json")
6
+ req_token = nil
7
+ assert_nothing_raised do
8
+ req_token = @im_client.request_authorization
9
+ end
10
+ assert_kind_of InfomemeClient::EntityHash, req_token
11
+ assert_respond_to req_token, :token, req_token.inspect
12
+ assert_respond_to req_token, :secret, req_token.inspect
13
+ assert_equal "test_token", req_token.token, req_token.inspect
14
+ assert_equal "test_secret", req_token.secret, req_token.inspect
15
+ end
16
+
17
+ def test_authorize_with_verifier
18
+ fake_request(:get, "/oauth/access_token?oauth_verifier=test_verifier", :body => fixture("oauth/access_token"), :content_type => "application/json")
19
+ fake_authorize_requests
20
+ assert_nothing_raised do
21
+ @im_client.authorize_with_verifier("test_token", "test_secret", "test_verifier")
22
+ end
23
+ check_authorized
24
+ end
25
+
26
+ def test_authorize_with_token
27
+ authorize
28
+ check_authorized
29
+ end
30
+
31
+ def test_authorize_with_credentials
32
+ fake_request(:get, "/oauth/request_token", :body => fixture("oauth/request_token"), :content_type => "application/json")
33
+ fake_request(:get, "/oauth/permissions", :body => fixture("oauth/permissions"), :content_type => "application/json")
34
+ fake_request(:put, "/oauth/authorize", :body => fixture("oauth/authorize"), :content_type => "application/json")
35
+ fake_request(:get, "/oauth/access_token?oauth_verifier=test_verifier", :body => fixture("oauth/access_token"), :content_type => "application/json")
36
+ fake_authorize_requests
37
+ assert_nothing_raised do
38
+ @im_client.authorize_with_credentials("test_login", "test_password")
39
+ end
40
+ check_authorized
41
+ assert @im_client.has_permission?(0)
42
+ assert @im_client.has_permission?(1)
43
+ assert @im_client.has_permission?(2)
44
+ end
45
+
46
+ private
47
+ def check_authorized
48
+ assert @im_client.authorized?
49
+ assert @im_client.confirmed?
50
+ assert @im_client.can_publish?
51
+ cred = @im_client.credentials
52
+ assert_equal "test_user", cred[:login]
53
+ assert_equal "test_token", cred[:token]
54
+ assert_equal "test_secret", cred[:secret]
55
+ end
56
+ end
@@ -0,0 +1,46 @@
1
+ require "helper"
2
+
3
+ class TestCommunication < Test::Unit::TestCase
4
+ def test_invalid_url
5
+ fake_request(:get, "/invalid_url", :body => "Error not found.", :status => ["404", "Not Found"])
6
+ assert_raise InfomemeClient::Error::RequestFailed do
7
+ @im_client.handle_response :get, "/invalid_url"
8
+ end
9
+ end
10
+
11
+ def test_response_error
12
+ fake_request(:get, "/test_error", :body => fixture("response/error"), :status => ["400", "Bad Request"], :content_type => "application/json")
13
+ assert_raises InfomemeClient::Error do
14
+ resp = @im_client.handle_response :get, "/test_error"
15
+ end
16
+ resp = @im_client.get "/test_error"
17
+ assert resp.error?
18
+ assert_respond_to resp, :errors
19
+ assert_kind_of InfomemeClient::EntityHash, resp.errors
20
+ assert_equal resp.errors.to_hash, {:test => {:test => "test error"}}
21
+ end
22
+
23
+ def test_response_ok
24
+ fake_request(:get, "/test_response", :body => fixture("response/test"), :content_type => "application/json")
25
+ resp = nil
26
+ assert_nothing_raised do
27
+ resp = @im_client.handle_response :get, "/test_response"
28
+ end
29
+ assert_instance_of InfomemeClient::Response, resp
30
+ assert_respond_to resp, :test_message
31
+ assert_equal resp.test_message, "test message"
32
+ end
33
+
34
+ def test_response_extract
35
+ fake_request(:get, "/test_extract", :body => fixture("response/test_extract"), :content_type => "application/json")
36
+ resp = nil
37
+ assert_nothing_raised do
38
+ resp = @im_client.handle_response :get, "/test_extract"
39
+ end
40
+ assert_instance_of InfomemeClient::Response, resp
41
+ assert_respond_to resp, :extract
42
+ assert_respond_to resp, :user
43
+ user = resp.extract(:user)
44
+ assert_instance_of InfomemeClient::EntityHash::User, user
45
+ end
46
+ end
@@ -0,0 +1,15 @@
1
+ require "helper"
2
+
3
+ class TestEntityHash < Test::Unit::TestCase
4
+ def test_entity_hash_attributes_available
5
+ eh = InfomemeClient::EntityHash.new({:foo => "bar"})
6
+ assert_respond_to eh, :foo
7
+ assert_equal "bar", eh.foo
8
+ end
9
+
10
+ def test_entity_hash_retrieve_hash
11
+ hash = {:foo => "bar", :bar => [{:foo => "bar"}]}
12
+ eh = InfomemeClient::EntityHash.new(hash)
13
+ assert_equal hash, eh.to_hash
14
+ end
15
+ end
@@ -0,0 +1,46 @@
1
+ require "helper"
2
+
3
+ class TestMemeFunctions < Test::Unit::TestCase
4
+ def test_browse
5
+ fake_request(:get, "/memes?page=1&pageSize=25&sort=date-asc", :body => fixture("memes/memes"), :content_type => "application/json")
6
+ check_paged :memes, @im_client.browse(:page => 1, :pageSize => 25, :sort => "date-asc", :no_extract => true)
7
+ check_list :memes, @im_client.browse(:page => 1, :pageSize => 25, :sort => "date-asc")
8
+ end
9
+
10
+ def test_search
11
+ fake_request(:get, "/memes/search?search_token=a&page=1&pageSize=25&sort=date-asc", :body => fixture("memes/memes"), :content_type => "application/json")
12
+ check_paged :memes, @im_client.search("a", :page => 1, :pageSize => 25, :sort => "date-asc", :no_extract => true)
13
+ check_list :memes, @im_client.search("a", :page => 1, :pageSize => 25, :sort => "date-asc")
14
+ end
15
+
16
+ def test_meme_types
17
+ fake_request(:get, "/memes/types", :body => fixture("memes/types"), :content_type => "application/json")
18
+ types = @im_client.meme_types
19
+ check_list :meme_types, types
20
+ assert types.size > 0
21
+ assert_respond_to types[0], :id
22
+ assert_respond_to types[0], :name
23
+ assert_respond_to types[0], :url
24
+ assert_respond_to types[0], :summary
25
+ assert_respond_to types[0], :description
26
+ assert_equal 1, types[0].id
27
+ assert_equal "TEST", types[0].name
28
+ assert_equal "http://test.url", types[0].url
29
+ assert_equal "test summary", types[0].summary
30
+ assert_equal "test description", types[0].description
31
+ end
32
+
33
+ def test_meme
34
+ fake_request(:get, "/memes/1", :body => fixture("memes/meme"), :content_type => "application/json")
35
+ meme = @im_client.meme(1)
36
+ check_eh :meme, meme
37
+ assert_respond_to meme, :name
38
+ assert_equal "test meme 1", meme.name
39
+ end
40
+
41
+ def test_meme_comments
42
+ fake_request(:get, "/memes/1/comments?page=1&pageSize=25", :body => fixture("memes/comments"), :content_type => "application/json")
43
+ check_paged :comments, @im_client.meme_comments(1, :page => 1, :pageSize => 25, :no_extract => true)
44
+ check_list :comments, @im_client.meme_comments(1, :page => 1, :pageSize => 25)
45
+ end
46
+ end
@@ -0,0 +1,25 @@
1
+ require "helper"
2
+
3
+ class TestPermissions < Test::Unit::TestCase
4
+ def test_all_permissions
5
+ fake_request(:get, "/oauth/permissions", :body => fixture("oauth/permissions"), :content_type => "application/json")
6
+ perms = @im_client.all_permissions
7
+ assert_instance_of Array, perms
8
+ all = {0 => "memes", 1 => "private_data", 2 => "order"}
9
+ perms.each do |perm|
10
+ assert_instance_of InfomemeClient::EntityHash, perm
11
+ assert_respond_to perm, :id
12
+ assert_respond_to perm, :name
13
+ assert_respond_to perm, :description
14
+ assert all.key?(perm.id)
15
+ assert_equal all[perm.id], perm.name
16
+ end
17
+ end
18
+
19
+ def test_refresh_permissions
20
+ authorize
21
+ (0..2).each do |perm|
22
+ assert @im_client.has_permission?(perm)
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,15 @@
1
+ require "helper"
2
+
3
+ class TestUserFunctions < Test::Unit::TestCase
4
+ def test_user
5
+ fake_request(:get, "/users/test_user", :body => fixture("users/user_public"), :content_type => "application/json")
6
+ user = @im_client.user("test_user")
7
+ check_eh :user, user
8
+ assert_respond_to user, :username
9
+ assert_equal "test_user", user.username
10
+ assert_respond_to user, :comments
11
+ assert_equal 3, user.comments
12
+ assert_respond_to user, :ratings
13
+ assert_equal 3, user.ratings
14
+ end
15
+ end
@@ -0,0 +1,47 @@
1
+ require "helper"
2
+
3
+ class TestUserMemeFunctions < Test::Unit::TestCase
4
+ def test_library
5
+ fake_request(:get, "/users/test_user/memes?page=1&pageSize=25&sort=date-asc", :body => fixture("users/memes"), :content_type => "application/json")
6
+ authorize
7
+ check_paged :memes, @im_client.library(:page => 1, :pageSize => 25, :sort => "date-asc", :no_extract => true)
8
+ check_list :memes, @im_client.library(:page => 1, :pageSize => 25, :sort => "date-asc")
9
+ end
10
+
11
+ def test_published
12
+ fake_request(:get, "/users/test_user/memes/published?page=1&pageSize=25&sort=date-asc", :body => fixture("users/memes"), :content_type => "application/json")
13
+ authorize
14
+ check_paged :memes, @im_client.published(:page => 1, :pageSize => 25, :sort => "date-asc", :no_extract => true)
15
+ check_list :memes, @im_client.published(:page => 1, :pageSize => 25, :sort => "date-asc")
16
+ end
17
+
18
+ def test_inactive
19
+ fake_request(:get, "/users/test_user/memes/inactive?page=1&pageSize=25&sort=date-asc", :body => fixture("users/memes"), :content_type => "application/json")
20
+ authorize
21
+ check_paged :memes, @im_client.inactive(:page => 1, :pageSize => 25, :sort => "date-asc", :no_extract => true)
22
+ check_list :memes, @im_client.inactive(:page => 1, :pageSize => 25, :sort => "date-asc")
23
+ end
24
+
25
+ def test_incomplete
26
+ fake_request(:get, "/users/test_user/memes/incomplete?page=1&pageSize=25&sort=date-asc", :body => fixture("users/memes"), :content_type => "application/json")
27
+ authorize
28
+ check_paged :memes, @im_client.incomplete(:page => 1, :pageSize => 25, :sort => "date-asc", :no_extract => true)
29
+ check_list :memes, @im_client.incomplete(:page => 1, :pageSize => 25, :sort => "date-asc")
30
+ end
31
+
32
+ def test_comment
33
+ fake_request(:post, "/users/test_user/memes/1/comments", :body => fixture("response/ok"), :content_type => "application/json")
34
+ authorize
35
+ assert_nothing_raised do
36
+ assert @im_client.comment(1, "test comment")
37
+ end
38
+ end
39
+
40
+ def test_rate
41
+ fake_request(:post, "/users/test_user/memes/1/rates", :body => fixture("response/ok"), :content_type => "application/json")
42
+ authorize
43
+ assert_nothing_raised do
44
+ assert @im_client.rate(1, 1)
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,45 @@
1
+ require "helper"
2
+
3
+ class TestUserOrderFunctions < Test::Unit::TestCase
4
+ def test_order_with_balance
5
+ fake_request(:post, "/users/test_user/memes/1/order", :body => fixture("response/ok"), :content_type => "application/json")
6
+ authorize
7
+ assert_nothing_raised do
8
+ assert @im_client.order_with_balance(1)
9
+ end
10
+ end
11
+
12
+ def test_order_with_paypal
13
+ fake_request(:post, "/users/test_user/memes/1/order", :body => fixture("users/order_paypal"), :content_type => "application/json")
14
+ authorize
15
+ assert_nothing_raised do
16
+ assert_equal "test_paypal_url", @im_client.order_with_paypal(1)
17
+ end
18
+ end
19
+
20
+ def test_transactions
21
+ fake_request(:get, "/users/test_user/transactions?page=1&pageSize=25", :body => fixture("users/transactions"), :content_type => "application/json")
22
+ authorize
23
+ check_paged :transactions, @im_client.transactions(:page => 1, :pageSize => 25, :no_extract => true)
24
+ check_list :transactions, @im_client.transactions(:page => 1, :pageSize => 25)
25
+ end
26
+
27
+ def test_invoices
28
+ fake_request(:get, "/users/test_user/invoices?page=1&pageSize=25", :body => fixture("users/invoices"), :content_type => "application/json")
29
+ authorize
30
+ check_paged :invoices, @im_client.invoices(:page => 1, :pageSize => 25, :no_extract => true)
31
+ check_list :invoices, @im_client.invoices(:page => 1, :pageSize => 25)
32
+ end
33
+
34
+ def test_invoice
35
+ fake_request(:get, "/users/test_user/invoices/1", :body => fixture("users/invoice"), :content_type => "application/json")
36
+ authorize
37
+ invoice = @im_client.invoice(1)
38
+ check_eh :invoice, invoice
39
+ assert_respond_to invoice, :amount_user
40
+ assert_equal 10.0, invoice.amount_user
41
+ assert_respond_to invoice, :meme
42
+ assert_respond_to invoice.meme, :name
43
+ assert_equal "test meme 1", invoice.meme.name
44
+ end
45
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: infomeme_client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.2.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -17,7 +17,56 @@ email: gem_info@infomeme.com
17
17
  executables: []
18
18
  extensions: []
19
19
  extra_rdoc_files: []
20
- files: []
20
+ files:
21
+ - lib/infomeme_client/errors.rb
22
+ - lib/infomeme_client/entity_hash.rb
23
+ - lib/infomeme_client/functions/user.rb
24
+ - lib/infomeme_client/functions/user_order.rb
25
+ - lib/infomeme_client/functions/meme.rb
26
+ - lib/infomeme_client/functions/user_meme.rb
27
+ - lib/infomeme_client/communication.rb
28
+ - lib/infomeme_client/response.rb
29
+ - lib/infomeme_client/base.rb
30
+ - lib/infomeme_client/functions.rb
31
+ - lib/infomeme_client/permissions.rb
32
+ - lib/infomeme_client/entity_hash/user.rb
33
+ - lib/infomeme_client/entity_hash/meme_type.rb
34
+ - lib/infomeme_client/entity_hash/comment.rb
35
+ - lib/infomeme_client/entity_hash/meme.rb
36
+ - lib/infomeme_client/entity_hash/invoice.rb
37
+ - lib/infomeme_client/entity_hash/transaction.rb
38
+ - lib/infomeme_client/meme_application.rb
39
+ - lib/infomeme_client.rb
40
+ - test/helper.rb
41
+ - test/test_user_order_functions.rb
42
+ - test/test_permissions.rb
43
+ - test/test_entity_hash.rb
44
+ - test/test_communication.rb
45
+ - test/fixtures/oauth/access_token.json
46
+ - test/fixtures/oauth/permissions.json
47
+ - test/fixtures/oauth/verify_access.json
48
+ - test/fixtures/oauth/authorize.json
49
+ - test/fixtures/oauth/request_token.json
50
+ - test/fixtures/response/test.json
51
+ - test/fixtures/response/ok.json
52
+ - test/fixtures/response/test_extract.json
53
+ - test/fixtures/response/error.json
54
+ - test/fixtures/memes/meme.json
55
+ - test/fixtures/memes/types.json
56
+ - test/fixtures/memes/comments.json
57
+ - test/fixtures/memes/memes.json
58
+ - test/fixtures/users/transactions.json
59
+ - test/fixtures/users/invoices.json
60
+ - test/fixtures/users/user_public.json
61
+ - test/fixtures/users/permissions.json
62
+ - test/fixtures/users/memes.json
63
+ - test/fixtures/users/user.json
64
+ - test/fixtures/users/invoice.json
65
+ - test/fixtures/users/order_paypal.json
66
+ - test/test_authorization.rb
67
+ - test/test_user_functions.rb
68
+ - test/test_meme_functions.rb
69
+ - test/test_user_meme_functions.rb
21
70
  has_rdoc: true
22
71
  homepage: https://github.com/Infomeme/infomeme_client
23
72
  licenses: []