Class: Datafiniti::Api
- Inherits:
-
Object
show all
- Defined in:
- lib/datafiniti/api.rb
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
#conn ⇒ Object
Returns the value of attribute conn.
9
10
11
|
# File 'lib/datafiniti/api.rb', line 9
def conn
@conn
end
|
Returns the value of attribute format.
8
9
10
|
# File 'lib/datafiniti/api.rb', line 8
def format
@format
end
|
#response ⇒ Object
Returns the value of attribute response.
10
11
12
|
# File 'lib/datafiniti/api.rb', line 10
def response
@response
end
|
#status ⇒ Object
Returns the value of attribute status.
12
13
14
|
# File 'lib/datafiniti/api.rb', line 12
def status
@status
end
|
#token ⇒ Object
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_csv ⇒ Object
30
31
32
|
# File 'lib/datafiniti/api.rb', line 30
def default_to_csv
@format = "csv"
end
|
#default_to_json ⇒ Object
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
|
#preview ⇒ Object
38
39
40
|
# File 'lib/datafiniti/api.rb', line 38
def preview
@response
end
|
#records ⇒ Object
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
|