Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Badgeville service hook #9

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions lib/services/badgeville.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
class Services::Badgeville < Services::Base

name "Badgeville"

events_allowed %w[ new_suggestion suggestion_votes_update ]

string :api_key, lambda { _("API Key") }, lambda { _('You can find your API key via Badgeville Admin Console > Develop > Home. Use the Cairo key.') }
string :site_id, lambda { _("Site ID") }, lambda { _('You can find your site ID via Badgeville Admin Console > Configure > Sites > Your Site. The ID is listed in the URL when viewing the site.') }
boolean :production, lambda { _("Production?") }, lambda { _('Integrate with the Badgeville production environment? If unchecked, sandbox will be used.') }

def perform
return false if data['api_key'].blank? || data['site_id'].blank?
host = (production) ? "api.v2.badgeville.com" : "sandbox.badgeville.com"
request = Net::HTTP::Get.new("/cairo/v1/#{data['api_key']}/sites/#{data['site_id']}/players/#{message[:bv_email]}/activities?do=create&data=#{message[:bv_data].to_json}")
http = Net::HTTP.new(host, 443)
http.use_ssl = true
response = http.request(request)
return response.is_a?(Net::HTTPSuccess)
end

def message
data = api_hash
hash = case event
when 'new_suggestion'
{
:bv_email => data['suggestion']['creator']['email'],
:bv_data => {
:verb => event,
:suggestion_id => data['suggestion']['id'],
:topic_id => data['suggestion']['topic']['id'],
:forum_id => data['suggestion']['topic']['forum']['id'],
:forum_name => data['suggestion']['topic']['forum']['name'],
:category => (data['suggestion']['category'].nil?) ? "none" : data['suggestion']['category']
}
}
when 'suggestion_votes_update'
{
:bv_email => data['suggestion']['creator']['email'],
:bv_data => {
:verb => event,
:suggestion_id => data['suggestion']['id'],
:topic_id => data['suggestion']['topic']['id'],
:forum_id => data['suggestion']['topic']['forum']['id'],
:forum_name => data['suggestion']['topic']['forum']['name'],
:category => (data['suggestion']['category'].nil?) ? "none" : data['suggestion']['category'],
:vote_count => data['suggestion']['vote_count']
}
}
else
super
end

hash
end
end