Skip to content

Instantly share code, notes, and snippets.

@mgroebner
Last active August 29, 2015 13:57
Show Gist options
  • Save mgroebner/9913865 to your computer and use it in GitHub Desktop.
Save mgroebner/9913865 to your computer and use it in GitHub Desktop.
Tickaroo Button Integration (Ruby)
module ApplicationHelper
def tickaroo_button(opts)
opts[:button_type] ||= :default
sig = TikButtonSignature.new.signature(opts)
s = ""
s += "width='#{opts[:width]}' " if opts[:width].present?
s += "height='#{opts[:height]}' " if opts[:height].present?
s += "lang='#{opts[:lang]}' " if opts[:lang].present?
s += "data-title='#{opts[title]}' " if opts[:title].present?
s += "data-home-team='#{opts[:home_team]}' " if opts[:home_team].present?
s += "data-away-team='#{opts[:away_team]}' " if opts[:away_team].present?
s += "data-starts-at='#{opts[:starts_at]}' " if opts[:starts_at].present?
s += "data-tournament='#{opts[:tournament]}' " if opts[:tournament].present?
s += "data-sportstype='#{opts[:sportstype]}' " if opts[:sportstype].present?
s += "data-lanme='#{opts[:lanme]}' " if opts[:lanme].present?
s += "data-laddress='#{opts[:laddress]}' " if opts[:laddress].present?
s += "data-lcity='#{opts[:lcity]}' " if opts[:lcity].present?
s += "data-lzip='#{opts[:lzip]}' " if opts[:lzip].present?
s += "data-external-id='#{opts[:external_id]}' " if opts[:external_id].present?
s += "data-auth-token='#{opts[:auth_token]}' " if opts[:auth_token].present?
s += "data-auth-signature='#{sig}'"
if opts[:button_type] == :score
"<a class='tickaroo-score-button' #{s}></a>".html_safe
else
"<a class='tickaroo-button' #{s}></a>".html_safe
end
end
end
class TikButtonSignature
VALID_PARAMS = [:title, :away_team, :home_team, :starts_at, :tournament, :sportstype, :lname, :laddress, :lcity, :lzip, :external_id, :auth_token]
CLIENT_SECRET = 'abc123'
attr_accessor :client_secret
def initialize(options = {})
@client_secret = options[:client_secret] || CLIENT_SECRET
end
def signature(params)
calculate_signature(params)
end
private
def calculate_signature(params)
encoded_parameter = params.collect{|i| [escape(i[0]).gsub('_', '-'), escape(i[1])] if VALID_PARAMS.include?(i[0])}.compact.sort
s = ""
encoded_parameter.each do |p|
s << '&' if s.present?
s << "#{p[0]}=#{p[1]}"
end
sign(client_secret, s)
end
def sign( key, base_string )
digest = OpenSSL::Digest::Digest.new( 'sha1' )
OpenSSL::HMAC.hexdigest( digest, key, base_string )
end
def escape(value)
OAuth::Helper.escape(value)
end
end
<!DOCTYPE html>
...
<%= tickaroo_button(:width => '120', :height => '32', :home_team => "FC Bayern", :away_team => "Dortmund", :starts_at => "#{(Time.now+2.weeks).to_i}", :sportstype => "soccer", :external_id => "123") %>
...
<script>(function(e,j,i,h,f,c,b){e[f]=e[f]||function(){(e[f].q=e[f].q||[]).push(arguments)},e[f].l=1*new Date();c=j.createElement(i),b=j.getElementsByTagName(i)[0];c.async=1;c.src=h;b.parentNode.insertBefore(c,b)})(window,document,"script","//widgets.tickaroo.com/widgets/tickaroo-button.js","_tbq");_tbq("load");</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment