Class: MxitRails::MxitApi::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/mxit_rails/mxit_api/api_client.rb

Constant Summary collapse

MXIT_AUTH_BASE_URI =
'https://auth.mxit.com'
MXIT_AUTH_TOKEN_URI =
MXIT_AUTH_BASE_URI + '/token'
MXIT_AUTH_CODE_URI =
MXIT_AUTH_BASE_URI + '/authorize'
MXIT_API_URI =
'http://api.mxit.com'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app_name, client_id, client_secret) ⇒ Client

Returns a new instance of Client.



15
16
17
18
19
# File 'lib/mxit_rails/mxit_api/api_client.rb', line 15

def initialize(app_name, client_id, client_secret)
  @app_name = app_name
  @client_id = client_id
  @client_secret = client_secret
end

Instance Attribute Details

#app_nameObject

Returns the value of attribute app_name.



12
13
14
# File 'lib/mxit_rails/mxit_api/api_client.rb', line 12

def app_name
  @app_name
end

#auth_tokenObject

Returns the value of attribute auth_token.



13
14
15
# File 'lib/mxit_rails/mxit_api/api_client.rb', line 13

def auth_token
  @auth_token
end

#client_idObject

Returns the value of attribute client_id.



12
13
14
# File 'lib/mxit_rails/mxit_api/api_client.rb', line 12

def client_id
  @client_id
end

#client_secretObject

Returns the value of attribute client_secret.



12
13
14
# File 'lib/mxit_rails/mxit_api/api_client.rb', line 12

def client_secret
  @client_secret
end

Instance Method Details

#batch_notify_users(mxit_ids, message, contains_markup) ⇒ Object



201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/mxit_rails/mxit_api/api_client.rb', line 201

def batch_notify_users(mxit_ids, message, contains_markup)
  Rails.logger.info('Requesting MXit API auth...')
  request_app_auth(["message/send"])
  Rails.logger.info('Finished MXit API auth.')

  batch_size = 50
  Rails.logger.info('Starting to notify users in batches of ' + batch_size.to_s + '...')
  i = 0
  while i < mxit_ids.count
    current_batch = mxit_ids[i, batch_size]
    i += batch_size

    to = current_batch.join(',')
    send_message(@app_name, to, message, contains_markup)

    Rails.logger.info("Total users notified: " + current_batch.count.to_s)
  end
  Rails.logger.info('Finished notifying!')
end

#get_contact_list(filter, options = { :skip => nil, :count => nil, :auth_token => nil }) ⇒ Object

The following filter parameters are available (only one can be specified at a time):

@All - Return all roster entries
@Friends - Return only friends
@Apps - Return only applications
@Invites  - Return all entries that is in an invite state
@Connections  - Return all entries that has been accepted
@Rejected - Return all entries that has been rejected
@Pending - Return all entries that is waiting to be accepted by the other party
@Deleted - Return all entries that was deleted
@Blocked - Return all entries that was blocked


175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/mxit_rails/mxit_api/api_client.rb', line 175

def get_contact_list(filter, options={ :skip => nil, :count => nil, :auth_token => nil })
  auth_token = options[:auth_token] || @auth_token
  check_auth_token(auth_token, ["graph/read"])

  response = http_client(MXIT_API_URI + "/user/socialgraph/contactlist") do |http, path|

    parameters = { :filter => filter }
    # skip and count are optional
    parameters[:skip] = skip if options[:skip]
    parameters[:count] = count if options[:count]

    request = Net::HTTP::Get.new(path + "?#{URI.encode_www_form(parameters)}")
    set_api_headers(request, auth_token.access_token)

    http.request(request)
  end

  case response
  when Net::HTTPSuccess then
    data = JSON.parse(response.body)

  else
    raise MxitRails::MxitApi::RequestException.new(response.message, response.code)
  end
end

#refresh_token(auth_token) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/mxit_rails/mxit_api/api_client.rb', line 106

