Class: Ruby::Miradore::Request

Inherits:
Object
  • Object
show all
Includes:
Finest::Builder, HTTParty
Defined in:
lib/ruby/miradore.rb

Overview

Ruby::Miradore::Request class This class is the base class for all the requests and implement the CRUD methods towards the API v1 version. The class is using the HTTParty gem to perform the requests and the Finest::Builder gem to transform the XML response into an array of Ruby::Miradore::Request objects.

Direct Known Subclasses

Device, Organization, User

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ Request

Returns a new instance of Request.

Examples:

Usage example for Ruby::Miradore::Request without arguments, it will use the default values

Ruby::Miradore::Request.new.all

Parameters:

  • args (Hash) (defaults to: {})

    The arguments to be passed to the API blank hash by default



70
71
72
73
74
75
76
77
# File 'lib/ruby/miradore.rb', line 70

def initialize(args = {})
  super args
  @subdomain ||= args.with_indifferent_access[:subdomain]
  @auth ||= args.with_indifferent_access[:auth]
  @json ||= args.to_json
  remove_instance_variable(:@subdomain) if @subdomain.nil?
  remove_instance_variable(:@auth) if @auth.nil?
end

Instance Attribute Details

#auth=(value) ⇒ Object (writeonly)

Sets the attribute auth

Parameters:

  • value

    the value to set the attribute auth to.



65
66
67
# File 'lib/ruby/miradore.rb', line 65

def auth=(value)
  @auth = value
end

#json=(value) ⇒ Object (writeonly)

Sets the attribute json

Parameters:

  • value

    the value to set the attribute json to.



65
66
67
# File 'lib/ruby/miradore.rb', line 65

def json=(value)
  @json = value
end

#subdomain=(value) ⇒ Object (writeonly)

Sets the attribute subdomain

Parameters:

  • value

    the value to set the attribute subdomain to.



65
66
67
# File 'lib/ruby/miradore.rb', line 65

def subdomain=(value)
  @subdomain = value
end

Instance Method Details

#all(args = {}) ⇒ Object

This call will perform all GET calls regardless the API version that it’s calling. The version would be determined based on the superclass name. If the superclass is Request the GET will call simple_v1 which will require the item which is class name. otherwise will call to retrieve devices based on filters.

Parameters:

  • args (Hash) (defaults to: {})

    The arguments to be passed to the API



85
86
87
88
89
90
91
# File 'lib/ruby/miradore.rb', line 85

def all(args = {})
  transform(
    Crack::XML.parse(
      http_method_v1(args).body
    ).dig('Content', 'Items'), args
  )
end

#as_json(_options = nil) ⇒ Object



101
102
103
# File 'lib/ruby/miradore.rb', line 101

def as_json(_options = nil)
  @json
end

#call(args = {}) ⇒ Object



93
94
95
96
97
98
99
# File 'lib/ruby/miradore.rb', line 93

def call(args = {})
  transform(
    Crack::XML.parse(
      http_method_v1(args.merge(attribute: 'ID')).body
    )['Content'], args
  )
end

#http_method_v1(args) ⇒ Object

Constructs the URL adding parameters to the URL based on the arguments passed and using the API v1 format. Transforms the body argument into XML format.



107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/ruby/miradore.rb', line 107

def http_method_v1(args)
  url = Miradore.url[:v1] % args.merge(
    subdomain: @subdomain || args[:subdomain],
    auth: args.fetch(:auth, @auth),
    id: args.fetch(:id, nil),
    item: self.class.name&.demodulize,
    attribute: args.fetch(:attribute, Miradore.select_attributes),
    filter: args.fetch(:filter, nil),
    options: args.fetch(:options, nil)&.to_query&.sub('&', ',')
  )
  self.class.method(args.fetch(:method, :get)).call(url, body: args.fetch(:body, nil)&.to_xml(root: 'Content', skip_types: true))
end

#transform(res, args = {}) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/ruby/miradore.rb', line 120

def transform(res, args = {})
  return res if args.fetch(:skip_transform, false) || res.is_a?(Net::HTTPClientError)

  args.merge!({ subdomain: args.fetch(:subdomain, @subdomain), auth: args.fetch(:auth, @auth) })
  if res.values.first.is_a?(Array)
    return res.values.first.map! do |e|
      self.class.new(args.merge(e.transform_keys(&:downcase)))
    end
  end
  self.class.new(args.merge(res.values.first&.transform_keys(&:downcase)))
rescue StandardError
  { error: "#{self.class.name&.demodulize}(s) not found", status: :not_found }
end