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
Direct Known Subclasses
ActionLog, Agent, BuyerTransaction, Company, Contact, EmailCampaign, Event, Group, Listing, Office, SellerTransaction, Task
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
- .underscore(attr) ⇒ Object
- .underscore_array(array) ⇒ Object
- .underscore_attribute_names(thing) ⇒ Object
- .underscore_hash(hash) ⇒ 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.
- #init_attrs_from_hash(hash = {}) ⇒ Object
-
#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
155 156 157 |
# File 'lib/moxiworks_platform/resource.rb', line 155 def initialize(hash={}) self.init_attrs_from_hash(hash) end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(meth, *args, &block) ⇒ Object
184 185 186 187 188 189 190 191 |
# File 'lib/moxiworks_platform/resource.rb', line 184 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
56 57 58 |
# File 'lib/moxiworks_platform/resource.rb', line 56 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
16 17 18 19 20 |
# File 'lib/moxiworks_platform/resource.rb', line 16 def self.attr_accessor(*vars) @attributes ||= [] @attributes.concat vars super(*vars) end |
.attributes ⇒ Array
all available accessors defined in this class
25 26 27 |
# File 'lib/moxiworks_platform/resource.rb', line 25 def self.attributes @attributes end |
.auth_header ⇒ String
formatted Authorization header content
44 45 46 47 48 49 50 51 |
# File 'lib/moxiworks_platform/resource.rb', line 44 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
67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/moxiworks_platform/resource.rb', line 67 def self.check_for_error_in_response(response) begin json = JSON.parse(response) MoxiworksPlatform::Session.instance. = response.headers[:set_cookie].first rescue nil 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
63 64 65 |
# File 'lib/moxiworks_platform/resource.rb', line 63 def self.content_type_header 'application/x-www-form-urlencoded' end |
.headers ⇒ Hash
HTTP headers required to connect to Moxi Works Platform
32 33 34 35 36 37 38 39 |
# File 'lib/moxiworks_platform/resource.rb', line 32 def self.headers { Authorization: auth_header, Accept: accept_header, 'Content-Type' => content_type_header, Cookie: Session.instance. } end |
.send_request(method, opts = {}, url = nil) ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/moxiworks_platform/resource.rb', line 82 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 |
.underscore(attr) ⇒ Object
143 144 145 146 147 148 149 |
# File 'lib/moxiworks_platform/resource.rb', line 143 def self.underscore(attr) attr.gsub(/::/, '/'). gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2'). gsub(/([a-z\d])([A-Z])/,'\1_\2'). tr("-", "_"). downcase end |
.underscore_array(array) ⇒ Object
126 127 128 129 130 131 132 |
# File 'lib/moxiworks_platform/resource.rb', line 126 def self.underscore_array(array) new_array = [] array.each do |member| new_array << self.underscore_attribute_names(member) end new_array end |
.underscore_attribute_names(thing) ⇒ Object
114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/moxiworks_platform/resource.rb', line 114 def self.underscore_attribute_names(thing) case thing when Array new_thing = self.underscore_array(thing) when Hash new_thing = self.underscore_hash(thing) else new_thing = thing end new_thing end |
.underscore_hash(hash) ⇒ Object
134 135 136 137 138 139 140 141 |
# File 'lib/moxiworks_platform/resource.rb', line 134 def self.underscore_hash(hash) hash.keys.each do |key| hash[key] = self.underscore_attribute_names hash[key] underscored = Resource.underscore(key) hash[underscored] = hash.delete(key) end hash end |
Instance Method Details
#attributes ⇒ Array
all available accessors defined in this class
162 163 164 |
# File 'lib/moxiworks_platform/resource.rb', line 162 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
168 169 170 |
# File 'lib/moxiworks_platform/resource.rb', line 168 def float_attrs [] end |
#init_attrs_from_hash(hash = {}) ⇒ Object
178 179 180 181 182 |
# File 'lib/moxiworks_platform/resource.rb', line 178 def init_attrs_from_hash(hash={}) hash.each do |key,val| instance_variable_set("@#{key}", val) end 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
174 175 176 |
# File 'lib/moxiworks_platform/resource.rb', line 174 def int_attrs [] end |
#numeric_attrs ⇒ Object
used by method_missing to ensure that a number is the type we expect it to be
194 195 196 |
# File 'lib/moxiworks_platform/resource.rb', line 194 def numeric_attrs int_attrs + float_attrs end |
#numeric_value_for(attr_name, opts = {}) ⇒ Object
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/moxiworks_platform/resource.rb', line 94 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 nil 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
201 202 203 204 205 |
# File 'lib/moxiworks_platform/resource.rb', line 201 def to_hash hash = {} self.attributes.each {|attr| hash[attr.to_sym] = self.send(attr)} hash end |