Class: MxitApi
- Inherits:
-
Object
- Object
- MxitApi
- Defined in:
- lib/mxit_api/profile.rb,
lib/mxit_api/user_id.rb,
lib/mxit_api/version.rb,
lib/mxit_api/initialize.rb,
lib/mxit_api/send_invite.rb,
lib/mxit_api/send_message.rb
Defined Under Namespace
Modules: Version
Instance Attribute Summary collapse
-
#access_token ⇒ Object
readonly
Returns the value of attribute access_token.
-
#app_name ⇒ Object
readonly
Returns the value of attribute app_name.
-
#expire_at ⇒ Object
readonly
Returns the value of attribute expire_at.
-
#refresh_token ⇒ Object
readonly
Returns the value of attribute refresh_token.
-
#scope ⇒ Object
readonly
Returns the value of attribute scope.
-
#token_type ⇒ Object
readonly
Returns the value of attribute token_type.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(client_id, client_secret, form_data = {grant_type: 'client_credentials', scope: 'message/send'}) ⇒ MxitApi
constructor
A new instance of MxitApi.
- #profile ⇒ Object
- #send_invite(contact) ⇒ Object
- #send_message(params) ⇒ Object
- #user_id ⇒ Object
Constructor Details
#initialize(client_id, client_secret, form_data = {grant_type: 'client_credentials', scope: 'message/send'}) ⇒ MxitApi
Returns a new instance of MxitApi.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/mxit_api/initialize.rb', line 5 def initialize(client_id, client_secret, form_data = {grant_type: 'client_credentials', scope: 'message/send'}) if form_data.kind_of?(Hash) url = URI.parse('https://auth.mxit.com/token') req = Net::HTTP::Post.new(url.path, 'Accept'=>'application/json') req.set_form_data(form_data) req.basic_auth(client_id,client_secret) http = Net::HTTP.new(url.host, url.port) http.use_ssl = true response = http.request(req) if response.code == '200' data = ActiveSupport::JSON.decode(response.body) @access_token = data['access_token'] @token_type = data['token_type'] @refresh_token = data['refresh_token'] @scope = data['scope'] @expire_at = data['expires_in'].to_i.seconds.from_now end end end |
Instance Attribute Details
#access_token ⇒ Object (readonly)
Returns the value of attribute access_token.
3 4 5 |
# File 'lib/mxit_api/initialize.rb', line 3 def access_token @access_token end |
#app_name ⇒ Object (readonly)
Returns the value of attribute app_name.
3 4 5 |
# File 'lib/mxit_api/initialize.rb', line 3 def app_name @app_name end |
#expire_at ⇒ Object (readonly)
Returns the value of attribute expire_at.
3 4 5 |
# File 'lib/mxit_api/initialize.rb', line 3 def expire_at @expire_at end |
#refresh_token ⇒ Object (readonly)
Returns the value of attribute refresh_token.
3 4 5 |
# File 'lib/mxit_api/initialize.rb', line 3 def refresh_token @refresh_token end |
#scope ⇒ Object (readonly)
Returns the value of attribute scope.
3 4 5 |
# File 'lib/mxit_api/initialize.rb', line 3 def scope @scope end |
#token_type ⇒ Object (readonly)
Returns the value of attribute token_type.
3 4 5 |
# File 'lib/mxit_api/initialize.rb', line 3 def token_type @token_type end |
Class Method Details
.connect(*args) ⇒ Object
25 26 27 28 |
# File 'lib/mxit_api/initialize.rb', line 25 def self.connect(*args) connection = new(*args) connection.access_token ? connection : nil end |
Instance Method Details
#profile ⇒ Object
3 4 5 6 7 8 9 10 11 12 13 14 15 |
# File 'lib/mxit_api/profile.rb', line 3 def profile profile = {} url = URI.parse('https://api.mxit.com/user/profile') req = Net::HTTP::Get.new(url.path, 'Authorization' => "#{token_type} #{access_token}", 'Accept'=>'application/json') http = Net::HTTP.new(url.host, url.port) http.use_ssl = true response = http.request(req) if response.code == '200' data = ActiveSupport::JSON.decode(response.body) profile = Hash[data.map {|k, v| [k.underscore, v] }] end HashWithIndifferentAccess.new(profile) end |
#send_invite(contact) ⇒ Object
3 4 5 6 7 8 9 |
# File 'lib/mxit_api/send_invite.rb', line 3 def send_invite(contact) url = URI.parse("https://api.mxit.com/user/socialgraph/contact/#{contact}") req = Net::HTTP::Put.new(url.path, 'Authorization' => "#{token_type} #{access_token}", 'Accept'=>'application/json') http = Net::HTTP.new(url.host, url.port) http.use_ssl = true http.request(req) end |
#send_message(params) ⇒ Object
3 4 5 6 7 8 9 10 11 12 13 14 15 |
# File 'lib/mxit_api/send_message.rb', line 3 def (params) params = Hash[params.map {|k, v| [k.to_s.camelize, v] }] params.reverse_merge!('ContainsMarkup' => 'true', 'From' => app_name) url = URI.parse('https://api.mxit.com/message/send/') req = Net::HTTP::Post.new(url.path, 'Authorization' => "#{token_type} #{access_token}", 'Accept'=>'application/json', 'Content-Type' =>'application/json') req.body = params.to_json http = Net::HTTP.new(url.host, url.port) http.use_ssl = true http.request(req) end |
#user_id ⇒ Object
2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
# File 'lib/mxit_api/user_id.rb', line 2 def user_id url = URI.parse('https://auth.mxit.com/userinfo?schema=openid') req = Net::HTTP::Get.new('/userinfo?schema=openid', 'Authorization' => "#{token_type} #{access_token}", 'Accept'=>'application/json', 'Content-Type' =>'application/json') http = Net::HTTP.new(url.host, url.port) http.use_ssl = true response = http.request(req) if response.code == '200' data = ActiveSupport::JSON.decode(response.body) data['sub'] end end |