Class: MindMeisterClient::Requester
- Inherits:
-
Object
- Object
- MindMeisterClient::Requester
- Defined in:
- lib/mind_meister_client.rb
Constant Summary collapse
- MM_API_SCOPES =
%w( auth boundaries connections files folders ideas images maps people realtime reflection tasks test themes user)
- SERVER_ADDRESS =
'www.mindmeister.com'
Instance Attribute Summary collapse
-
#api_key ⇒ Object
Returns the value of attribute api_key.
-
#auth_token ⇒ Object
Returns the value of attribute auth_token.
-
#http_client ⇒ Object
Returns the value of attribute http_client.
Instance Method Summary collapse
-
#api_default_params ⇒ Hash
Bare minimum to send to MM API.
-
#api_scope?(ruby_method_name) ⇒ TrueClass
Detects if method name seems to be from MM API.
-
#callback(frob) ⇒ Hash
Handles callback call after user of MMC acquired frob.
- #init_http_client ⇒ Object
-
#initialize(api_key, secret_key, auth_token = nil) ⇒ Requester
constructor
A new instance of Requester.
-
#method_missing(id, *args) ⇒ Object
When calling MM API method, it is expected to be underscore separated and without the initial mm_.
-
#prepare_api_method(ruby_method_name) ⇒ String
maps_new_from_template -> mm.maps.newFromTemplate.
- #raise_auth_token_request ⇒ Object
-
#request(api_method_name, *args) ⇒ Hash
Makes the actual call to MM API.
-
#signed_query_string(params) ⇒ String
From hash, this methods creates query parameters for API call.
Constructor Details
#initialize(api_key, secret_key, auth_token = nil) ⇒ Requester
Returns a new instance of Requester.
19 20 21 22 23 24 25 |
# File 'lib/mind_meister_client.rb', line 19 def initialize api_key, secret_key, auth_token = nil @api_key = api_key @auth_token = auth_token @secret_key = secret_key init_http_client end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(id, *args) ⇒ Object
When calling MM API method, it is expected to be underscore separated and without the initial mm_
45 46 47 48 49 50 51 52 |
# File 'lib/mind_meister_client.rb', line 45 def method_missing id, *args if api_scope? id api_method_name = prepare_api_method id request api_method_name, *args else super end end |
Instance Attribute Details
#api_key ⇒ Object
Returns the value of attribute api_key.
15 16 17 |
# File 'lib/mind_meister_client.rb', line 15 def api_key @api_key end |
#auth_token ⇒ Object
Returns the value of attribute auth_token.
16 17 18 |
# File 'lib/mind_meister_client.rb', line 16 def auth_token @auth_token end |
#http_client ⇒ Object
Returns the value of attribute http_client.
17 18 19 |
# File 'lib/mind_meister_client.rb', line 17 def http_client @http_client end |
Instance Method Details
#api_default_params ⇒ Hash
Bare minimum to send to MM API
117 118 119 120 121 122 |
# File 'lib/mind_meister_client.rb', line 117 def api_default_params { api_key: @api_key, auth_token: @auth_token, response_format: 'json' } end |
#api_scope?(ruby_method_name) ⇒ TrueClass
Detects if method name seems to be from MM API
59 60 61 62 63 |
# File 'lib/mind_meister_client.rb', line 59 def api_scope? ruby_method_name ruby_method_scope = ruby_method_name.to_s.split('_')[0] ruby_method_scope && MM_API_SCOPES.include?(ruby_method_scope) end |
#callback(frob) ⇒ Hash
Handles callback call after user of MMC acquired frob
37 38 39 40 |
# File 'lib/mind_meister_client.rb', line 37 def callback frob # Calling MM API to get actual auth_token auth_get_token frob: frob end |
#init_http_client ⇒ Object
27 28 29 30 |
# File 'lib/mind_meister_client.rb', line 27 def init_http_client @http_client = Net::HTTP.new SERVER_ADDRESS, 443 @http_client.use_ssl = true end |
#prepare_api_method(ruby_method_name) ⇒ String
maps_new_from_template -> mm.maps.newFromTemplate
This is helper for method_missing method of this class
130 131 132 133 134 135 136 137 138 139 |
# File 'lib/mind_meister_client.rb', line 130 def prepare_api_method ruby_method_name rmn_parts = ruby_method_name.to_s.split('_') 'mm.%s.%s%s'%[ rmn_parts[0], rmn_parts[1], rmn_parts[2..rmn_parts.length].map(&:capitalize).join('') ] end |
#raise_auth_token_request ⇒ Object
101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/mind_meister_client.rb', line 101 def raise_auth_token_request api_call_params = { api_key: @api_key, method: 'mm.auth.getToken', perms: 'read' } raise ApiCallRequiredError.new 'Authentication token missing', 'https://%s/services/auth/?%s'%[ SERVER_ADDRESS, signed_query_string(api_call_params) ] end |
#request(api_method_name, *args) ⇒ Hash
Makes the actual call to MM API
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/mind_meister_client.rb', line 74 def request api_method_name, *args unless @auth_token || api_method_name =~ /auth/ raise_auth_token_request end if args[0] api_call_params = args[0].merge(api_default_params) else api_call_params = api_default_params end api_call_params[:method] = api_method_name api_data = JSON.parse http_client.get('/services/rest?' + signed_query_string(api_call_params)).body, symbolize_names: true if api_data[:rsp][:stat] == 'fail' raise RequestError.new api_data[:rsp][:err][:code].to_i, api_data[:rsp][:err][:msg], api_call_params else api_data[:rsp] end end |
#signed_query_string(params) ⇒ String
From hash, this methods creates query parameters for API call. This method also appends signature, required by
most of MindMeister API calls.
More about signing MM API calls at www.mindmeister.com/developers/authentication
149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
# File 'lib/mind_meister_client.rb', line 149 def signed_query_string params query_params_joined = params.sort.inject('') { |memo, key| memo += key[0].to_s + key[1].to_s memo } signature_data = @secret_key + query_params_joined query_string_params = URI.encode_www_form(params) signature = Digest::MD5.hexdigest(signature_data) query_string_params + '&api_sig=' + signature end |