infomeme_client 0.1.4 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (56) hide show
  1. metadata +26 -165
  2. data/.document +0 -5
  3. data/.gitignore +0 -21
  4. data/LICENSE +0 -20
  5. data/README.rdoc +0 -8
  6. data/Rakefile +0 -55
  7. data/VERSION +0 -1
  8. data/lib/infomeme_client.rb +0 -22
  9. data/lib/infomeme_client/base.rb +0 -77
  10. data/lib/infomeme_client/communication.rb +0 -75
  11. data/lib/infomeme_client/entity_hash.rb +0 -66
  12. data/lib/infomeme_client/entity_hash/comment.rb +0 -2
  13. data/lib/infomeme_client/entity_hash/invoice.rb +0 -2
  14. data/lib/infomeme_client/entity_hash/meme.rb +0 -45
  15. data/lib/infomeme_client/entity_hash/meme_type.rb +0 -2
  16. data/lib/infomeme_client/entity_hash/transaction.rb +0 -2
  17. data/lib/infomeme_client/entity_hash/user.rb +0 -2
  18. data/lib/infomeme_client/errors.rb +0 -22
  19. data/lib/infomeme_client/functions.rb +0 -16
  20. data/lib/infomeme_client/functions/meme.rb +0 -23
  21. data/lib/infomeme_client/functions/user.rb +0 -79
  22. data/lib/infomeme_client/functions/user_meme.rb +0 -127
  23. data/lib/infomeme_client/functions/user_order.rb +0 -31
  24. data/lib/infomeme_client/meme_application.rb +0 -18
  25. data/lib/infomeme_client/permissions.rb +0 -22
  26. data/lib/infomeme_client/response.rb +0 -52
  27. data/test/fixtures/memes/comments.json +0 -1
  28. data/test/fixtures/memes/meme.json +0 -1
  29. data/test/fixtures/memes/memes.json +0 -1
  30. data/test/fixtures/memes/types.json +0 -1
  31. data/test/fixtures/oauth/access_token.json +0 -1
  32. data/test/fixtures/oauth/authorize.json +0 -1
  33. data/test/fixtures/oauth/permissions.json +0 -1
  34. data/test/fixtures/oauth/request_token.json +0 -1
  35. data/test/fixtures/oauth/verify_access.json +0 -1
  36. data/test/fixtures/response/error.json +0 -1
  37. data/test/fixtures/response/ok.json +0 -1
  38. data/test/fixtures/response/test.json +0 -1
  39. data/test/fixtures/response/test_extract.json +0 -1
  40. data/test/fixtures/users/invoice.json +0 -1
  41. data/test/fixtures/users/invoices.json +0 -1
  42. data/test/fixtures/users/memes.json +0 -1
  43. data/test/fixtures/users/order_paypal.json +0 -1
  44. data/test/fixtures/users/permissions.json +0 -1
  45. data/test/fixtures/users/transactions.json +0 -1
  46. data/test/fixtures/users/user.json +0 -1
  47. data/test/fixtures/users/user_public.json +0 -1
  48. data/test/helper.rb +0 -69
  49. data/test/test_authorization.rb +0 -56
  50. data/test/test_communication.rb +0 -46
  51. data/test/test_entity_hash.rb +0 -15
  52. data/test/test_meme_functions.rb +0 -46
  53. data/test/test_permissions.rb +0 -25
  54. data/test/test_user_functions.rb +0 -15
  55. data/test/test_user_meme_functions.rb +0 -47
  56. data/test/test_user_order_functions.rb +0 -45
@@ -1,66 +0,0 @@
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
@@ -1,2 +0,0 @@
1
- class InfomemeClient::EntityHash::Comment < InfomemeClient::EntityHash
2
- end
@@ -1,2 +0,0 @@
1
- class InfomemeClient::EntityHash::Invoice < InfomemeClient::EntityHash
2
- end
@@ -1,45 +0,0 @@
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
@@ -1,2 +0,0 @@
1
- class InfomemeClient::EntityHash::MemeType < InfomemeClient::EntityHash
2
- end
@@ -1,2 +0,0 @@
1
- class InfomemeClient::EntityHash::Transaction < InfomemeClient::EntityHash
2
- end
@@ -1,2 +0,0 @@
1
- class InfomemeClient::EntityHash::User < InfomemeClient::EntityHash
2
- end
@@ -1,22 +0,0 @@
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
@@ -1,16 +0,0 @@
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
@@ -1,23 +0,0 @@
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
@@ -1,79 +0,0 @@
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
@@ -1,127 +0,0 @@
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
@@ -1,31 +0,0 @@
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
@@ -1,18 +0,0 @@
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