whos_got_dirt 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (79) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +6 -0
  3. data/.rspec +2 -0
  4. data/.travis.yml +20 -0
  5. data/.yardopts +3 -0
  6. data/Gemfile +7 -0
  7. data/LICENSE +20 -0
  8. data/README.md +61 -0
  9. data/Rakefile +69 -0
  10. data/lib/whos_got_dirt/renderer.rb +66 -0
  11. data/lib/whos_got_dirt/request.rb +218 -0
  12. data/lib/whos_got_dirt/requests/entity/corp_watch.rb +123 -0
  13. data/lib/whos_got_dirt/requests/entity/little_sis.rb +52 -0
  14. data/lib/whos_got_dirt/requests/entity/open_corporates.rb +104 -0
  15. data/lib/whos_got_dirt/requests/entity/open_duka.rb +33 -0
  16. data/lib/whos_got_dirt/requests/entity/poderopedia.rb +48 -0
  17. data/lib/whos_got_dirt/requests/entity.rb +54 -0
  18. data/lib/whos_got_dirt/requests/list/little_sis.rb +37 -0
  19. data/lib/whos_got_dirt/requests/list/open_corporates.rb +36 -0
  20. data/lib/whos_got_dirt/requests/list.rb +16 -0
  21. data/lib/whos_got_dirt/requests/relation/open_corporates.rb +72 -0
  22. data/lib/whos_got_dirt/requests/relation/open_oil.rb +49 -0
  23. data/lib/whos_got_dirt/requests/relation.rb +51 -0
  24. data/lib/whos_got_dirt/response.rb +79 -0
  25. data/lib/whos_got_dirt/responses/entity/corp_watch.rb +72 -0
  26. data/lib/whos_got_dirt/responses/entity/little_sis.rb +66 -0
  27. data/lib/whos_got_dirt/responses/entity/open_corporates.rb +61 -0
  28. data/lib/whos_got_dirt/responses/entity/open_duka.rb +42 -0
  29. data/lib/whos_got_dirt/responses/entity/poderopedia.rb +44 -0
  30. data/lib/whos_got_dirt/responses/entity.rb +7 -0
  31. data/lib/whos_got_dirt/responses/helpers/little_sis.rb +34 -0
  32. data/lib/whos_got_dirt/responses/helpers/open_corporates.rb +28 -0
  33. data/lib/whos_got_dirt/responses/list/little_sis.rb +42 -0
  34. data/lib/whos_got_dirt/responses/list/open_corporates.rb +34 -0
  35. data/lib/whos_got_dirt/responses/list.rb +7 -0
  36. data/lib/whos_got_dirt/responses/relation/open_corporates.rb +66 -0
  37. data/lib/whos_got_dirt/responses/relation/open_oil.rb +70 -0
  38. data/lib/whos_got_dirt/responses/relation.rb +7 -0
  39. data/lib/whos_got_dirt/result.rb +106 -0
  40. data/lib/whos_got_dirt/validator.rb +52 -0
  41. data/lib/whos_got_dirt/version.rb +3 -0
  42. data/lib/whos_got_dirt.rb +43 -0
  43. data/schemas/popolo.json +1619 -0
  44. data/spec/cassettes/corp_watch_entity.yml +35 -0
  45. data/spec/cassettes/little_sis_entity.yml +98 -0
  46. data/spec/cassettes/little_sis_list.yml +159 -0
  47. data/spec/cassettes/open_corporates_entity.yml +137 -0
  48. data/spec/cassettes/open_corporates_list.yml +60 -0
  49. data/spec/cassettes/open_corporates_relation.yml +121 -0
  50. data/spec/cassettes/open_duka_entity.yml +1149 -0
  51. data/spec/cassettes/open_oil_relation.yml +640 -0
  52. data/spec/cassettes/poderopedia_entity.yml +43 -0
  53. data/spec/renderer_spec.rb +48 -0
  54. data/spec/request_spec.rb +205 -0
  55. data/spec/requests/entity/corp_watch_spec.rb +99 -0
  56. data/spec/requests/entity/little_sis_spec.rb +33 -0
  57. data/spec/requests/entity/open_corporates_spec.rb +81 -0
  58. data/spec/requests/entity/open_duka_spec.rb +21 -0
  59. data/spec/requests/entity/poderopedia_spec.rb +21 -0
  60. data/spec/requests/list/little_sis_spec.rb +25 -0
  61. data/spec/requests/list/open_corporates_spec.rb +33 -0
  62. data/spec/requests/relation/open_corporates_spec.rb +53 -0
  63. data/spec/requests/relation/open_oil_spec.rb +37 -0
  64. data/spec/response_spec.rb +119 -0
  65. data/spec/responses/entity/corp_watch_spec.rb +31 -0
  66. data/spec/responses/entity/little_sis_spec.rb +31 -0
  67. data/spec/responses/entity/open_corporates_spec.rb +31 -0
  68. data/spec/responses/entity/open_duka_spec.rb +25 -0
  69. data/spec/responses/entity/poderopedia_spec.rb +25 -0
  70. data/spec/responses/list/little_sis_spec.rb +31 -0
  71. data/spec/responses/list/open_corporates_spec.rb +31 -0
  72. data/spec/responses/relation/open_corporates_spec.rb +31 -0
  73. data/spec/responses/relation/open_oil_spec.rb +31 -0
  74. data/spec/result_spec.rb +90 -0
  75. data/spec/spec_helper.rb +21 -0
  76. data/spec/support/shared_examples_for_requests.rb +155 -0
  77. data/spec/validator_spec.rb +43 -0
  78. data/whos_got_dirt.gemspec +28 -0
  79. metadata +281 -0
