Class: HashblueController
- Inherits:
-
ApplicationController
- Object
- ApplicationController
- HashblueController
- Defined in:
- app/controllers/hashblue_controller.rb
Overview
O2 Labs has exposed the power of #blue to developers via a simple REST & JSON based API, enabling new ways for users to manage their texts and add combine the ubiquity of SMS with their applications, users simply grant an application access to their messages stream or just certain messages.
Juan de Bravo ([email protected]) Ruby sensei at The Lab (thelab.o2.com)
Constant Summary collapse
- DEFAULT_PARAMS =
parameters required in both OAuth steps
{ :client_id => Rails.application.config.hashblue.client_id }
Instance Method Summary collapse
-
#code ⇒ Object
OAuth step2: retrieve the code from HashBlue, ask for a valid access token and forward to the user defined uri.
-
#endpoint ⇒ Object
HashBlue endpoint.
-
#index ⇒ Object
OAuth step1: redirect to HashBlue endpoint with the application idenfitifer and the redirect uri.
- #redirect_uri ⇒ Object
-
#rest ⇒ Object
rest client to HashBlue endpoint.
Instance Method Details
#code ⇒ Object
OAuth step2: retrieve the code from HashBlue, ask for a valid access token and forward to the user defined uri
34 35 36 37 38 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 69 70 71 |
# File 'app/controllers/hashblue_controller.rb', line 34 def code # This code is retrieved from HashBlue code = params[:code] hb_params = { :redirect_uri => redirect_uri, :client_secret => Rails.application.config.hashblue.client_secret, :grant_type => "authorization_code", :code => code } hb_params.merge!(DEFAULT_PARAMS) response = rest.post( "/oauth/access_token?".concat(hb_params.collect { |k, v| "#{k}=#{v.to_s}" }.join("&")), nil) if response.code.eql?("200") response = ActiveSupport::JSON.decode(response.body) if response.has_key?("access_token") Rails.application.config.hashblue.forward_action.nil? and raise RuntimeError, "Invalid forward_action value" url = Rails.application.config.hashblue.forward_action.split("#") url.length == 2 or raise RuntimeError, "Invalid forward_action value" redirect_to ({ :controller => url.first, :action => url.last, :access_token => response["access_token"], :expires_in => response["expires_in"], :refresh_token => response["refresh_token"] }) elsif response.has_key?("error") logger.error "Error while accessing to HashBlue #{response['error']}" raise RuntimeError, response['error'] end else logger.error "Error #{response.code} while accessing to HashBlue #{response.body}" raise RuntimeError, "Unable to access to hashblue" end end |
#endpoint ⇒ Object
HashBlue endpoint
82 83 84 |
# File 'app/controllers/hashblue_controller.rb', line 82 def endpoint Rails.application.config.hashblue.uri end |
#index ⇒ Object
OAuth step1: redirect to HashBlue endpoint with the application idenfitifer and the redirect uri
23 24 25 26 27 28 29 30 |
# File 'app/controllers/hashblue_controller.rb', line 23 def index hb_params = { :redirect_uri => redirect_uri } hb_params.merge!(DEFAULT_PARAMS) redirect_to "#{endpoint}/oauth/authorize?".concat(hb_params.collect { |k, v| "#{k}=#{v.to_s}" }.join("&")) end |
#redirect_uri ⇒ Object
86 87 88 |
# File 'app/controllers/hashblue_controller.rb', line 86 def redirect_uri "#{request.protocol}#{request.host_with_port}/hashblue/code" end |
#rest ⇒ Object
rest client to HashBlue endpoint
74 75 76 77 78 79 |
# File 'app/controllers/hashblue_controller.rb', line 74 def rest @@rest ||= (uri = URI.parse(endpoint) rest = Net::HTTP.new(uri.host, uri.port) rest.use_ssl = true rest) end |