Class: Glue

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/gluestick.rb

Constant Summary collapse

GLUE_API_VERSION =
'4.5'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeGlue

Returns a new instance of Glue.



19
20
21
22
# File 'lib/gluestick.rb', line 19

def initialize()
  @method_family = nil
  @glue_token = ""
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/gluestick.rb', line 24

def method_missing(name, *args)
  if @method_family == nil
    # The first missing_method is the first part of the method
    @method_family = name
    # Return self to chain a second method_missing
    self
  else
    # The second missing_method is the second part of the method
    # The current API format is "/v1/part_a/part_b?params"
    method = "/v2/%s/%s" % [@method_family.to_s, name.to_s]
    @method_family = nil
    # Build HTTParty options from a hash and provide auth
    glue_params = {:version => GLUE_API_VERSION}
    glue_params.merge!({:token => @glue_token}) unless @glue_token.empty?
    glue_params.merge!(args[0]) unless args[0].nil?
    options = {:query => glue_params}
    begin
      response = self.class.get(method, options)
    rescue SocketError => desc
      raise GlueError.new("Could not connect")
    end
    # A couple convenience exceptions
    raise GlueError.new("401 Unauthorized") if response.code == 401
    raise GlueError.new("404 Not Found") if response.code == 404
    raise GlueError.new("Invalid request") unless (200..299) === response.code
    # Return the response as a hash, thanks to crack
    if '/v2/user/login' == method
      @glue_token = response['adaptiveblue']['response']['ping']['token']
    end
    response
  end
end

Instance Attribute Details

#glue_tokenObject

Returns the value of attribute glue_token.



15
16
17
# File 'lib/gluestick.rb', line 15

def glue_token
  @glue_token
end