Class: StmApi::Donation
- Inherits:
-
Object
- Object
- StmApi::Donation
- Defined in:
- lib/stm_api.rb
Overview
Your code goes here…
Constant Summary collapse
- BEARER =
'LAXQszxcmpGMWi24y0NFt00YPWGJnJOo9Ba8ijLcI1fmiKHI1PDF7KG7PGJU7KcX'
Instance Attribute Summary collapse
-
#campaign_id ⇒ Object
Returns the value of attribute campaign_id.
-
#currency ⇒ Object
Returns the value of attribute currency.
-
#team_id ⇒ Object
Returns the value of attribute team_id.
-
#userhash ⇒ Object
Returns the value of attribute userhash.
Instance Method Summary collapse
- #campaigns ⇒ Object
- #donate(params = {}) ⇒ Object
- #find_one_team(id) ⇒ Object
-
#initialize(params = {}) ⇒ Donation
constructor
A new instance of Donation.
- #statistics ⇒ Object
- #user_info ⇒ Object
- #user_teams ⇒ Object
Constructor Details
#initialize(params = {}) ⇒ Donation
Returns a new instance of Donation.
16 17 18 19 20 21 |
# File 'lib/stm_api.rb', line 16 def initialize(params = {}) @userhash = params[:userhash] @currency = params[:currency] @team_id = params[:team_id] @campaign_id = campaigns.first unless params[:campaign_id] end |
Instance Attribute Details
#campaign_id ⇒ Object
Returns the value of attribute campaign_id.
13 14 15 |
# File 'lib/stm_api.rb', line 13 def campaign_id @campaign_id end |
#currency ⇒ Object
Returns the value of attribute currency.
11 12 13 |
# File 'lib/stm_api.rb', line 11 def currency @currency end |
#team_id ⇒ Object
Returns the value of attribute team_id.
12 13 14 |
# File 'lib/stm_api.rb', line 12 def team_id @team_id end |
#userhash ⇒ Object
Returns the value of attribute userhash.
10 11 12 |
# File 'lib/stm_api.rb', line 10 def userhash @userhash end |
Instance Method Details
#campaigns ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/stm_api.rb', line 48 def campaigns campaigns_raw = RestClient.get("https://api.sharethemeal.org/api/meta", content_type: :json, accept: :json, Authorization: "Bearer #{BEARER}") campaigns_json = JSON.parse(campaigns_raw) found_campaigns = [] campaigns_json["campaigns"].each do |camp, v| found_campaigns << camp end found_campaigns end |
#donate(params = {}) ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/stm_api.rb', line 70 def donate(params = {}) token_payload = { 'userHash' => @userhash, 'currency' => @currency } client_token = RestClient.post('https://api.sharethemeal.org/api/payment/braintree/client-tokens', token_payload.to_json, content_type: :json, accept: :json, Authorization: "Bearer #{BEARER}") client_token_response = JSON.parse(client_token) auth_reply = JSON.parse(Base64.decode64(client_token_response['clientToken'])) finger_print = URI.encode_www_form_component(auth_reply['authorizationFingerprint']) payment_infos = RestClient.get("https://api.braintreegateway.com/merchants/#{auth_reply['merchantId']}/client_api/v1/payment_methods?sharedCustomerIdentifierType=undefined&braintreeLibraryVersion=braintree%2Fweb%2F2.15.5&merchantAccountId=#{auth_reply['merchantAccountId']}&authorizationFingerprint=#{finger_print}&callback=") payment_infos_json = JSON.parse(payment_infos) transaction_payload = { 'userHash' => @userhash, 'amount' => params[:amount], 'currency' => @currency, 'paymentMethodNonce' => payment_infos_json['paymentMethods'].first['nonce'], 'teamId' => @team_id, 'campaignId' => @campaign_id } transaction_response = RestClient.post('https://api.sharethemeal.org/api/payment/braintree/transactions', transaction_payload.to_json, content_type: :json, accept: :json, 'Authorization' => "Bearer #{BEARER}") transaction_response_json = JSON.parse(transaction_response) if transaction_response_json['result']['donationCreated'] == true return true else return false end # rescue # return false end |
#find_one_team(id) ⇒ Object
60 61 62 63 64 65 66 67 68 |
# File 'lib/stm_api.rb', line 60 def find_one_team(id) teams = user_teams teams.each do |t| if t["teamId"] == id return t end end return false end |
#statistics ⇒ Object
31 32 33 34 35 36 37 |
# File 'lib/stm_api.rb', line 31 def statistics user_info_response = RestClient.get("https://api.sharethemeal.org/api/campaigns/zomba/status", content_type: :json, accept: :json, Authorization: "Bearer #{BEARER}") user_info_json = JSON.parse(user_info_response) return user_info_json end |
#user_info ⇒ Object
23 24 25 26 27 28 29 |
# File 'lib/stm_api.rb', line 23 def user_info user_info_response = RestClient.get("https://api.sharethemeal.org/api/users/#{@userhash}", content_type: :json, accept: :json, Authorization: "Bearer #{BEARER}") user_info_json = JSON.parse(user_info_response) return user_info_json end |
#user_teams ⇒ Object
39 40 41 42 43 44 45 46 |
# File 'lib/stm_api.rb', line 39 def user_teams team_statistic = RestClient.get("https://api.sharethemeal.org/api/users/#{@userhash}/teams", content_type: :json, accept: :json, Authorization: "Bearer #{BEARER}") team_statistic_json = JSON.parse(team_statistic) team_statistic_json["userTeams"] end |