Class: Glassfrog::Client

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Utils

#extract_id, #parameterize, #symbolize_keys

Constructor Details

#initialize(attrs = {}) {|_self| ... } ⇒ Glassfrog::Client

Initializes a new Client object.

Parameters:

  • attrs={}
    Hash, String

    Either just the API key, or a Hash of options.

Yields:

  • (_self)

Yield Parameters:



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_keyString

Returns:

  • (String)


21
22
23
# File 'lib/glassfrog/client.rb', line 21

def api_key
  @api_key
end

#cache_entityTempFile (readonly)

Returns:

  • (TempFile)


29
30
31
# File 'lib/glassfrog/client.rb', line 29

def cache_entity
  @cache_entity
end

#cache_metaTempFile (readonly)

Returns:

  • (TempFile)


29
30
31
# File 'lib/glassfrog/client.rb', line 29

def cache_meta
  @cache_meta
end

#cachingBoolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/glassfrog/client.rb', line 25

def caching
  @caching
end

#caching_settingsHash

Returns:

  • (Hash)


27
28
29
# File 'lib/glassfrog/client.rb', line 27

def caching_settings
  @caching_settings
end

#httpHTTP (readonly)

Returns:

  • (HTTP)


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.

Parameters:

  • tmpdir (String)

    Path to the temporary directory.

Returns:

  • (Proc)

    Proc containing the directory deletion.



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.

Returns:

  • (Boolean)

    Whether 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.

Parameters:

  • circles=nil (Array<Glassfrog::Circle>)

    Array of circle objects (used instead of a GET request).

  • roles=nil (Array<Glassfrog::Role>)

    Array of role objects (used instead of a GET request).

Returns:



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.

Parameters:

  • type (Glassfrog::Base)

    Object type to send request to.

  • options={}
    Hash, Glassfrog::Base, Integer, String, URI

    Options to specify the ID of the object to delete.

Returns:

  • (Boolean)

    Whether the request was successful.

Raises:

  • (ArgumentError)


152
153
154
155
156
157
# File 'lib/glassfrog/client.rb', line 152

def delete(type, options)
  klass = TYPES[parameterize(type)]
  identifier = extract_id(options, 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.

Parameters:

  • circles=nil (Array<Glassfrog::Circle>)

    Array of circle objects of which the root will be found.

  • roles=nil (Array<Glassfrog::Role>)

    Array of role objects to use to find supporting role of the root circle.

Returns:



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.

Parameters:

  • type (Glassfrog::Base)

    Object type to send request to.

  • options={}
    Hash, Glassfrog::Base, Integer, String, URI

    Options to specify object(s) to fetch.

Returns:



113
114
115
116
117
# File 'lib/glassfrog/client.rb', line 113

def get(type, options={})
  klass = TYPES[parameterize(type)]
  options = parse_params(options, klass)
  klass.public_send(:get, self, options)
end

#headersHash

Gets the HTTP headers for requests.

Returns:

  • (Hash)

    The headers.



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.

Parameters:

  • type (Glassfrog::Base)

    Object type to send request to.

  • identifier=nil (Integer)

    The ID of the object to update.

  • options={}
    Hash, Glassfrog::Base

    Options to specify attribute(s) to update and/or ID.

Returns:

  • (Hash, Glassfrog::Base, Integer, String, URI, Boolean)

    The options passed if successful or false if unsuccessful.

Raises:

  • (ArgumentError)


138
139
140
141
142
143
144
# File 'lib/glassfrog/client.rb', line 138

def patch(type, identifier=nil, options)
  klass = TYPES[parameterize(type)]
  identifier = extract_id(options, klass) if identifier.nil?
  raise(ArgumentError, "No valid id found given in options") if identifier.nil?
  options = validate_options(options, klass)
  if klass.public_send(:patch, self, identifier, options) then options else false end
end

#post(type, options) ⇒ Array<Glassfrog::Base>

Sends a POST request to the corresponding object type.

Parameters:

  • type (Glassfrog::Base)

    Object type to send request to.

  • options={}
    Hash, Glassfrog::Base

    Options to specify attribute(s) of object being created.

Returns:



125
126
127
128
129
# File 'lib/glassfrog/client.rb', line 125

def post(type, options)
  klass = TYPES[parameterize(type)]
  options = validate_options(options, klass)
  klass.public_send(:post, self, options)
end