Class: Inbox::API
- Inherits:
-
Object
- Object
- Inbox::API
- Defined in:
- lib/inbox.rb
Instance Attribute Summary collapse
-
#access_token ⇒ Object
readonly
Returns the value of attribute access_token.
-
#api_server ⇒ Object
Returns the value of attribute api_server.
-
#app_id ⇒ Object
readonly
Returns the value of attribute app_id.
-
#app_secret ⇒ Object
readonly
Returns the value of attribute app_secret.
Instance Method Summary collapse
-
#_perform_account_action!(action) ⇒ Object
Billing Methods.
- #downgrade_account! ⇒ Object
-
#initialize(app_id, app_secret, access_token = nil, api_server = 'https://api.inboxapp.com') ⇒ API
constructor
A new instance of API.
-
#namespaces ⇒ Object
Convenience Methods.
- #set_access_token(token) ⇒ Object
- #token_for_code(code) ⇒ Object
- #upgrade_account! ⇒ Object
- #url_for_authentication(redirect_uri, login_hint = '', options = {}) ⇒ Object
- #url_for_path(path) ⇒ Object
Constructor Details
#initialize(app_id, app_secret, access_token = nil, api_server = 'https://api.inboxapp.com') ⇒ API
Returns a new instance of API.
47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/inbox.rb', line 47 def initialize(app_id, app_secret, access_token = nil, api_server = 'https://api.inboxapp.com') raise "When overriding the Inbox API server address, you must include https://" unless api_server.include?('://') @api_server = api_server @access_token = access_token @app_secret = app_secret @app_id = app_id ::RestClient.add_before_execution_proc do |req, params| req.add_field('X-Inbox-API-Wrapper', 'ruby') end end |
Instance Attribute Details
#access_token ⇒ Object (readonly)
Returns the value of attribute access_token.
43 44 45 |
# File 'lib/inbox.rb', line 43 def access_token @access_token end |
#api_server ⇒ Object
Returns the value of attribute api_server.
42 43 44 |
# File 'lib/inbox.rb', line 42 def api_server @api_server end |
#app_id ⇒ Object (readonly)
Returns the value of attribute app_id.
44 45 46 |
# File 'lib/inbox.rb', line 44 def app_id @app_id end |
#app_secret ⇒ Object (readonly)
Returns the value of attribute app_secret.
45 46 47 |
# File 'lib/inbox.rb', line 45 def app_secret @app_secret end |
Instance Method Details
#_perform_account_action!(action) ⇒ Object
Billing Methods
100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/inbox.rb', line 100 def _perform_account_action!(action) raise UnexpectedAccountAction.new unless action == "upgrade" || action == "downgrade" protocol, domain = @api_server.split('//') account_ids = namespaces.all.collect {|n| n.account_id }.uniq account_ids.each do | id | ::RestClient.post("#{protocol}//#{@app_secret}:@#{domain}/a/#{@app_id}/accounts/#{id}/#{action}",{}) do |response, request, result| # Throw any exceptions json = Inbox.interpret_response(result, response, :expected_class => Object) end end end |
#downgrade_account! ⇒ Object
117 118 119 |
# File 'lib/inbox.rb', line 117 def downgrade_account! _perform_account_action!('downgrade') end |
#namespaces ⇒ Object
Convenience Methods
93 94 95 96 |
# File 'lib/inbox.rb', line 93 def namespaces @namespaces ||= RestfulModelCollection.new(Namespace, self, nil) @namespaces end |
#set_access_token(token) ⇒ Object
73 74 75 |
# File 'lib/inbox.rb', line 73 def set_access_token(token) @access_token = token end |
#token_for_code(code) ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/inbox.rb', line 77 def token_for_code(code) data = { 'client_id' => app_id, 'client_secret' => app_secret, 'grant_type' => 'authorization_code', 'code' => code } ::RestClient.get("https://www.inboxapp.com/oauth/token", {:params => data}) do |response, request, result| json = Inbox.interpret_response(result, response, :expected_class => Object) return json['access_token'] end end |
#upgrade_account! ⇒ Object
113 114 115 |
# File 'lib/inbox.rb', line 113 def upgrade_account! _perform_account_action!('upgrade') end |
#url_for_authentication(redirect_uri, login_hint = '', options = {}) ⇒ Object
65 66 67 68 69 70 71 |
# File 'lib/inbox.rb', line 65 def url_for_authentication(redirect_uri, login_hint = '', = {}) trialString = 'false' if [:trial] == true trialString = 'true' end "https://www.inboxapp.com/oauth/authorize?client_id=#{@app_id}&trial=#{trialString}&response_type=code&scope=email&login_hint=#{login_hint}&redirect_uri=#{redirect_uri}" end |
#url_for_path(path) ⇒ Object
59 60 61 62 63 |
# File 'lib/inbox.rb', line 59 def url_for_path(path) raise NoAuthToken.new if @access_token == nil protocol, domain = @api_server.split('//') "#{protocol}//#{@access_token}:@#{domain}#{path}" end |