def refresh_token(auth_token)
  if auth_token.refresh_token.nil?
    raise MxitRails::MxitApi::Exception.new("The provided auth token doesn't have a refresh " +
      "token.")
  end

  response = http_client(MXIT_AUTH_TOKEN_URI) do |http, path|

    request = new_post_request(path, {
      "grant_type" => "refresh_token",
      "refresh_token" => auth_token.refresh_token
    })

    http.request(request)
  end

  case response
  when Net::HTTPSuccess then
    auth_token = AuthToken.new(JSON.parse(response.body))

  else
    raise MxitRails::MxitApi::RequestException.new(response.message, response.code)
  end
end

#request_app_auth(scopes) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/mxit_rails/mxit_api/api_client.rb', line 21

def request_app_auth(scopes)
  if scopes.empty?
    raise MxitRails::MxitApi::Exception.new("No scopes were provided.")
  end

  response = http_client(MXIT_AUTH_TOKEN_URI) do |http, path|

    request = new_post_request(path, {
      "grant_type" => "client_credentials",
      "scope" => scopes.join(" ")
    })

    http.request(request)
  end

  case response
  when Net::HTTPSuccess then
    @auth_token = AuthToken.new(JSON.parse(response.body))

  else
    raise MxitRails::MxitApi::RequestException.new(response.message, response.code)
  end
end

#request_user_auth(code, redirect_uri) ⇒ Object

NOTE: ‘user_code_request_uri` must be used before `request_user_auth` because it provides the `code` argument. `redirect_uri` must match the one used in the `user_code_request_uri` call



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/mxit_rails/mxit_api/api_client.rb', line 70

def request_user_auth(code, redirect_uri)
  response = http_client(MXIT_AUTH_TOKEN_URI) do |http, path|

    request = new_post_request(path, {
      "grant_type" => "authorization_code",
      "code" => code,
      "redirect_uri" => redirect_uri
    })

    http.request(request)
  end

  case response
  when Net::HTTPSuccess then
    @auth_token = AuthToken.new(JSON.parse(response.body))

  else
    raise MxitRails::MxitApi::RequestException.new(response.message, response.code)
  end
end

#revoke_token(auth_token) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/mxit_rails/mxit_api/api_client.rb', line 91

def revoke_token(auth_token)
  response = http_client(MXIT_AUTH_BASE_URI + "/revoke") do |http, path|

    request = new_post_request(path, {
      "token" => auth_token.access_token
    })

    http.request(request)
  end

  if response.code != '200'
    raise MxitRails::MxitApi::RequestException.new(response.message, response.code)
  end
end

#send_message(from, to, body, contains_markup, auth_token = nil) ⇒ Object

When sending as the app the ‘message/send` scope is required otherwise `message/user`



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/mxit_rails/mxit_api/api_client.rb', line 134

def send_message(from, to, body, contains_markup, auth_token=nil)
  auth_token = auth_token || @auth_token

  if from == @app_name
    check_auth_token(auth_token, ["message/send"])
  else
    check_auth_token(auth_token, ["message/user"])
  end

  response = http_client(MXIT_API_URI + "/message/send/") do |http, path|

    request = Net::HTTP::Post.new(path)
    set_api_headers(request, auth_token.access_token)

    request.body = {
      "Body" => body,
      "ContainsMarkup" => contains_markup,
      "From" => from,
      "To" => to
      # "Spool" => default(true)
      # "SpoolTimeOut" => default(60*60*24*7)
    }.to_json

    http.request(request)
  end

  if response.code != '200'
    raise MxitRails::MxitApi::RequestException.new(response.message, response.code)
  end
end

#user_code_request_uri(redirect_uri, state, scopes) ⇒ Object

The user’s response to the authorisation code request will be redirected to ‘redirect_uri`. If the request was successful there will be a `code` request parameter; otherwise `error`.

redirect_uri - absolute URI to which the user will be redirected after authorisation state - passed back to ‘redirect_uri` as a request parameter scopes - list of scopes to which access is required



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/mxit_rails/mxit_api/api_client.rb', line 51

def user_code_request_uri(redirect_uri, state, scopes)
  if scopes.empty?
    raise MxitRails::MxitApi::Exception.new("No scopes were provided.")
  end

  # build parameters
  parameters = {
    :response_type => "code",
    :client_id => @client_id,
    :redirect_uri => redirect_uri,
    :state => state,
    :scope => scopes.join(' ')
  }

  path = MXIT_AUTH_CODE_URI + "?#{URI.encode_www_form(parameters)}"
end