Class: MoxiworksPlatform::Resource
- Inherits:
-
Object
- Object
- MoxiworksPlatform::Resource
- Defined in:
- lib/moxiworks_platform/resource.rb
Overview
provides underlying logic for connecting to Moxi Works Platform over HTTPS
Class Method Summary collapse
-
.accept_header ⇒ String
formatted Accept header content.
-
.attr_accessor(*vars) ⇒ Object
keep a list of attr_accessors defined in this class.
-
.attributes ⇒ Array
all available accessors defined in this class.
-
.auth_header ⇒ String
formatted Authorization header content.
- .check_for_error_in_response(response) ⇒ Object
-
.content_type_header ⇒ String
formatted Content-Type header.
-
.headers ⇒ Hash
HTTP headers required to connect to Moxi Works Platform.
- .send_request(method, opts = {}, url = nil) ⇒ Object
Instance Method Summary collapse
-
#attributes ⇒ Array
all available accessors defined in this class.
-
#float_attrs ⇒ Object
used by method_missing to ensure that a number is the type we expect it to be this should be overridden if we have any float values we want to return as floats.
-
#initialize(hash = {}) ⇒ Resource
constructor
maps Hash values to Instance variables for mapping JSON object values to our Class attributes.
-
#int_attrs ⇒ Object
used by method_missing to ensure that a number is the type we expect it to be this should be overridden if we have any int values we want to return as ints.
- #method_missing(meth, *args, &block) ⇒ Object
-
#numeric_attrs ⇒ Object
used by method_missing to ensure that a number is the type we expect it to be.
- #numeric_value_for(attr_name, opts = {}) ⇒ Object
-
#to_hash ⇒ Hash
convert this class into a Hash structure where attributes are Hash key names and attribute values are Hash values.
Constructor Details
#initialize(hash = {}) ⇒ Resource
maps Hash values to Instance variables for mapping JSON object values to our Class attributes
80 81 82 83 84 |
# File 'lib/moxiworks_platform/resource.rb', line 80 def initialize(hash={}) hash.each do |key,val| instance_variable_set("@#{key}", val) end end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(meth, *args, &block) ⇒ Object
86 87 88 89 90 91 92 93 |
# File 'lib/moxiworks_platform/resource.rb', line 86 def method_missing(meth, *args, &block) name = meth.to_sym if numeric_attrs.include? name return numeric_value_for(name, type: :integer) if int_attrs.include? name return numeric_value_for(name, type: :float) if float_attrs.include? name end super(meth, *args, &block) end |
Class Method Details
.accept_header ⇒ String
formatted Accept header content
52 53 54 |
# File 'lib/moxiworks_platform/resource.rb', line 52 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 |
.attributes ⇒ Array
all available accessors defined in this class
22 23 24 |
# File 'lib/moxiworks_platform/resource.rb', line 22 def self.attributes @attributes end |
.auth_header ⇒ String
formatted Authorization header content
40 41 42 43 44 45 46 47 |
# File 'lib/moxiworks_platform/resource.rb', line 40 def self.auth_header raise MoxiworksPlatform::Exception::AuthorizationError, '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 |
.check_for_error_in_response(response) ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/moxiworks_platform/resource.rb', line 63 def self.check_for_error_in_response(response) begin json = JSON.parse(response) return if json.is_a?(Array) rescue => e raise MoxiworksPlatform::Exception::RemoteRequestFailure, "unable to parse remote response #{e}\n response:\n #{response}" end = "unable to perform remote action on Moxi Works platform\n" << json['messages'].join(',') unless json['messages'].nil? raise MoxiworksPlatform::Exception::RemoteRequestFailure, if not json['status'].nil? and (%w(error fail).include?(json['status'])) end |
.content_type_header ⇒ String
formatted Content-Type header
59 60 61 |
# File 'lib/moxiworks_platform/resource.rb', line 59 def self.content_type_header 'application/x-www-form-urlencoded' end |
.headers ⇒ Hash
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_header } end |
.send_request(method, opts = {}, url = nil) ⇒ Object
111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/moxiworks_platform/resource.rb', line 111 def self.send_request(method, opts={}, url=nil) RestClient::Request.execute(method: method, url: url, payload: opts, headers: self.headers) do |response| puts response if MoxiworksPlatform::Config.debug self.check_for_error_in_response(response) json = JSON.parse(response) return false if not json['status'].nil? and json['status'] =='fail' self.new(json) unless json.nil? or json.empty? end end |
Instance Method Details
#attributes ⇒ Array
all available accessors defined in this class
98 99 100 |
# File 'lib/moxiworks_platform/resource.rb', line 98 def attributes self.class.attributes + numeric_attrs end |
#float_attrs ⇒ Object
used by method_missing to ensure that a number is the type we expect it to be this should be overridden if we have any float values we want to return as floats
156 157 158 |
# File 'lib/moxiworks_platform/resource.rb', line 156 def float_attrs [] end |
#int_attrs ⇒ Object
used by method_missing to ensure that a number is the type we expect it to be this should be overridden if we have any int values we want to return as ints
150 151 152 |
# File 'lib/moxiworks_platform/resource.rb', line 150 def int_attrs [] end |
#numeric_attrs ⇒ Object
used by method_missing to ensure that a number is the type we expect it to be
144 145 146 |
# File 'lib/moxiworks_platform/resource.rb', line 144 def numeric_attrs int_attrs + float_attrs end |
#numeric_value_for(attr_name, opts = {}) ⇒ Object
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/moxiworks_platform/resource.rb', line 123 def numeric_value_for(attr_name, opts={}) val = self.instance_variable_get("@#{attr_name}") return val.to_i if val.is_a? Numeric and opts[:type] == :integer return val if val.is_a? Numeric return 0 if val.nil? or val.empty? val.gsub!(/[^[:digit:]|\.]/, '') if val.is_a? String case opts[:type] when :integer instance_variable_set("@#{attr_name}", (val.nil? or val.empty?) ? nil : val.to_i) when :float instance_variable_set("@#{attr_name}", (val.nil? or val.empty?) ? nil : val.to_f) else instance_variable_set("@#{attr_name}", nil) end self.instance_variable_get("@#{attr_name}") rescue => e puts "problem with auto conversion: #{e.} #{e.backtrace}" nil end |
#to_hash ⇒ Hash
convert this class into a Hash structure where attributes are Hash key names and attribute values are Hash values
105 106 107 108 109 |
# File 'lib/moxiworks_platform/resource.rb', line 105 def to_hash hash = {} self.attributes.each {|attr| hash[attr.to_sym] = self.send(attr)} hash end |