Class: MoxiworksPlatform::Resource

Inherits:
Object
  • Object
show all
Defined in:
lib/moxiworks_platform/resource.rb

Overview

provides underlying logic for connecting to Moxi Works Platform over HTTPS

Direct Known Subclasses

Contact

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash = {}) ⇒ Resource

maps Hash values to Instance variables for mapping JSON object values to our Class attributes


64
65
66
67
68
# File 'lib/moxiworks_platform/resource.rb', line 64

def initialize(hash={})
  hash.each do |key,val|
    instance_variable_set("@#{key}", val)
  end
end

Class Method Details

.accept_headerString

formatted Accept header content


51
52
53
# File 'lib/moxiworks_platform/resource.rb', line 51

def self.accept_header
  'application/vnd.moxi-platform+json;version=1'
end

.attr_accessor(*vars) ⇒ Object

keep a list of attr_accessors defined in this class

used to convert Resource child object into parameters required for saving in Moxi Works Platform


13
14
15
16
17
# File 'lib/moxiworks_platform/resource.rb', line 13

def self.attr_accessor(*vars)
  @attributes ||= []
  @attributes.concat vars
  super(*vars)
end

.attributesArray

all available accessors defined in this class


22
23
24
# File 'lib/moxiworks_platform/resource.rb', line 22

def self.attributes
  @attributes
end

.auth_headerString

formatted Authorization header content


40
41
42
43
44
45
46
# File 'lib/moxiworks_platform/resource.rb', line 40

def self.auth_header
  raise 'MoxiworksPlatform::Credentials must be set before using' unless
      MoxiworksPlatform::Credentials.set?
  identifier = MoxiworksPlatform::Credentials.platform_identifier
  secret = MoxiworksPlatform::Credentials.platform_secret
  'Basic ' + Base64.encode64("#{identifier}:#{secret}").gsub(/\n/, '')
end

.content_typeString

formatted Content-Type header


58
59
60
# File 'lib/moxiworks_platform/resource.rb', line 58

def self.content_type
  'application/x-www-form-urlencoded'
end

.headersHash

HTTP headers required to connect to Moxi Works Platform


29
30
31
32
33
34
35
# File 'lib/moxiworks_platform/resource.rb', line 29

def self.headers
  {
      Authorization: auth_header,
      Accept: accept_header,
      'Content-Type' =>  content_type
  }
end

Instance Method Details

#attributesArray

all available accessors defined in this class


73
74
75
# File 'lib/moxiworks_platform/resource.rb', line 73

def attributes
  self.class.attributes
end

#to_hashHash

convert this class into a Hash structure where attributes are Hash key names and attribute values are Hash values


80
81
82
83
84
# File 'lib/moxiworks_platform/resource.rb', line 80

def to_hash
  hash = {}
  self.attributes.each {|attr| hash[attr.to_sym] = self.send(attr)}
  hash
end