@@ -0,0 +1,104 @@
1
+ module WhosGotDirt
2
+ module Requests
3
+ module Entity
4
+ # Requests for companies from the OpenCorporates API.
5
+ #
6
+ # OpenCorporates' `fields` and `order` parameters are not supported.
7
+ #
8
+ # @example Supply an API key.
9
+ # "open_corporates_api_key": "..."
10
+ #
11
+ # @example Find companies by name prefix.
12
+ # "name~=" "ACME*"
13
+ #
14
+ # @example Find companies by jurisdiction code.
15
+ # "jurisdiction_code": "gb"
16
+ #
17
+ # @example Find companies with one of many jurisdiction codes.
18
+ # "jurisdiction_code|=": ["gb", "ie"]
19
+ #
20
+ # @example Find companies by country code.
21
+ # "country_code": "gb"
22
+ #
23
+ # @example Find companies with one of many country codes.
24
+ # "country_code|=": ["gb", "ie"]
25
+ #
26
+ # @example Find companies by status.
27
+ # "current_status": "Dissolved"
28
+ #
29
+ # @example Find companies by industry code.
30
+ # "industry_code": "be_nace_2008-66191"
31
+ #
32
+ # @example Find companies with all the given country codes.
33
+ # "a:industry_code": "be_nace_2008-66191",
34
+ # "b:industry_code": "be_nace_2008-66199"
35
+ #
36
+ # @example Find companies with one of many country codes.
37
+ # "industry_code|=": ["be_nace_2008-66191", "be_nace_2008-66199"]
38
+ #
39
+ # @example Find active companies (`true` for inactive).
40
+ # "inactive": false
41
+ #
42
+ # @example Find branch companies (`false` for non-branch).
43
+ # "branch": true
44
+ #
45
+ # @example Find nonprofit companies (`false` for others).
46
+ # "nonprofit": true
47
+ class OpenCorporates < Request
48
+ @base_url = 'https://api.opencorporates.com/companies/search'
49
+
50
+ # Returns the URL to request.
51
+ #
52
+ # @return [String] the URL to request
53
+ def to_s
54
+ "#{base_url}?#{to_query(convert.merge(order: 'score'))}"
55
+ end
56
+
57
+ # Returns the "AND" operator's serialization.
58
+ #
59
+ # @return [String] the "AND" operator's serialization
60
+ def and_operator
61
+ ','
62
+ end
63
+
64
+ # Returns the "OR" operator's serialization.
65
+ #
66
+ # @return [String] the "OR" operator's serialization
67
+ def or_operator
68
+ '|'
69
+ end
70
+
71
+ # Converts the MQL parameters to API-specific parameters.
72
+ #
73
+ # @return [Hash] API-specific parameters
74
+ # @see https://api.opencorporates.com/documentation/API-Reference
75
+ def convert
76
+ match('q', 'name')
77
+ equal('company_type', 'classification')
78
+ equal('created_since', 'created_at>=')
79
+ date_range('incorporation_date', 'founding_date')
80
+ date_range('dissolution_date', 'dissolution_date')
81
+ equal('per_page', 'limit', default: input['open_corporates_api_key'] && 100)
82
+
83
+ input['contact_details'] && input['contact_details'].each do |contact_detail|
84
+ if contact_detail['type'] == 'address' && (contact_detail['value'] || contact_detail['value~='])
85
+ output['registered_address'] = contact_detail['value'] || contact_detail['value~=']
86
+ end
87
+ end
88
+
89
+ # API-specific parameters.
90
+ equal('api_token', 'open_corporates_api_key')
91
+ one_of('jurisdiction_code', 'jurisdiction_code', transform: lambda{|v| v.downcase})
92
+ one_of('country_code', 'country_code', transform: lambda{|v| v.downcase})
93
+ equal('current_status', 'current_status')
94
+ all_of('industry_codes', 'industry_code', backup: :one_of)
95
+ equal('inactive', 'inactive', valid: [true, false])
96
+ equal('branch', 'branch', valid: [true, false])
97
+ equal('nonprofit', 'nonprofit', valid: [true, false])
98
+
99
+ output
100
+ end
101
+ end
102
+ end
103
+ end
104
+ end
@@ -0,0 +1,33 @@
1
+ module WhosGotDirt
2
+ module Requests
3
+ module Entity
4
+ # Requests for entities from the OpenDuka API.
5
+ #
6
+ # @example Supply an API key.
7
+ # "open_duka_api_key": "..."
8
+ class OpenDuka < Request
9
+ @base_url = 'http://www.openduka.org/index.php/api/search'
10
+
11
+ # Returns the URL to request.
12
+ #
13
+ # @return [String] the URL to request
14
+ def to_s
15
+ "#{base_url}?#{to_query(convert)}"
16
+ end
17
+
18
+ # Converts the MQL parameters to API-specific parameters.
19
+ #
20
+ # @return [Hash] API-specific parameters
21
+ # @see http://www.openduka.org/index.php/api/documentation
22
+ def convert
23
+ match('term', 'name')
24
+
25
+ # API-specific parameters.
26
+ equal('key', 'open_duka_api_key')
27
+
28
+ output
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,48 @@
1
+ module WhosGotDirt
2
+ module Requests
3
+ module Entity
4
+ # Requests for entities from the Poderopedia API.
5
+ #
6
+ # The Poderopedia API returns 10 results only, without pagination.
7
+ #
8
+ # The `entity` filter is required.
9
+ #
10
+ # @example Supply an API key.
11
+ # "poderopedia_api_key": "..."
12
+ #
13
+ # @example Find entities of the class `Person` or `Organization`.
14
+ # "entity": "Person"
15
+ class Poderopedia < Request
16
+ @base_url = 'http://api.poderopedia.org/visualizacion/search'
17
+
18
+ # Returns the URL to request.
19
+ #
20
+ # @return [String] the URL to request
21
+ def to_s
22
+ "#{base_url}?#{to_query(convert)}"
23
+ end
24
+
25
+ # Converts the MQL parameters to API-specific parameters.
26
+ #
27
+ # @return [Hash] API-specific parameters
28
+ # @see http://api.poderopedia.org/search
29
+ def convert
30
+ match('alias', 'name')
31
+
32
+ # API-specific parameters.
33
+ equal('user_key', 'poderopedia_api_key')
34
+ equal('entity', 'type', valid: ['Person', 'Organization'], transform: lambda{|v|
35
+ case v
36
+ when 'persona'
37
+ 'Person'
38
+ when 'organizacion'
39
+ 'Organization'
40
+ end
41
+ })
42
+
43
+ output
44
+ end
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,54 @@
1
+ module WhosGotDirt
2
+ module Requests
3
+ # Requests for entities.
4
+ #
5
+ # Many APIs support the parameters below. For API-specific parameters, visit
6
+ # the API's request class.
7
+ #
8
+ # You may filter `dissolution_date` the same as `founding_date`.
9
+ #
10
+ # @example Find entities by name (fuzzy match).
11
+ # "name~=": "ACME Inc."
12
+ #
13
+ # @example Find entities by classification.
14
+ # "classification": "Private Limited Company"
15
+ #
16
+ # @example Find entities with one of many classifications.
17
+ # "classification|=": "Private Limited Company"
18
+ #
19
+ # @example Find entities with a creation date greater than or equal to a given value.
20
+ # "created_at>=": "2010-01-01"
21
+ #
22
+ # @example Find organizations by founding date.
23
+ # "founding_date": "2010-01-01"
24
+ #
25
+ # @example Find organizations with a founding date greater than or equal to a given value.
26
+ # "founding_date>=": "2010-01-03"
27
+ #
28
+ # @example Find organizations with a founding date greater than a given value.
29
+ # "founding_date>": "2010-01-02"
30
+ #
31
+ # @example Find organizations with a founding date less than or equal to a given value.
32
+ # "founding_date<=": "2010-01-04"
33
+ #
34
+ # @example Find organizations with a founding date less than a given value.
35
+ # "founding_date<": "2010-01-05"
36
+ #
37
+ # @example Find entities by identifier.
38
+ # "identifiers": [{
39
+ # "identifier": "911653725",
40
+ # "scheme": "IRS Employer Identification Number"
41
+ # }]
42
+ #
43
+ # @example Find entities by address (fuzzy match).
44
+ # "contact_details": [{
45
+ # "type": "address",
46
+ # "value~=": "52 London"
47
+ # }]
48
+ #
49
+ # @example Limit the number of results.
50
+ # "limit": 5
51
+ module Entity
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,37 @@
1
+ module WhosGotDirt
2
+ module Requests
3
+ module List
4
+ # Requests for lists from the LittleSis API.
5
+ #
6
+ # The `q` parameter matches names and descriptions.
7
+ #
8
+ # @example Supply an API key.
9
+ # "little_sis_api_key": "..."
10
+ class LittleSis < Request
11
+ # The JSON response has less metadata, e.g. number of results.
12
+ @base_url = 'https://api.littlesis.org/lists.xml'
13
+
14
+ # Returns the URL to request.
15
+ #
16
+ # @return [String] the URL to request
17
+ def to_s
18
+ "#{base_url}?#{to_query(convert)}"
19
+ end
20
+
21
+ # Converts the MQL parameters to API-specific parameters.
22
+ #
23
+ # @return [Hash] API-specific parameters
24
+ # @see http://api.littlesis.org/documentation#lists
25
+ def convert
26
+ match('q', 'name')
27
+ equal('num', 'limit')
28
+
29
+ # API-specific parameters.
30
+ equal('_key', 'little_sis_api_key')
31
+
32
+ output
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,36 @@
1
+ module WhosGotDirt
2
+ module Requests
3
+ module List
4
+ # Requests for corporate groupings from the OpenCorporates API.
5
+ #
6
+ # OpenCorporates' `q` filter performs a prefix search.
7
+ #
8
+ # @example Supply an API key.
9
+ # "open_corporates_api_key": "..."
10
+ class OpenCorporates < Request
11
+ @base_url = 'https://api.opencorporates.com/corporate_groupings/search'
12
+
13
+ # Returns the URL to request.
14
+ #
15
+ # @return [String] the URL to request
16
+ def to_s
17
+ "#{base_url}?#{to_query(convert)}"
18
+ end
19
+
20
+ # Converts the MQL parameters to API-specific parameters.
21
+ #
22
+ # @return [Hash] API-specific parameters
23
+ # @see https://api.opencorporates.com/documentation/API-Reference
24
+ def convert
25
+ match('q', 'name')
26
+ equal('per_page', 'limit', default: input['open_corporates_api_key'] && 100)
27
+
28
+ # API-specific parameters.
29
+ equal('api_token', 'open_corporates_api_key')
30
+
31
+ output
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,16 @@
1
+ module WhosGotDirt
2
+ module Requests
3
+ # Requests for lists.
4
+ #
5
+ # Many APIs support the parameters below. For API-specific parameters, visit
6
+ # the API's request class.
7
+ #
8
+ # @example Find lists by name (fuzzy match).
9
+ # "name~=": "Barclays"
10
+ #
11
+ # @example Limit the number of results.
12
+ # "limit": 5
13
+ module List
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,72 @@
1
+ module WhosGotDirt
2
+ module Requests
3
+ module Relation
4
+ # Requests for corporate officerships from the OpenCorporates API.
5
+ #
6
+ # OpenCorporates' `date_of_birth` and `address` filters require an API
7
+ # key. Its `order` parameter is not supported.
8
+ #
9
+ # @example Supply an API key.
10
+ # "open_corporates_api_key": "..."
11
+ #
12
+ # @example Find officerships by jurisdiction code.
13
+ # "jurisdiction_code": "gb"
14
+ #
15
+ # @example Find officerships with one of many jurisdiction codes.
16
+ # "jurisdiction_code|=": ["gb", "ie"]
17
+ #
18
+ # @example Find officerships by role.
19
+ # "role": "ceo"
20
+ #
21
+ # @example Find active officerships.
22
+ # "inactive": false
23
+ #
24
+ # @example Find inactive officerships.
25
+ # "inactive": true
26
+ class OpenCorporates < Request
27
+ @base_url = 'https://api.opencorporates.com/officers/search'
28
+
29
+ # Returns the URL to request.
30
+ #
31
+ # @return [String] the URL to request
32
+ def to_s
33
+ "#{base_url}?#{to_query(convert.merge(order: 'score'))}"
34
+ end
35
+
36
+ # Returns the "OR" operator's serialization.
37
+ #
38
+ # @return [String] the "OR" operator's serialization
39
+ def or_operator
40
+ '|'
41
+ end
42
+
43
+ # Converts the MQL parameters to API-specific parameters.
44
+ #
45
+ # @return [Hash] API-specific parameters
46
+ # @see https://api.opencorporates.com/documentation/API-Reference
47
+ def convert
48
+ equal('per_page', 'limit', default: input['open_corporates_api_key'] && 100)
49
+
50
+ input['subject'] && input['subject'].each do |subject|
51
+ match('q', 'name', input: subject)
52
+ date_range('date_of_birth', 'birth_date', input: subject)
53
+
54
+ subject['contact_details'] && subject['contact_details'].each do |contact_detail|
55
+ if contact_detail['type'] == 'address' && (contact_detail['value'] || contact_detail['value~='])
56
+ output['address'] = contact_detail['value'] || contact_detail['value~=']
57
+ end
58
+ end
59
+ end
60
+
61
+ # API-specific parameters.
62
+ equal('api_token', 'open_corporates_api_key')
63
+ one_of('jurisdiction_code', 'jurisdiction_code', transform: lambda{|v| v.downcase})
64
+ equal('position', 'role')
65
+ equal('inactive', 'inactive', valid: [true, false])
66
+
67
+ output
68
+ end
69
+ end
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,49 @@
1
+ module WhosGotDirt
2
+ module Requests
3
+ module Relation
4
+ # Requests for concessions from the OpenOil API.
5
+ #
6
+ # @example Supply an API key.
7
+ # "open_oil_api_key": "..."
8
+ #
9
+ # @example Find concessions by country code.
10
+ # "country_code": "BR"
11
+ #
12
+ # @example Find concessions with a "licensed" or "unlicensed" status.
13
+ # "status": "licensed"
14
+ #
15
+ # @example Find concessions with an "offshore" or "onshore" type.
16
+ # "type": "offshore"
17
+ class OpenOil < Request
18
+ @base_url = 'https://api.openoil.net/concession/search'
19
+
20
+ # Returns the URL to request.
21
+ #
22
+ # @return [String] the URL to request
23
+ def to_s
24
+ "#{base_url}?#{to_query(convert)}"
25
+ end
26
+
27
+ # Converts the MQL parameters to API-specific parameters.
28
+ #
29
+ # @return [Hash] API-specific parameters
30
+ # @see http://openoil.net/openoil-api/
31
+ def convert
32
+ equal('per_page', 'limit')
33
+
34
+ input['subject'] && input['subject'].each do |subject|
35
+ equal('licensee', 'name', input: subject) # doesn't work, upstream bug
36
+ end
37
+
38
+ # API-specific parameters.
39
+ equal('apikey', 'open_oil_api_key')
40
+ equal('country', 'country_code', transform: lambda{|v| v.upcase})
41
+ equal('status', 'status', valid: ['licensed', 'unlicensed'])
42
+ equal('type', 'type', valid: ['offshore', 'onshore'])
43
+
44
+ output
45
+ end
46
+ end
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,51 @@
1
+ module WhosGotDirt
2
+ module Requests
3
+ # Requests for relations.
4
+ #
5
+ # Many APIs support the parameters below. For API-specific parameters, visit
6
+ # the API's request class.
7
+ #
8
+ # @example Find related entities by name (fuzzy match).
9
+ # "subject": [{
10
+ # "name~=": "John Smith"
11
+ # }]
12
+ #
13
+ # @example Find related people by birth date.
14
+ # "subject": [{
15
+ # "birth_date": "2010-01-01"
16
+ # }]
17
+ #
18
+ # @example Find related people with a birth date greater than or equal to a given value.
19
+ # "subject": [{
20
+ # "birth_date>=": "2010-01-03"
21
+ # }]
22
+ #
23
+ # @example Find related people with a birth date greater than a given value.
24
+ # "subject": [{
25
+ # "birth_date>": "2010-01-02"
26
+ # }]
27
+ #
28
+ # @example Find related people with a birth date less than or equal to a given value.
29
+ # "subject": [{
30
+ # "birth_date<=": "2010-01-04"
31
+ # }]
32
+ #
33
+ # @example Find related people with a birth date less than a given value.
34
+ # "subject": [{
35
+ # "birth_date<": "2010-01-05"
36
+ # }]
37
+ #
38
+ # @example Find related entities by address (fuzzy match).
39
+ # "subject": [{
40
+ # "contact_details": [{
41
+ # "type": "address",
42
+ # "value~=": "52 London"
43
+ # }]
44
+ # }]
45
+ #
46
+ # @example Limit the number of results.
47
+ # "limit": 5
48
+ module Relation
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,79 @@
1
+ module WhosGotDirt
2
+ # Accepts a response and returns the results in a consistent format.
3
+ #
4
+ # @example Create a new class for transforming responses to results.
5
+ # class MyAPIResponse < WhosGotDirt::Response
6
+ # def to_a
7
+ # JSON.load(body).map do |result|
8
+ # {
9
+ # request_url: env.url.to_s,
10
+ # name: result['Name'],
11
+ # }
12
+ # end
13
+ # end
14
+ # end
15
+ #
16
+ # @example Use the class to parse a response.
17
+ # url = MyAPIRequest.new(name: 'John Smith').to_s
18
+ # response = Faraday.get(url)
19
+ # MyAPIResponse.new(response).to_a
20
+ # # [{
21
+ # # :request_url=>"https://api.example.com/endpoint?name=John+Smith",
22
+ # # :name=>"John Smith"
23
+ # # }, {
24
+ # # :request_url=>"https://api.example.com/endpoint?name=John+Smith",
25
+ # # :name=>"John Aaron Smith"
26
+ # # }]
27
+ class Response < SimpleDelegator
28
+ class << self
29
+ # @!attribute [r] template
30
+ # @return [Hash] the result template
31
+ attr_reader :template
32
+ end
33
+
34
+ # Returns the result renderer.
35
+ #
36
+ # @return the result renderer
37
+ def renderer
38
+ @renderer ||= Renderer.new(self.class.template)
39
+ end
40
+
41
+ # Returns the parsed response body.
42
+ #
43
+ # @return the parsed response body
44
+ def parsed_body
45
+ @parsed_body ||= parse_body
46
+ end
47
+
48
+ # @abstract Subclass and override {#parse_body} to parse the response body
49
+ def parse_body
50
+ raise NotImplementedError
51
+ end
52
+
53
+ # @abstract Subclass and override {#to_a} to transform the parsed response
54
+ # body into results
55
+ def to_a
56
+ raise NotImplementedError
57
+ end
58
+
59
+ # @!method initialize(response)
60
+ # Sets the response's response.
61
+ # @param [Faraday::Response] response a response
62
+
63
+ # @!method body
64
+ # Returns the response body.
65
+ # @return [String] the response body
66
+
67
+ # @!method env
68
+ # Returns the response's internals.
69
+ # @return [#url,#request_headers] the response's internals
70
+
71
+ # @!method headers
72
+ # Returns the response headers.
73
+ # @return [#[]] the response headers
74
+
75
+ # @!method status
76
+ # Returns the HTTP status code.
77
+ # @return [Fixnum] the HTTP status code
78
+ end
79
+ end
@@ -0,0 +1,72 @@
1
+ module WhosGotDirt
2
+ module Responses
3
+ module Entity
4
+ # Converts entities from the CorpWatch API to Popolo format.
5
+ #
6
+ # @see http://api.corpwatch.org/documentation/api_examples.html#A17
7
+ class CorpWatch < Response
8
+ @template = {
9
+ '@type' => 'Entity',
10
+ 'name' => '/company_name',
11
+ 'identifiers' => [{
12
+ 'identifier' => '/cw_id',
13
+ 'scheme' => 'CorpWatch',
14
+ }, {
15
+ 'identifier' => '/irs_number',
16
+ 'scheme' => 'IRS Employer Identification Number',
17
+ }, {
18
+ 'identifier' => '/cik',
19
+ 'scheme' => 'SEC Central Index Key',
20
+ }],
21
+ 'contact_details' => [{
22
+ 'type' => 'address',
23
+ 'value' => '/raw_address',
24
+ }],
25
+ # API-specific.
26
+ 'industry_code' => '/sic_code',
27
+ 'industry_name' => '/industry_name',
28
+ 'sector_code' => '/sic_sector',
29
+ 'sector_name' => '/sector_name',
30
+ 'country_code' => '/country_code',
31
+ 'subdiv_code' => '/subdiv_code',
32
+ 'min_year' => '/min_year',
33
+ 'max_year' => '/max_year',
34
+ 'source_type' => '/source_type',
35
+ 'num_parents' => '/num_parents',
36
+ 'num_children' => '/num_children',
37
+ 'top_parent_id' => '/top_parent_id',
38
+ }
39
+
40
+ # Parses the response body.
41
+ #
42
+ # @return [Array<Hash>] the parsed response body
43
+ def parse_body
44
+ JSON.load(body)
45
+ end
46
+
47
+ # Returns the total number of matching results.
48
+ #
49
+ # @return [Fixnum] the total number of matching results
50
+ def count
51
+ Integer(parsed_body['meta']['total_results'])
52
+ end
53
+
54
+ # Returns the current index.
55
+ #
56
+ # @return [Fixnum] the current index
57
+ def index
58
+ parsed_body['meta']['parameters']['index']
59
+ end
60
+
61
+ # Transforms the parsed response body into results.
62
+ #
63
+ # @return [Array<Hash>] the results
64
+ def to_a
65
+ parsed_body['result'].map do |data|
66
+ Result.new('Entity', renderer.result(data), self).finalize!
67
+ end
68
+ end
69
+ end
70
+ end
71
+ end
72
+ end