Class: Glassfrog::Client
- Inherits:
-
Object
- Object
- Glassfrog::Client
- Includes:
- Utils
- Defined in:
- lib/glassfrog/client.rb
Overview
Encapsulates HTTP/GlassFrog message sending.
Constant Summary collapse
- CACHE =
'glassfrog-cache-'
- TYPES =
{ action: Glassfrog::Action, checklist_item: Glassfrog::ChecklistItem, circle: Glassfrog::Circle, metric: Glassfrog::Metric, person: Glassfrog::Person, project: Glassfrog::Project, role: Glassfrog::Role, trigger: Glassfrog::Trigger }
- ASSOCIATED_PARAMS =
{ Glassfrog::Role => { Glassfrog::Circle => [:circle_id, :id], Glassfrog::Person => [:person_id, :id] }, Glassfrog::Person => { Glassfrog::Circle => [:circle_id, :id], Glassfrog::Role => [:role, :name] }, Glassfrog::Project => { Glassfrog::Circle => [:circle_id, :id], Glassfrog::Person => [:person_id, :id] }, Glassfrog::Metric => { Glassfrog::Circle => [:circle_id, :id], Glassfrog::Role => [:role_id, :id] }, Glassfrog::ChecklistItem => { Glassfrog::Circle => [:circle_id, :id] }, Glassfrog::Action => { Glassfrog::Person => [:person_id, :id], Glassfrog::Circle => [:circle_id, :id] }, Glassfrog::Trigger => { Glassfrog::Person => [:person_id, :id], Glassfrog::Circle => [:circle_id, :id] } }
Instance Attribute Summary collapse
- #api_key ⇒ String
- #cache_entity ⇒ TempFile readonly
- #cache_meta ⇒ TempFile readonly
- #caching ⇒ Boolean
- #caching_settings ⇒ Hash
- #http ⇒ HTTP readonly
Class Method Summary collapse
-
.finalize(tmpdir) ⇒ Proc
Garbage collection finalizer for the cache directory; if a cache directory was created it will be deleted with the Client object.
Instance Method Summary collapse
-
#api_key? ⇒ Boolean
Checks if there is an API Key.
-
#build_hierarchy(circles = nil, roles = nil) ⇒ Glassfrog::Circle
Builds the organization’s circle hierarchy.
-
#delete(type, options) ⇒ Boolean
Sends a DELETE request to the corresponding object type.
-
#find_root(circles = nil, roles = nil) ⇒ Glassfrog::Circle
Find the root circle of an array of circles.
-
#get(type, options = {}) ⇒ Array<Glassfrog::Base>
Sends a GET request to the corresponding object type.
-
#headers ⇒ Hash
Gets the HTTP headers for requests.
-
#initialize(attrs = {}) {|_self| ... } ⇒ Glassfrog::Client
constructor
Initializes a new Client object.
-
#patch(type, identifier = nil, options) ⇒ Hash, ...
Sends a PATCH request to the corresponding object type.
-
#post(type, options) ⇒ Array<Glassfrog::Base>
Sends a POST request to the corresponding object type.
Methods included from Utils
#extract_id, #parameterize, #symbolize_keys
Constructor Details
#initialize(attrs = {}) {|_self| ... } ⇒ Glassfrog::Client
Initializes a new Client object.
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/glassfrog/client.rb', line 89 def initialize(attrs={}) if attrs.class == String @api_key = attrs elsif attrs.class == Hash attrs.each do |key, value| instance_variable_set("@#{key}", value) end else raise(ArgumentError, 'Invalid Arguements. Must be String or Hash.') end yield(self) if block_given? @caching ||= nil @caching = @caching || (@caching.nil? && @caching_settings) tmpdir = @caching ? setup_cache : nil ObjectSpace.define_finalizer(self, self.class.finalize(tmpdir)) if tmpdir @http = @caching ? HTTP.cache({ metastore: @cache_meta, entitystore: @cache_entity }) : HTTP end |
Instance Attribute Details
#api_key ⇒ String
21 22 23 |
# File 'lib/glassfrog/client.rb', line 21 def api_key @api_key end |
#cache_entity ⇒ TempFile (readonly)
29 30 31 |
# File 'lib/glassfrog/client.rb', line 29 def cache_entity @cache_entity end |
#cache_meta ⇒ TempFile (readonly)
29 30 31 |
# File 'lib/glassfrog/client.rb', line 29 def @cache_meta end |
#caching ⇒ Boolean
25 26 27 |
# File 'lib/glassfrog/client.rb', line 25 def caching @caching end |
#caching_settings ⇒ Hash
27 28 29 |
# File 'lib/glassfrog/client.rb', line 27 def caching_settings @caching_settings end |
#http ⇒ HTTP (readonly)
23 24 25 |
# File 'lib/glassfrog/client.rb', line 23 def http @http end |
Class Method Details
.finalize(tmpdir) ⇒ Proc
Garbage collection finalizer for the cache directory; if a cache directory was created it will be deleted with the Client object.
220 221 222 |
# File 'lib/glassfrog/client.rb', line 220 def self.finalize(tmpdir) proc { FileUtils.remove_entry(tmpdir) } end |
Instance Method Details
#api_key? ⇒ Boolean
Checks if there is an API Key.
193 194 195 |
# File 'lib/glassfrog/client.rb', line 193 def api_key? !!(api_key) end |
#build_hierarchy(circles = nil, roles = nil) ⇒ Glassfrog::Circle
Builds the organization’s circle hierarchy.
165 166 167 |
# File 'lib/glassfrog/client.rb', line 165 def build_hierarchy(circles=nil, roles=nil) Glassfrog::Graph.hierarchy(self, circles, roles) end |
#delete(type, options) ⇒ Boolean
Sends a DELETE request to the corresponding object type.
152 153 154 155 156 157 |
# File 'lib/glassfrog/client.rb', line 152 def delete(type, ) klass = TYPES[parameterize(type)] identifier = extract_id(, klass) raise(ArgumentError, "No valid id found given in options") unless identifier if klass.public_send(:delete, self, { id: identifier }) then true else false end end |
#find_root(circles = nil, roles = nil) ⇒ Glassfrog::Circle
Find the root circle of an array of circles.
175 176 177 178 179 |
# File 'lib/glassfrog/client.rb', line 175 def find_root(circles=nil, roles=nil) circles ||= self.get :circles roles ||= self.get :roles Glassfrog::Graph.root(circles, roles) end |
#get(type, options = {}) ⇒ Array<Glassfrog::Base>
Sends a GET request to the corresponding object type.
113 114 115 116 117 |
# File 'lib/glassfrog/client.rb', line 113 def get(type, ={}) klass = TYPES[parameterize(type)] = parse_params(, klass) klass.public_send(:get, self, ) end |
#headers ⇒ Hash
Gets the HTTP headers for requests.
185 186 187 |
# File 'lib/glassfrog/client.rb', line 185 def headers { 'X-Auth-Token' => self.api_key } end |
#patch(type, identifier = nil, options) ⇒ Hash, ...
Sends a PATCH request to the corresponding object type.
138 139 140 141 142 143 144 |
# File 'lib/glassfrog/client.rb', line 138 def patch(type, identifier=nil, ) klass = TYPES[parameterize(type)] identifier = extract_id(, klass) if identifier.nil? raise(ArgumentError, "No valid id found given in options") if identifier.nil? = (, klass) if klass.public_send(:patch, self, identifier, ) then else false end end |
#post(type, options) ⇒ Array<Glassfrog::Base>
Sends a POST request to the corresponding object type.
125 126 127 128 129 |
# File 'lib/glassfrog/client.rb', line 125 def post(type, ) klass = TYPES[parameterize(type)] = (, klass) klass.public_send(:post, self, ) end |