Class: LDA::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/lda_gov/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config = LDA.configuration) ⇒ Client

Returns a new instance of Client.



10
11
12
# File 'lib/lda_gov/client.rb', line 10

def initialize(config = LDA.configuration)
  @config = config
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



8
9
10
# File 'lib/lda_gov/client.rb', line 8

def config
  @config
end

Instance Method Details

#all(path, params = {}) ⇒ Object



43
44
45
46
47
# File 'lib/lda_gov/client.rb', line 43

def all(path, params = {})
  records = []
  paginate(path, params) { |response| records.concat(response.results) }
  records
end

#clientsObject



63
64
65
# File 'lib/lda_gov/client.rb', line 63

def clients
  @clients ||= Resources::Clients.new(self)
end

#connectionObject



14
15
16
# File 'lib/lda_gov/client.rb', line 14

def connection
  @connection ||= build_connection
end

#constantsObject



71
72
73
# File 'lib/lda_gov/client.rb', line 71

def constants
  @constants ||= Resources::Constants.new(self)
end

#contributionsObject



55
56
57
# File 'lib/lda_gov/client.rb', line 55

def contributions
  @contributions ||= Resources::Contributions.new(self)
end

#filingsObject

Resource accessors



51
52
53
# File 'lib/lda_gov/client.rb', line 51

def filings
  @filings ||= Resources::Filings.new(self)
end

#get(path, params = {}) ⇒ Object



18
19
20
21
22
23
24
25
# File 'lib/lda_gov/client.rb', line 18

def get(path, params = {})
  response = connection.get(path, compact_params(params))
  handle_response(response)
rescue Faraday::TimeoutError => e
  raise ConnectionError, "Request timed out: #{e.message}"
rescue Faraday::ConnectionFailed => e
  raise ConnectionError, "Connection failed: #{e.message}"
end

#inspectObject



75
76
77
# File 'lib/lda_gov/client.rb', line 75

def inspect
  "#<#{self.class} base_url=#{config.base_url.inspect} timeout=#{config.timeout}>"
end

#lobbyistsObject



67
68
69
# File 'lib/lda_gov/client.rb', line 67

def lobbyists
  @lobbyists ||= Resources::Lobbyists.new(self)
end

#paginate(path, params = {}) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/lda_gov/client.rb', line 27

def paginate(path, params = {})
  page = 1
  iterations = 0
  loop do
    iterations += 1
    response = get(path, params.merge(page: page))
    yield response
    break if response.empty?
    break unless response.next_page?

    page = response.next_page_number
    break if page.nil?
    break if iterations >= config.max_pages
  end
end

#registrantsObject



59
60
61
# File 'lib/lda_gov/client.rb', line 59

def registrants
  @registrants ||= Resources::Registrants.new(self)
end