Class: WhosGotDirt::Request
- Inherits:
-
Object
- Object
- WhosGotDirt::Request
- Defined in:
- lib/whos_got_dirt/request.rb
Overview
Accepts MQL parameters and return URLs to request.
Direct Known Subclasses
WhosGotDirt::Requests::Entity::CorpWatch, WhosGotDirt::Requests::Entity::LittleSis, WhosGotDirt::Requests::Entity::OpenCorporates, WhosGotDirt::Requests::Entity::OpenDuka, WhosGotDirt::Requests::Entity::Poderopedia, WhosGotDirt::Requests::List::LittleSis, WhosGotDirt::Requests::List::OpenCorporates, WhosGotDirt::Requests::Relation::OpenCorporates, WhosGotDirt::Requests::Relation::OpenOil
Class Attribute Summary collapse
-
.base_url ⇒ Object
readonly
Returns the value of attribute base_url.
Instance Attribute Summary collapse
-
#:output(: output) ⇒ Hash
The API-specific parameters.
-
#input ⇒ Hash
The MQL parameters.
-
#output ⇒ Object
readonly
Returns the value of attribute output.
Class Method Summary collapse
-
.to_query(params) ⇒ String
Transforms a query string from a hash to a string.
Instance Method Summary collapse
-
#all_of(target, source, opts = {}) ⇒ Hash
Helper method to map a parameter that supports MQL
AND
-like constraints. - #and_operator ⇒ Object abstract
-
#base_url ⇒ String
Returns the base URL to be used in the request.
-
#date_range(target, source, opts = {}) ⇒ Hash
Helper method to map a date parameter that supports comparisons.
-
#equal(target, source, opts = {}) ⇒ Hash
Helper method to map a parameter that supports the MQL equality operator.
-
#initialize(input = {}) ⇒ Request
constructor
Sets the MQL parameters.
-
#match(target, source, opts = {}) ⇒ Hash
Helper method to map a parameter that supports the MQL
~=
operator. -
#one_of(target, source, opts = {}) ⇒ Hash
Helper method to map a parameter that supports the MQL
|=
operator. - #or_operator ⇒ Object abstract
- #to_s ⇒ Object abstract
Constructor Details
#initialize(input = {}) ⇒ Request
Sets the MQL parameters.
45 46 47 48 |
# File 'lib/whos_got_dirt/request.rb', line 45 def initialize(input = {}) @input = ActiveSupport::HashWithIndifferentAccess.new(input) @output = {} end |
Class Attribute Details
.base_url ⇒ Object (readonly)
Returns the value of attribute base_url.
21 22 23 |
# File 'lib/whos_got_dirt/request.rb', line 21 def base_url @base_url end |
Instance Attribute Details
#:output(: output) ⇒ Hash
Returns the API-specific parameters.
40 |
# File 'lib/whos_got_dirt/request.rb', line 40 attr_reader :output |
#input ⇒ Hash
Returns the MQL parameters.
36 37 38 |
# File 'lib/whos_got_dirt/request.rb', line 36 def input @input end |
#output ⇒ Object (readonly)
Returns the value of attribute output.
40 41 42 |
# File 'lib/whos_got_dirt/request.rb', line 40 def output @output end |
Class Method Details
.to_query(params) ⇒ String
Transforms a query string from a hash to a string.
27 28 29 30 31 |
# File 'lib/whos_got_dirt/request.rb', line 27 def to_query(params) params.map do |key,value| "#{CGI.escape(key.to_s)}=#{CGI.escape(value.to_s)}" end * '&' end |
Instance Method Details
#all_of(target, source, opts = {}) ⇒ Hash
Helper method to map a parameter that supports MQL AND
-like constraints.
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/whos_got_dirt/request.rb', line 134 def all_of(target, source, opts = {}) params = parameters(opts) if params[source] equal(target, source, opts) else values = [] params.each do |key,value| if key[/:([a-z_]+)$/, 1] == source values << value end end if values.empty? if opts.key?(:backup) send(opts[:backup], target, source, opts) end else output[target] = values.map{|v| transform(v, opts)}.join(and_operator) end end output end |
#and_operator ⇒ Object
Subclass and override #and_operator to return the "AND" operator's serialization
186 187 188 |
# File 'lib/whos_got_dirt/request.rb', line 186 def and_operator raise NotImplementedError end |
#base_url ⇒ String
Returns the base URL to be used in the request.
53 54 55 |
# File 'lib/whos_got_dirt/request.rb', line 53 def base_url self.class.base_url end |
#date_range(target, source, opts = {}) ⇒ Hash
Helper method to map a date parameter that supports comparisons.
165 166 167 168 169 170 171 172 173 174 175 176 |
# File 'lib/whos_got_dirt/request.rb', line 165 def date_range(target, source, opts = {}) params = parameters(opts) # @note OpenCorporates date range format. if params[source] output[target] = "#{params[source]}:#{params[source]}" elsif params["#{source}>="] || params["#{source}>"] || params["#{source}<="] || params["#{source}<"] output[target] = "#{params["#{source}>="] || params["#{source}>"]}:#{params["#{source}<="] || params["#{source}<"]}" end output end |
#equal(target, source, opts = {}) ⇒ Hash
Helper method to map a parameter that supports the MQL equality operator.
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/whos_got_dirt/request.rb', line 67 def equal(target, source, opts = {}) params = parameters(opts) if opts.key?(:valid) if opts[:valid].include?(params[source]) output[target] = transform(params[source], opts) end else if params[source] output[target] = transform(params[source], opts) elsif opts[:default] output[target] = opts[:default] end end output end |
#match(target, source, opts = {}) ⇒ Hash
Helper method to map a parameter that supports the MQL ~=
operator.
93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/whos_got_dirt/request.rb', line 93 def match(target, source, opts = {}) params = parameters(opts) if params[source] equal(target, source, opts) elsif params["#{source}~="] output[target] = transform(params["#{source}~="], opts) end output end |
#one_of(target, source, opts = {}) ⇒ Hash
Helper method to map a parameter that supports the MQL |=
operator.
113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/whos_got_dirt/request.rb', line 113 def one_of(target, source, opts = {}) params = parameters(opts) if params[source] equal(target, source, opts) elsif params["#{source}|="] output[target] = params["#{source}|="].map{|v| transform(v, opts)}.join(or_operator) end output end |
#or_operator ⇒ Object
Subclass and override #or_operator to return the "OR" operator's serialization
192 193 194 |
# File 'lib/whos_got_dirt/request.rb', line 192 def or_operator raise NotImplementedError end |
#to_s ⇒ Object
Subclass and override #to_s to return the URL from which to
GET
the results
180 181 182 |
# File 'lib/whos_got_dirt/request.rb', line 180 def to_s raise NotImplementedError end |