Class: ApiQueryProvider::Provider
- Inherits:
-
Object
- Object
- ApiQueryProvider::Provider
- Defined in:
- lib/api-query-provider/provider.rb
Overview
Provides method chaining support replaces parameters in the api_path named parameters will be replaced by the assosicated value from where calls if a :where symbol is present in the string, any further values provided by where will be concatted to a key1=value,key2=value string and replaced there a value provided by limit will be replaced in to the :limit symbol if you can request additional fields or restrict the fields provided, place a :select symbol in the string
Instance Attribute Summary collapse
-
#count_constraint ⇒ Object
readonly
Returns the value of attribute count_constraint.
-
#klass ⇒ Object
readonly
Returns the value of attribute klass.
-
#select_fields ⇒ Object
readonly
Returns the value of attribute select_fields.
-
#where_constraints ⇒ Object
readonly
Returns the value of attribute where_constraints.
Class Method Summary collapse
Instance Method Summary collapse
- #execute ⇒ Object
-
#initialize(base) ⇒ Provider
constructor
A new instance of Provider.
- #limit(count) ⇒ Object
- #replace_path ⇒ Object
- #response ⇒ Object
- #select(*fields) ⇒ Object
- #where(opt = {}) ⇒ Object
Constructor Details
#initialize(base) ⇒ Provider
Returns a new instance of Provider.
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/api-query-provider/provider.rb', line 15 def initialize (base) api_url = base.api_url api_path = base.api_path if base.is_a? Class @where_constraints = {} @select_fields = [] @count_constraint = 0 @klass = base else @where_constraints = base.where_constraints @select_fields = base.select_fields @count_constraint = base.count_constraint end end |
Instance Attribute Details
#count_constraint ⇒ Object (readonly)
Returns the value of attribute count_constraint.
11 12 13 |
# File 'lib/api-query-provider/provider.rb', line 11 def count_constraint @count_constraint end |
#klass ⇒ Object (readonly)
Returns the value of attribute klass.
13 14 15 |
# File 'lib/api-query-provider/provider.rb', line 13 def klass @klass end |
#select_fields ⇒ Object (readonly)
Returns the value of attribute select_fields.
12 13 14 |
# File 'lib/api-query-provider/provider.rb', line 12 def select_fields @select_fields end |
#where_constraints ⇒ Object (readonly)
Returns the value of attribute where_constraints.
10 11 12 |
# File 'lib/api-query-provider/provider.rb', line 10 def where_constraints @where_constraints end |
Class Method Details
.interface ⇒ Object
91 92 93 |
# File 'lib/api-query-provider/provider.rb', line 91 def self.interface ApiQueryProvider::Provider.new(self) end |
.system_symbols ⇒ Object
95 96 97 |
# File 'lib/api-query-provider/provider.rb', line 95 def self.system_symbols [ :where, :count, :select ] end |
Instance Method Details
#execute ⇒ Object
83 84 85 86 87 88 89 |
# File 'lib/api-query-provider/provider.rb', line 83 def execute local_response = self.response [klass.data_selector.call(JSON.parse(local_response.body))].flatten.map do |elem| klass.new elem end end |
#limit(count) ⇒ Object
37 38 39 40 41 |
# File 'lib/api-query-provider/provider.rb', line 37 def limit(count) @count_constraint = count return self end |
#replace_path ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/api-query-provider/provider.rb', line 49 def replace_path replaced_path = klass.api_path.dup used_keys = [] @where_constraints.each do |key, value| if replaced_path.gsub! /:#{key}/, value.to_s used_keys << key end end @where_constraints.reject! { |key, value| used_keys.include? key } replaced_path.gsub! /:where/, @where_constraints.to_a.map { |e| e.join("=") }.join("&") replaced_path.gsub! /:select/, @select_fields.join(",") replaced_path.gsub! /:limit/, @count_constraint.to_s if replaced_path.include?(":") raise "you didn't replace all fields in the api_path" end replaced_path end |
#response ⇒ Object
73 74 75 76 77 78 79 80 81 |
# File 'lib/api-query-provider/provider.rb', line 73 def response begin uri = URI.join(klass.api_url, self.replace_path) rescue raise "invalid uri" end HTTParty.get(uri.to_s) end |
#select(*fields) ⇒ Object
43 44 45 46 47 |
# File 'lib/api-query-provider/provider.rb', line 43 def select(*fields) @select_fields |= fields.flatten return self end |
#where(opt = {}) ⇒ Object
31 32 33 34 35 |
# File 'lib/api-query-provider/provider.rb', line 31 def where(opt = {}) @where_constraints.merge! opt return self end |