gocarrot 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/carrot.rb +180 -0
  2. metadata +46 -0
data/lib/carrot.rb ADDED
@@ -0,0 +1,180 @@
1
+ # Carrot -- Copyright (C) 2012 Carrot Inc.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ require 'base64'
16
+ require 'digest'
17
+ require 'json'
18
+ require 'net/http'
19
+ require 'cgi'
20
+
21
+ class Carrot
22
+ attr_accessor :app_id, :app_secret, :uuid, :hostname
23
+
24
+ # Create a new Carrot instance.
25
+ #
26
+ # @param app_id [String] Facebook Application Id for your application.
27
+ # @param app_secret [String] Carrot Application Secret for your application.
28
+ # @param udid [String] a per-user unique identifier. We suggest using email address or the Facebook 'third_party_id'. You may also specify `nil` and instead provide a value as you call methods.
29
+ # @param hostname [String] the hostname to use for Carrot API endpoints.
30
+ def initialize(app_id, app_secret, uuid = nil, hostname = 'gocarrot.com')
31
+ @app_id = app_id
32
+ @app_secret = app_secret
33
+ @uuid = uuid
34
+ @hostname = hostname
35
+ end
36
+
37
+ # Validate the active, or specified user.
38
+ #
39
+ # @param udid [String] a per-user unique identifier or `nil` for the value of {Carrot#uuid}. We suggest using email address or the Facebook 'third_party_id'.
40
+ #
41
+ # @return [Symbol] returns one of: `:authorized`, `:read_only`, `:not_created`, or `:unknown`
42
+ def validate_user(uuid = @uuid)
43
+ @uuid = uuid
44
+ http = Net::HTTP.new @hostname, 443
45
+ http.use_ssl = true
46
+ request = Net::HTTP::Get.new "/games/#{@app_id}/users.json?id=#{CGI::escape(uuid.to_s)}"
47
+ response = http.request(request)
48
+ case response
49
+ when Net::HTTPSuccess # User has fully-authorized app
50
+ return :authorized
51
+ when Net::HTTPUnauthorized # Read-only permissions
52
+ return :read_only
53
+ when Net::HTTPClientError # User has not been created
54
+ return :not_created
55
+ else
56
+ puts response.body
57
+ end
58
+ return :unknown
59
+ end
60
+
61
+ # Add a user to the Carrot service.
62
+ #
63
+ # @param access_token [String] the Facebook user access token for the user to add.
64
+ # @param udid [String] a per-user unique identifier or `nil` to use the value of {Carrot#uuid}. We suggest using email address or the Facebook 'third_party_id'.
65
+ #
66
+ # @return [Symbol] one of: `:authorized`, `:read_only`, `:not_authorized` or `:unknown`
67
+ def create_user(access_token, uuid = @uuid)
68
+ http = Net::HTTP.new @hostname, 443
69
+ http.use_ssl = true
70
+
71
+ request = Net::HTTP::Post.new "/games/#{@app_id}/users.json"
72
+ request.set_form_data({'access_token' => access_token, 'api_key' => @uuid})
73
+ response = http.request(request)
74
+ case response
75
+ when Net::HTTPSuccess # User created
76
+ return :authorized
77
+ when Net::HTTPUnauthorized # Read-only permissions
78
+ return :read_only
79
+ when Net::HTTPMethodNotAllowed # User has not authorized app
80
+ return :not_authorized
81
+ else
82
+ puts response.body
83
+ end
84
+ return :unknown
85
+ end
86
+
87
+ # Post an achievement to the Carrot service.
88
+ #
89
+ # @param achievement_id [String] the achievement identifier.
90
+ # @param udid [String, nil] a per-user unique identifier or `nil` to use the value of {Carrot#uuid}. We suggest using email address or the Facebook 'third_party_id'.
91
+ #
92
+ # @return [Symbol] one of: `:success`, `:read_only`, `:not_found`, `:not_authorized` or `:unknown`
93
+ def post_achievement(achievement_id, uuid = @uuid)
94
+ return post_signed_request("/me/achievements.json", {'api_key' => uuid, 'achievement_id' => achievement_id})
95
+ end
96
+
97
+ # Post a high score to the Carrot service.
98
+ #
99
+ # @param score [String, Integer] the high score value to post.
100
+ # @param leaderboard_id [String] the leaderboard identifier to which the score should be posted.
101
+ # @param udid [String] a per-user unique identifier or `nil` to use the value of {Carrot#uuid}. We suggest using email address or the Facebook 'third_party_id'.
102
+ #
103
+ # @return [Symbol] one of: `:success`, `:read_only`, `:not_found`, `:not_authorized` or `:unknown`
104
+ def post_highscore(score, leaderboard_id = "", uuid = @uuid)
105
+ return post_signed_request("/me/scores.json", {'api_key' => uuid, 'value' => score, 'leaderboard_id' => leaderboard_id})
106
+ end
107
+
108
+ # Post an Open Graph action to the Carrot service.
109
+ #
110
+ # If creating an object, you are required to include 'title', 'description', 'image_url' and
111
+ # 'object_type' in `object_properties`.
112
+ #
113
+ # @param action_id [String] Carrot action id.
114
+ # @param object_instance_id [String] the object instance id of the Carrot object type to create or post; use `nil` if you are creating a throw-away object.
115
+ # @param action_properties [Hash] the properties to be sent along with the Carrot action, or `nil`.
116
+ # @param object_properties [Hash] the properties for the new object, if creating an object, or `nil`.
117
+ # @param udid [String] a per-user unique identifier or `nil` to use the value of {Carrot#uuid}. We suggest using email address or the Facebook 'third_party_id'.
118
+ #
119
+ # @return [Symbol] one of: `:success`, `:read_only`, `:not_found`, `:not_authorized` or `:unknown`
120
+ def post_action(action_id, object_instance_id, action_properties = {}, object_properties = {}, uuid = @uuid)
121
+ payload = {
122
+ 'api_key' => uuid,
123
+ 'action_id' => action_id,
124
+ 'action_properties' => JSON.generate(action_properties || {}),
125
+ 'object_properties' => JSON.generate(object_properties || {})
126
+ }
127
+ payload.update({'object_instance_id' => object_instance_id}) if object_instance_id
128
+ return post_signed_request("/me/actions.json", payload)
129
+ end
130
+
131
+ # Post a 'Like' to the Carrot service.
132
+ #
133
+ # @param object_type [Symbol] one of: `:game`, `:publisher`, `:achievement`, or `:object`.
134
+ # @param object_id [String] if `:achievement` or `:object` is specified as `object_type` this is the identifier of the achievement or object.
135
+ # @param udid [String] a per-user unique identifier or `nil` to use the value of {Carrot#uuid}. We suggest using email address or the Facebook 'third_party_id'.
136
+ #
137
+ # @return [Symbol] one of: `:success`, `:read_only`, `:not_found`, `:not_authorized` or `:unknown`
138
+ def post_like(object_type, object_id = nil, uuid = @uuid)
139
+ payload = {
140
+ 'api_key' => uuid,
141
+ 'object' => "#{object_type}#{":#{object_id}" if object_id}"
142
+ }
143
+ return post_signed_request("/me/like.json", payload)
144
+ end
145
+
146
+ private
147
+
148
+ def post_signed_request(endpoint, payload, uuid = @uuid)
149
+ payload.update({
150
+ 'game_id' => @app_id,
151
+ 'request_date' => Time.now.to_i,
152
+ 'request_id' => Digest::SHA1.hexdigest(Time.now.to_s)[8..16]
153
+ })
154
+ string_to_sign = "POST\n#{@hostname}\n#{endpoint}\n"
155
+ sig_keys = payload.keys.sort
156
+ string_to_sign += sig_keys.map { |k| "#{k}=#{payload[k]}" }.join('&')
157
+ signature = Base64.encode64(OpenSSL::HMAC.digest(OpenSSL::Digest::Digest.new("sha256"), @app_secret, string_to_sign)).strip
158
+ payload.update({'sig' => signature})
159
+
160
+ http = Net::HTTP.new @hostname, 443
161
+ http.use_ssl = true
162
+
163
+ request = Net::HTTP::Post.new endpoint
164
+ request.set_form_data(payload)
165
+ response = http.request(request)
166
+ case response
167
+ when Net::HTTPSuccess # Success
168
+ return :success
169
+ when Net::HTTPUnauthorized # User has read-only permissions
170
+ return :read_only
171
+ when Net::HTTPNotFound # Resource not found
172
+ return :not_found
173
+ when Net::HTTPMethodNotAllowed # User has not authorized app
174
+ return :not_authorized
175
+ else
176
+ puts response.body
177
+ end
178
+ return :unknown
179
+ end
180
+ end
metadata ADDED
@@ -0,0 +1,46 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: gocarrot
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.3
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Pat Wilson
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-12-05 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: Interface with your app on http://gocarrot.com
15
+ email: pat@gocarrot.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - lib/carrot.rb
21
+ homepage: http://rubygems.org/gems/gocarrot
22
+ licenses: []
23
+ post_install_message:
24
+ rdoc_options: []
25
+ require_paths:
26
+ - lib
27
+ required_ruby_version: !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ required_rubygems_version: !ruby/object:Gem::Requirement
34
+ none: false
35
+ requirements:
36
+ - - ! '>='
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ requirements: []
40
+ rubyforge_project:
41
+ rubygems_version: 1.8.15
42
+ signing_key:
43
+ specification_version: 3
44
+ summary: Carrot
45
+ test_files: []
46
+ has_rdoc: