Class: Xn::XnApiSession
- Inherits:
-
Object
- Object
- Xn::XnApiSession
- Defined in:
- lib/xn.rb
Instance Attribute Summary collapse
-
#api ⇒ Object
Returns the value of attribute api.
-
#api_suffix ⇒ Object
readonly
Could make these configurable via xn.yml file in the future:.
-
#login_path ⇒ Object
readonly
Could make these configurable via xn.yml file in the future:.
-
#url ⇒ Object
readonly
Could make these configurable via xn.yml file in the future:.
-
#user ⇒ Object
readonly
Could make these configurable via xn.yml file in the future:.
Instance Method Summary collapse
-
#create_vertex(model, props) ⇒ Object
Create a vertex of given model with given properties return a hash of the vertex or nil.
-
#exec_action(vertex, action_name, props = {}) ⇒ Object
execute the named action with any properties as args.
-
#find_or_create_by_model_and_name(model, name, filter_string = nil, props = {}) ⇒ Object
find or create a vertex by model and name and optionally provide a filter string to append to the model/xxx/ URL and properties to set if not found.
-
#find_vertex_by_model(model, filter_string) ⇒ Object
Fetch a single vertex of given model type from the server return a hash of the vertex or nil.
-
#find_vertex_by_part(part, filter_string) ⇒ Object
Fetch a single vertex of given part type from the server return a hash of the vertex or nil.
-
#initialize(server_url, user_email, api_suffix = "v1") ⇒ XnApiSession
constructor
A new instance of XnApiSession.
-
#login ⇒ Object
Login to the host and prompt for password.
-
#password ⇒ Object
Get a user’s password from the command line don’t cache me…
-
#related_vertices(vertex) ⇒ Object
return all vertices related to the given one.
- #token ⇒ Object
-
#update_vertex(vertex, update_hash) ⇒ Object
update the given vertex with the given hash return a hash of the updates or nil.
Constructor Details
#initialize(server_url, user_email, api_suffix = "v1") ⇒ XnApiSession
Returns a new instance of XnApiSession.
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/xn.rb', line 33 def initialize(server_url, user_email, api_suffix = "v1") @url = URI(server_url) @login_path = '/sessions.json' @user = user_email @api_suffix = api_suffix @api = ApiRequestor.new url if ENV['LMTOKEN'] @token = ENV['LMTOKEN'] puts "Using token from env LMTOKEN" else @token = token end @api.token = @token end |
Instance Attribute Details
#api ⇒ Object
Returns the value of attribute api.
31 32 33 |
# File 'lib/xn.rb', line 31 def api @api end |
#api_suffix ⇒ Object (readonly)
Could make these configurable via xn.yml file in the future:
30 31 32 |
# File 'lib/xn.rb', line 30 def api_suffix @api_suffix end |
#login_path ⇒ Object (readonly)
Could make these configurable via xn.yml file in the future:
30 31 32 |
# File 'lib/xn.rb', line 30 def login_path @login_path end |
#url ⇒ Object (readonly)
Could make these configurable via xn.yml file in the future:
30 31 32 |
# File 'lib/xn.rb', line 30 def url @url end |
#user ⇒ Object (readonly)
Could make these configurable via xn.yml file in the future:
30 31 32 |
# File 'lib/xn.rb', line 30 def user @user end |
Instance Method Details
#create_vertex(model, props) ⇒ Object
Create a vertex of given model with given properties return a hash of the vertex or nil
113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/xn.rb', line 113 def create_vertex(model, props) debug "create_vertex(#{model}, #{props})" raise "model and :name property are required" if model.nil? or props.nil? or props[:name].nil? request_path = "/#{api_suffix}/model/#{model.downcase}" api.put request_path, props do |response| if response and response[0] != false return vertex = response[2] end end end |
#exec_action(vertex, action_name, props = {}) ⇒ Object
execute the named action with any properties as args
179 180 181 182 183 184 185 186 187 |
# File 'lib/xn.rb', line 179 def exec_action(vertex, action_name, props = {}) debug "exec_action(#{vertex}, #{action_name}, #{props})" request_path = "/#{api_suffix}/model/#{vertex['meta']['model_name']}/#{vertex['id']}/action/#{action_name}" debug "POST #{request_path} (#{props.to_json})" api.post request_path, props do |response| debug "executed #{action_name} on vertex [#{response}]" response end end |
#find_or_create_by_model_and_name(model, name, filter_string = nil, props = {}) ⇒ Object
find or create a vertex by model and name and optionally provide a filter string to append to the model/xxx/ URL and properties to set if not found.
(example filter_string: ‘filter/name/?name=myname’)
147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/xn.rb', line 147 def find_or_create_by_model_and_name(model, name, filter_string = nil, props = {}) debug "find_or_create_by_model_and_name(#{model}, #{name}, #{filter_string}, #{props})" filter_string = "filter/name/?name[value]=#{name}" unless filter_string props = props.merge name: name obj = find_vertex_by_model(model, filter_string) if obj.nil? or obj.empty? debug "about to create a #{model} vertex with #{props} properties" if obj.nil? or obj.empty? obj = create_vertex(model, props) end obj end |
#find_vertex_by_model(model, filter_string) ⇒ Object
Fetch a single vertex of given model type from the server return a hash of the vertex or nil
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/xn.rb', line 76 def find_vertex_by_model(model, filter_string) debug "find_vertex_by_model(#{model.downcase}, #{filter_string})" raise "model is a required argument" if model.nil? if filter_string and filter_string[/\?/] filter_string = "#{filter_string}&limit=1" else filter_string = "#{filter_string}?limit=1" end request_path = "/#{api_suffix}/model/#{model.downcase}/#{filter_string}" debug "GET #{request_path}" api.get request_path do |response| vertex = response.first debug "found vertex [#{vertex}]" vertex end end |
#find_vertex_by_part(part, filter_string) ⇒ Object
Fetch a single vertex of given part type from the server return a hash of the vertex or nil
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/xn.rb', line 95 def find_vertex_by_part(part, filter_string) debug "find_vertex_by_part(#{part}, #{filter_string})" raise "part is a required argument" if part.nil? if filter_string and filter_string[/\?/] filer_string = "#{filter_string}&limit=1" else filter_string = "#{filter_string}?limit=1" end request_path = "/#{api_suffix}/is/#{part.downcase}/#{filter_string}" api.get request_path do |response| vertex = response.first debug "found vertex [#{vertex}]" vertex end end |
#login ⇒ Object
Login to the host and prompt for password. return the token
63 64 65 66 67 68 |
# File 'lib/xn.rb', line 63 def login request_hash = { user: { email: user, password: password } } api.post login_path, request_hash do |response| response['token'] end end |
#password ⇒ Object
Get a user’s password from the command line don’t cache me…
51 52 53 54 55 56 57 58 59 |
# File 'lib/xn.rb', line 51 def password if RUBY_PLATFORM == 'java' console = System.console pass = console.read_password("Password: ") java.lang.String.new(pass) else pass = ask("Password: ") { |q| q.echo = "*" } end end |
#related_vertices(vertex) ⇒ Object
return all vertices related to the given one
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 |
# File 'lib/xn.rb', line 160 def (vertex) debug "related_vertices(#{vertex})" if vertex and vertex.is_a? Hash and vertex['meta']['model_name'] model = vertex['meta']['model_name'] request_path = "/#{api_suffix}/model/#{model}/#{vertex['id']}/rel" api.get request_path do |parts| debug " parts: #{parts}" = parts.map do |part| find_vertex_by_model model, "#{vertex['id']}/rel/#{part}" end.compact.reject do |response| response[:status] == 404 # No need to tell us what doesn't exist! end debug "found related: [#{}]" return if .any? end end end |
#token ⇒ Object
70 71 72 |
# File 'lib/xn.rb', line 70 def token @token ||= login end |
#update_vertex(vertex, update_hash) ⇒ Object
update the given vertex with the given hash return a hash of the updates or nil
127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/xn.rb', line 127 def update_vertex(vertex, update_hash) debug "update_vertex(#{vertex}, #{update_hash})" return nil if vertex.nil? or update_hash.nil? if !vertex.is_a? Hash vertex = find_vertex_by_part :record, vertex end request_path = "/#{api_suffix}/model/#{vertex['meta']['model_name']}/#{vertex['id']}" debug "PATCH #{request_path} (#{update_hash.to_json})" api.patch request_path, update_hash do |response| debug "updated vertex [#{response}]" response end end |