Class: Punchtab::API
- Inherits:
-
Object
- Object
- Punchtab::API
- Defined in:
- lib/punchtab/api.rb
Constant Summary collapse
- BASE_API_URL =
'https://api.punchtab.com/v1'- ACTIVITIES =
%w(visit tweet like plusone comment invite reply apply share purchase addtotimeline search download view checkin subscribe follow)
Instance Attribute Summary collapse
-
#access_token ⇒ Object
readonly
Returns the value of attribute access_token.
Instance Method Summary collapse
- #authenticate ⇒ Object
-
#create_activity(activity_name, points = 100) ⇒ Object
Required Parameters * ‘activity_name’<~String> - retrieve only a list of activity from the activity_name.
-
#get_activity(options = {}) ⇒ Object
Required Parameters None Optional Parameters options<~Hash> * ‘activity_name’<~String> - retrieve only a list of activities for the activity.
-
#get_leaderboard(options = {}) ⇒ Object
Required Parameters None Optional Parameters options<~Hash> * ‘with’<~String> - User Id or ‘me’.
-
#get_reward(options = {}) ⇒ Object
Required Parameters None Optional Parameters options<~Hash> * ‘limit’<~Integer> - limits the number of rewards.
-
#get_user ⇒ Object
Required Parameters None Optional Parameters None Return api.punchtab.com/v1/user?access_token=<access_token>.
-
#initialize(options = {}) ⇒ API
constructor
A new instance of API.
-
#logout ⇒ Object
Required Parameters None Optional Parameters None Return api.punchtab.com/v1/auth/logout.
-
#redeem_activity_offer(reward_id) ⇒ Object
Required Parameters * ‘reward_id’<~Integer> - reward id for the activity offer to redeem Optional Parameters None Return curl i -X POST ‘reward_id=123’ api.punchtab.com/v1/activity/redeem?access_token=<access_token>.
-
#status ⇒ Object
Required Parameters None Optional Parameters None Return api.punchtab.com/v1/auth/status.
Methods included from Utils
Constructor Details
#initialize(options = {}) ⇒ API
Returns a new instance of API.
22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/punchtab/api.rb', line 22 def initialize( = {}) @client_id = [:client_id] # required @access_key = [:access_key] # required @secret_key = [:secret_key] # required @domain = [:domain] # required @user_info = [:user_info] # optional Punchtab::API.headers 'Referer' => "http://#{@domain}" unless @client_id && @access_key && @secret_key && @domain raise Exception.new('Client Id, Access Key, Secret Key and Domain are required to authenticate, before using PunchTab services.') end end |
Instance Attribute Details
#access_token ⇒ Object (readonly)
Returns the value of attribute access_token.
20 21 22 |
# File 'lib/punchtab/api.rb', line 20 def access_token @access_token end |
Instance Method Details
#authenticate ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/punchtab/api.rb', line 39 def authenticate # setup the user data structure user_data = {:id => @client_id} if @user_info user_data.merge!(@user_info) end # prepare authentication params time_stamp = Time.now.to_i auth_request = Base64.encode64(JSON.dump(user_data)) string_to_sign = "#{auth_request} #{time_stamp}" hmac = OpenSSL::HMAC.new(@secret_key, OpenSSL::Digest::SHA1.new) signature = hmac.update(string_to_sign).hexdigest # make the POST call path = '/auth/sso' # setup the post params post_data = { :client_id => @client_id, :key => @access_key, :auth_request => auth_request, :timestamp => time_stamp, :signature => signature } raw_response = Punchtab::API.post(path, :body => post_data) response = Punchtab::Utils.process_response(raw_response) # return the access token @access_token = response.authResponse.accessToken end |
#create_activity(activity_name, points = 100) ⇒ Object
Required Parameters
* 'activity_name'<~String> - retrieve only a list of activity from the activity_name.
* 'points'<~Integer> - points for the activity, default is 100
Optional Parameters
None
Return curl i -X POST ‘points=200’ api.punchtab.com/v1/activity/<activity_name>?access_token=<access_token>
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
# File 'lib/punchtab/api.rb', line 144 def create_activity(activity_name, points=100) unless activity_valid?(activity_name) puts "Specify an activity from the list: '#{activity_list}'" return end # make the POST call if activity_name path = "/activity/#{activity_name}" else path = '/activity' end = {:access_token => @access_token} raw_response = Punchtab::API.post(path, {:body => "points=#{points}", :query => }) Punchtab::Utils.process_response(raw_response) end |
#get_activity(options = {}) ⇒ Object
Required Parameters
None
Optional Parameters
<~Hash>
* 'activity_name'<~String> - retrieve only a list of activities for the activity.
* 'limit'<~Integer> - limits the number of activities.
* 'user_id'<~Integer> - retrieve the activity for a specific user_id, instead of the user currently logged in.
Return api.punchtab.com/v1/activity/[activity_name]?access_token=<access_token>
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/punchtab/api.rb', line 119 def get_activity(={}) activity_name = .delete(:activity_name) unless activity_name.nil? || activity_valid?(activity_name) puts "Specify an activity from the list: '#{activity_list}'" return end # make the GET call if activity_name path = "/activity/#{activity_name}" else path = '/activity' end .merge!({:access_token => @access_token}) raw_response = Punchtab::API.get(path, :query => ) Punchtab::Utils.process_response(raw_response) end |
#get_leaderboard(options = {}) ⇒ Object
Required Parameters
None
Optional Parameters
<~Hash>
* 'with'<~String> - User Id or 'me'. Defaults to 'me', which will get a leaderboard with the current user.
* 'days'<~String> - if set to 'all' returns the leaderboard from the beginning using redeemable points, otherwise it returns the leaderboard from the last 30 days.
* 'limit'<~Integer> - limits the number of users in the leaderboard.
* 'page'<~Integer> - specifies the page of result you want (rank will be relative to the page).
Return api.punchtab.com/v1/leaderboard?access_token=<access_token>
224 225 226 227 228 229 230 231 |
# File 'lib/punchtab/api.rb', line 224 def get_leaderboard(={}) # make the GET call path = '/leaderboard' .merge!({:with => 'me', :access_token => @access_token}) raw_response = Punchtab::API.get(path, :query => ) Punchtab::Utils.process_response(raw_response) end |
#get_reward(options = {}) ⇒ Object
Required Parameters
None
Optional Parameters
<~Hash>
* 'limit'<~Integer> - limits the number of rewards.
Return api.punchtab.com/v1/reward?access_token=<access_token>
203 204 205 206 207 208 209 210 |
# File 'lib/punchtab/api.rb', line 203 def get_reward(={}) # make the GET call path = '/reward' .merge!({:access_token => @access_token}) raw_response = Punchtab::API.get(path, :query => ) Punchtab::Utils.process_response(raw_response) end |
#get_user ⇒ Object
Required Parameters
None
Optional Parameters
None
Return api.punchtab.com/v1/user?access_token=<access_token>
185 186 187 188 189 190 191 192 |
# File 'lib/punchtab/api.rb', line 185 def get_user # make the GET call path = '/user' = {:access_token => @access_token} raw_response = Punchtab::API.get(path, :query => ) Punchtab::Utils.process_response(raw_response) end |
#logout ⇒ Object
Required Parameters
None
Optional Parameters
None
Return
https://api.punchtab.com/v1/auth/logout
76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/punchtab/api.rb', line 76 def logout # make the POST call path = '/auth/logout' # setup the post params post_data = { :token => @access_token, :key => @access_key } raw_response = Punchtab::API.post(path, :body => post_data) Punchtab::Utils.process_response(raw_response) end |
#redeem_activity_offer(reward_id) ⇒ Object
Required Parameters
* 'reward_id'<~Integer> - reward id for the activity offer to redeem
Optional Parameters
None
Return curl i -X POST ‘reward_id=123’ api.punchtab.com/v1/activity/redeem?access_token=<access_token>
167 168 169 170 171 172 173 174 175 |
# File 'lib/punchtab/api.rb', line 167 def redeem_activity_offer(reward_id) # make the POST call path = '/activity/redeem' = {:access_token => @access_token} raw_response = Punchtab::API.post(path, {:body => "reward_id=#{reward_id}", :query => }) Punchtab::Utils.process_response(raw_response) end |
#status ⇒ Object
95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/punchtab/api.rb', line 95 def status # make the POST call path = '/auth/status' # setup the post params post_data = { :token => @access_token, :key => @access_key } raw_response = Punchtab::API.post(path, :body => post_data) Punchtab::Utils.process_response(raw_response) end |