vestorly_api 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 19465edda2fedfc554025d76141293240c906024
4
+ data.tar.gz: ef795cee4d5b01e3f0ee1f4df32237fd35778c4b
5
+ SHA512:
6
+ metadata.gz: 74ffe20691f94828cc7c0e8838172b55a279e0ac8931bb3c23a20a41dedf801f52c29e0cf4d4bec4f3f4fccfeebe15ec7235a1907436535d9614721bdd2d6349
7
+ data.tar.gz: 396a7063aac7bb2ce13ca7dec354d25a81268ebce11880846c71cddffd54e42c632db76239236a7cb5e173fab3b7b0304e497a0170bc3b3c9dadff9642b75c1e
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format documentation
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in vestorly_api.gemspec
4
+ gemspec
5
+
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 David Rodas
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,151 @@
1
+ # VestorlyApi
2
+
3
+ The Vestorly API provides the ability for external developers to synchronize their client data with Vestorly.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'vestorly_api'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install vestorly_api
18
+
19
+ ## Running the tests
20
+
21
+ ```bash
22
+ rspec
23
+ ```
24
+
25
+ ## Basic Setup
26
+
27
+ To setup the client, you'll need the following configuration.
28
+
29
+ Rails example
30
+
31
+ ```ruby
32
+ VestorlyApi.configure do |config|
33
+ config.api_uri = ENV['VESTORLY_API_URI']
34
+ end
35
+ ```
36
+
37
+ ### SignIn to Vestorly API
38
+
39
+ Sign in will return the authentication token on success, and if the sign_in is invalid, it will raise `VestorlyApi::Exceptions::InvalidSignInCredentials`
40
+
41
+ **Example**: Authentication to the API
42
+
43
+ ```ruby
44
+ sign_in_api = VestorlyApi::SignIn.new('my@user.com', 'password')
45
+
46
+ begin
47
+ authentication_token = sign_in_api.sign_in # vestorly-auth
48
+ rescue VestorlyApi::Exceptions::InvalidSignInCredentials
49
+ # Do rescue stuff...
50
+ end
51
+ ```
52
+
53
+ `VestorlyApi::SignIn` also provides the following public methods usable after the sign_in was successful:
54
+
55
+ ```ruby
56
+ sign_in_api.authentication_token # vestorly-auth
57
+ sign_in_api.advisor_id # advisor id
58
+ ```
59
+
60
+ ### Using the advisor API object
61
+
62
+ Once logged in with a valid sign_in object, it can be passed to the `VestorlyApi::Advisor` to request on the advisor API part:
63
+
64
+ **Example**: Obtain the list of prospective clients for the logged in advisor
65
+
66
+ ```ruby
67
+ sign_in_api = VestorlyApi::SignIn.new('my@user.com', 'password')
68
+ sign_in_api.sign_in
69
+
70
+ advisor_api = VestorlyApi::Advisor.new(sign_in_api)
71
+ advisor_user_entries = advisor_api.advisor_user_entries
72
+
73
+ # with query params
74
+ query_params_hash = { 'filter_by' => 'prospects' }
75
+ advisor_user_entries = advisor_api.advisor_user_entries( query_params_hash )
76
+ ```
77
+
78
+ **Example**: Return a list of active advisor accounts
79
+
80
+
81
+ ```ruby
82
+ sign_in_api = VestorlyApi::SignIn.new('my@user.com', 'password')
83
+ sign_in_api.sign_in
84
+
85
+ advisor_api = VestorlyApi::Advisor.new(sign_in_api)
86
+ advisor_posts = advisor_api.advisor_posts
87
+
88
+ # with query params
89
+ query_params_hash = { 'filter_by' => 'prospects' }
90
+ advisor_posts = advisor_api.advisor_posts(query_params_hash)
91
+ ```
92
+
93
+ **Example**: Creating a new reader for the advisor
94
+
95
+ ```ruby
96
+ require 'vestorly_api'
97
+
98
+ sign_in_api = VestorlyApi::SignIn.new('my@user.com', 'password')
99
+ sign_in_api.sign_in
100
+
101
+ advisor_api = VestorlyApi::AdvisorBase.new(sign_in_api)
102
+
103
+ new_member_params = {
104
+ email: "lovecraft@gmail.com",
105
+ first_name: "Love",
106
+ group_id: "541c9e06c3b5a0bdda000001",
107
+ last_name: "Craft"
108
+ }
109
+
110
+ new_member = advisor_api.create_member( new_member_params )
111
+
112
+ ```
113
+
114
+ **NB**: The examples above asume the sign_in was successful
115
+
116
+
117
+ ### Sign out of Vestorly API
118
+
119
+ To sign out of the Vestorly API we can use the `VestorlyApi::SigOut` object
120
+
121
+ **Example**: The sign out can be call in the 2 following forms
122
+
123
+ ```ruby
124
+ # Creating an object and calling sign_out method
125
+ response = VestorlyApi::SignOut.new(authentication_token).sign_out
126
+
127
+ # Calling sig_out method on the class
128
+ response = VestorlyApi::SignOut.sign_out(authentication_token)
129
+
130
+ # response is a hash containing code and message keys
131
+ p response # { code: 202, message: "Successfully logged out." }
132
+
133
+ # with invalid authentication token
134
+ p response # { code: 404, message: "Not signed in. }
135
+ ```
136
+
137
+ ## Dependencies
138
+
139
+ ### Ruby
140
+
141
+ ```bash
142
+ ruby 2.1.1p76
143
+ ```
144
+
145
+ ## Contributing
146
+
147
+ 1. Fork it ( http://github.com/<my-github-username>/vestorly_api/fork )
148
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
149
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
150
+ 4. Push to the branch (`git push origin my-new-feature`)
151
+ 5. Create new Pull Request
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,30 @@
1
+ require 'httparty'
2
+
3
+ require 'vestorly_api/version'
4
+ require 'vestorly_api/exceptions'
5
+ require 'vestorly_api/default_endpoint'
6
+
7
+ require 'vestorly_api/response_utils'
8
+
9
+ require 'vestorly_api/sign_in'
10
+ require 'vestorly_api/sign_out'
11
+
12
+ require 'vestorly_api/advisor_base'
13
+ require 'vestorly_api/member'
14
+
15
+ module VestorlyApi
16
+ class << self
17
+ attr_accessor :config
18
+ end
19
+
20
+ def self.configure
21
+ self.config ||= Configuration.new
22
+ yield config
23
+ raise VestorlyApi::Exceptions::InvalidURIError if (config.api_uri =~ URI::regexp).nil?
24
+ config.api_uri << '/' unless config.api_uri[-1, 1] == '/'
25
+ end
26
+
27
+ class Configuration
28
+ attr_accessor :api_uri
29
+ end
30
+ end
@@ -0,0 +1,74 @@
1
+ module VestorlyApi
2
+ class AdvisorBase
3
+
4
+ include HTTParty
5
+ extend DefaultEndpoint
6
+ extend ResponseUtils
7
+
8
+ def initialize(authenticated_sign_in)
9
+ @authenticated_sign_in = authenticated_sign_in
10
+ @query_params
11
+ end
12
+
13
+ def self.advisor_api_endpoint
14
+ "#{AdvisorBase.default_api_endpoint}/advisors"
15
+ end
16
+
17
+ def action_api_endpoint(request_action)
18
+ "#{AdvisorBase.advisor_api_endpoint}/#{@authenticated_sign_in.advisor_id}/#{request_action}"
19
+ end
20
+
21
+ def members(query_params={})
22
+ members_response = get_request('advisor_user_entries.json', query_params)
23
+ return members_response['members'] if members_response.key?('members')
24
+ return members_response
25
+ end
26
+
27
+ def advisor_posts(query_params={})
28
+ get_request('posts.json', query_params)
29
+ end
30
+
31
+ def create_member(post_params={})
32
+ post_request('members.json', { member: post_params })
33
+ return new_member['member'] if new_member.response.code == '201'
34
+ return new_member
35
+ end
36
+
37
+ protected
38
+
39
+ # TODO: refactor this to a query object
40
+ def post_request(request_action, query_params={})
41
+ AdvisorBase.post( action_api_endpoint(request_action), :query => request_query_params(query_params))
42
+ end
43
+
44
+ def get_request(request_action, query_params={})
45
+ AdvisorBase.get( action_api_endpoint(request_action), :query => request_query_params(query_params))
46
+ end
47
+
48
+ def put_request(request_action, query_params={})
49
+ AdvisorBase.put( action_api_endpoint(request_action), :query => request_query_params(query_params))
50
+ end
51
+
52
+ def delete_request(request_action, query_params={})
53
+ AdvisorBase.put( action_api_endpoint(request_action), :query => request_query_params(query_params))
54
+ end
55
+
56
+ def request_query_params(query_params={})
57
+ default_query_params.merge(query_params)
58
+ end
59
+
60
+ def default_query_params
61
+ {
62
+ "vestorly-auth" => @authenticated_sign_in.authentication_token
63
+ }
64
+ end
65
+
66
+ def query_params_to_string(query_params_hash)
67
+ query_params_string = ""
68
+ query_params_hash.each do |key, value|
69
+ query_params_string += "&#{key}=#{value}"
70
+ end
71
+ query_params_string
72
+ end
73
+ end
74
+ end
@@ -0,0 +1,18 @@
1
+ module VestorlyApi
2
+ module DefaultEndpoint
3
+
4
+ extend self
5
+
6
+ def base_api_uri
7
+ VestorlyApi.config.api_uri
8
+ end
9
+
10
+ def default_api_endpoint(version=self.api_version)
11
+ "#{base_api_uri}api/v#{version}"
12
+ end
13
+
14
+ def api_version
15
+ 1
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,10 @@
1
+ module VestorlyApi
2
+ module Exceptions
3
+
4
+ class Error < StandardError; end
5
+ class AuthenticationError < Error; end
6
+ class InvalidSignInCredentials < Error; end
7
+ class InvalidURIError < Error; end
8
+
9
+ end
10
+ end
@@ -0,0 +1,20 @@
1
+ module VestorlyApi
2
+ class Member < AdvisorBase
3
+
4
+ DEFAULT_ACTION = 'members.json'.freeze
5
+ FETCH_DEFAULT_ACTION = 'advisor_user_entries.json'.freeze
6
+
7
+ def fetch(qury_params={})
8
+ response = get_request(FETCH_DEFAULT_ACTION, qury_params)
9
+ if Member.ok_response?(response)
10
+ response['members']
11
+ else
12
+ raise VestorlyApi::Exceptions::Error
13
+ end
14
+ end
15
+
16
+ def create(member_params={})
17
+
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,19 @@
1
+ module VestorlyApi
2
+ module ResponseUtils
3
+
4
+ extend self
5
+
6
+ def ok_response?(response)
7
+ response_status_code(response).between?(200, 201)
8
+ end
9
+
10
+ def response_status_code(response)
11
+ response.code
12
+ end
13
+
14
+ def response_status_message(response)
15
+ response.message
16
+ end
17
+
18
+ end
19
+ end
@@ -0,0 +1,56 @@
1
+ module VestorlyApi
2
+ class SignIn
3
+
4
+ include HTTParty
5
+ extend DefaultEndpoint
6
+
7
+ def initialize(username, password)
8
+ @username = username
9
+ @password = password
10
+ @authentication_token = nil
11
+ @sign_in_response = nil
12
+ end
13
+
14
+ def sign_in
15
+ @sign_in_response = SignIn.post( SignIn.sign_in_api_endpoint, query: default_query_params )
16
+ if ok_response?
17
+ authentication_token
18
+ else
19
+ raise VestorlyApi::Exceptions::InvalidSignInCredentials
20
+ end
21
+ end
22
+
23
+ def authentication_token
24
+ @sign_in_response["vestorly-auth"]
25
+ end
26
+
27
+ def self.sign_in_api_endpoint
28
+ "#{SignIn.default_api_endpoint}/session_management/sign_in?version=#{SignIn.api_version}"
29
+ end
30
+
31
+ def advisor_id
32
+ @sign_in_response["advisor_id"]
33
+ end
34
+
35
+ private
36
+
37
+ def default_query_params
38
+ {
39
+ :username => @username,
40
+ :password => @password
41
+ }
42
+ end
43
+
44
+ def ok_response?
45
+ response_status_code.between?(200, 201)
46
+ end
47
+
48
+ def response_status_code
49
+ @sign_in_response.code
50
+ end
51
+
52
+ def response_status_message
53
+ @sign_in_response.message
54
+ end
55
+ end
56
+ end