Class: Datafiniti::Api

Inherits:
Object
  • Object
show all
Defined in:
lib/datafiniti/api.rb

Direct Known Subclasses

Listings, Locations, Products

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ Api

Returns a new instance of Api.



15
16
17
18
19
20
# File 'lib/datafiniti/api.rb', line 15

def initialize(args={})
  query = args[:query].gsub(/\bhttp\:\/\/\b/, '*')
  @format = "json"
  @conn ||= Faraday.new("https://#{ENV['DATAFINITI_API_KEY']}:@api.datafiniti.co/v2/data")
  @response = solr_query(query)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object



48
49
50
# File 'lib/datafiniti/api.rb', line 48

def method_missing(name, *args, &block)
  @response.to_h[name.to_s]
end

Instance Attribute Details

#connObject

Returns the value of attribute conn.



9
10
11
# File 'lib/datafiniti/api.rb', line 9

def conn
  @conn
end

#formatObject

Returns the value of attribute format.



8
9
10
# File 'lib/datafiniti/api.rb', line 8

def format
  @format
end

#responseObject

Returns the value of attribute response.



10
11
12
# File 'lib/datafiniti/api.rb', line 10

def response
  @response
end

#statusObject

Returns the value of attribute status.



12
13
14
# File 'lib/datafiniti/api.rb', line 12

def status
  @status
end

#tokenObject

Returns the value of attribute token.



7
8
9
# File 'lib/datafiniti/api.rb', line 7

def token
  @token
end

Instance Method Details

#default_to_csvObject



30
31
32
# File 'lib/datafiniti/api.rb', line 30

def default_to_csv
  @format = "csv"
end

#default_to_jsonObject



34
35
36
# File 'lib/datafiniti/api.rb', line 34

def default_to_json
  @format = "json"
end

#download(force = false) ⇒ Object



42
43
44
45
46
# File 'lib/datafiniti/api.rb', line 42

def download(force=false)
  query = SolrQuery.build(@query)
  force ? @dl_code = get("/v2/data/locations/download?view=location_#{@format}&q=#{query}") : @dl_code ||= get("/v2/data/locations/download?view=location_#{@format}&q=#{query}")
  return download_object
end

#previewObject



38
39
40
# File 'lib/datafiniti/api.rb', line 38

def preview
  @response
end

#recordsObject



26
27
28
# File 'lib/datafiniti/api.rb', line 26

def records
  @response.records
end

#solr_query(query) ⇒ Object



59
60
61
62
63
64
# File 'lib/datafiniti/api.rb', line 59

def solr_query(query)
  query = build_new_query(query, true)
  @response = get("/v2/data/#{data_type}/preview?view=#{data_type.singularize}_#{format}&q=#{query}")
  update_records
  return @response
end

#where(query) ⇒ Object



52
53
54
55
56
57
# File 'lib/datafiniti/api.rb', line 52

def where(query)
  query = build_new_query(query)
  @response = get("/v2/data/#{data_type}/preview?view=#{data_type.singularize}_#{format}&q=#{query}")
  update_records
  return @response
end