external_api_service 0.1.1 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c8a6d1ed464dd884eb7ecec6f783d78521006391
4
- data.tar.gz: 12c9a87d3e9d9740133cddadb0e7cbe7c94def6d
3
+ metadata.gz: af81f8d361d5ed627c6f17da68411718d284c534
4
+ data.tar.gz: e3b73255e7910c738706597346e807425b7adb42
5
5
  SHA512:
6
- metadata.gz: 00b068a699b4a932442f4b7a1e93760420235e2a05402c1cad9a2e7e41297b40cfc08d4a79357d81e0622ca0a6b620c9aef6dc6be6de44cba862a523062248a6
7
- data.tar.gz: abe663289c6bf5ad6d8f4926a486a9c03a971987a495a599a2a7432a54fe46b2c751ff816f4e061d010d3203f2ff1f190c70d1ff271d8495d981625dfe369631
6
+ metadata.gz: 813fe93f53e076cb4cb69dfc4d4db68becdefe2d7be0f3fb722680fcf5736cfd49807d5edd24baa3bbc532bb90b6b4ae209dc38c4413a782bd2accdd76f33db6
7
+ data.tar.gz: 1777330e5f6f72c5890bf57a808a71105ba1e88ccd886c48d0b153bcac6b266d0ecd799acf18b6be7f2cd514f7d550b4936a6c0041f47dbb705d7d5c5f2b53a5
data/README.md CHANGED
@@ -25,11 +25,12 @@ To use, you need to require the service module wherever you are wanting to use i
25
25
 
26
26
  require ‘ExternalApiService’
27
27
 
28
+ ### GET
28
29
  To make a GET request, you need an endpoint (required), and any query params in a Hash (optional). The endpoint must return `JSON`.
29
30
  It will work with `HTTPS` or `HTTP`. Call `get_service`:
30
31
 
31
32
  # no params
32
- url = https://data.cityofchicago.org/Buildings/Problem-Landlord-List-Map/dip3-ud6z
33
+ url = 'https://data.cityofchicago.org/resource/dip3-ud6z.json'
33
34
  ExternalApiService.get_service(url)
34
35
 
35
36
  # with params
@@ -39,7 +40,7 @@ It will work with `HTTPS` or `HTTP`. Call `get_service`:
39
40
 
40
41
  `get_service` will transform the JSON to Ruby Hash with symbolized names:
41
42
 
42
- # First entry from the Chicago Problem Landlord List in Array of Hashes (https://data.cityofchicago.org/Buildings/Problem-Landlord-List-Map/dip3-ud6z)
43
+ # First entry from the Chicago Problem Landlord List in Array of Hashes ('https://data.cityofchicago.org/resource/dip3-ud6z.json')
43
44
 
44
45
  [{:respondent=>”Ravine Properties, LLC”, :secondary_address=>”5849 W ARTHINGTON ST”,
45
46
  :location=>{:needs_recoding=>false, :longitude=>”-87.7716557532”, :latitude=>”41.8690630014”},
@@ -47,10 +48,28 @@ It will work with `HTTPS` or `HTTP`. Call `get_service`:
47
48
  :street_block_id=>”7445”, :ward=>”29”, :address=>”1001 S MAYFIELD AVE”, :y_coordinate=>”1895377.068646989”,
48
49
  :community_area=>”AUSTIN”, :longitude=>”-87.7716557532”, :latitude=>”41.8690630014”, :community_area_number=>”25”}]
49
50
 
50
- ## Features To-Do
51
- Does not support `JSONP`.
51
+ ### POST
52
+ To make a POST request, you need an endpoint (required), the data you want to post, and authentication. Right now it only supports basic auth.
53
+ The endpoint must accept and return `JSON`.
52
54
 
53
- Only makes GET requests. Probably need to update to make POST requests as well.
55
+ There is a header param available, but it is optional, and `Net/HTTP` handles most of it behind the scenes.
56
+
57
+
58
+ #example: Add a subscriber to your Mailchimp Account (Api V3)
59
+ auth = {'api_key': '123key'}
60
+ optional_header = {}
61
+ data_to_post: {'email' => 'example.email@example.com', 'status' => 'subscribed'}
62
+ endpoint = 'https://us9.api.mailchimp.com/3.0/lists/123uniquelistID/members'
63
+ ExternalApiService.post_service(endpoint, data_to_post, auth, optional_header)
64
+
65
+ Note:
66
+ You have to have the trailing '/' for the path for Ruby to POST properly e.g. http://sample.com/ (I think)
67
+
68
+ `post_service` will return the body of the response in a Ruby hash with symbolized keys.
69
+
70
+ ## Sandbox
71
+ If you want to fire it up, you can run `bin/console` from the command line to launch it. You still need `require ExternalApiService`,
72
+ but you can see it in action.
54
73
 
55
74
  ## Contributing
56
75
 
@@ -3,17 +3,25 @@ require 'json'
3
3
 
4
4
  class HTTP_Client
5
5
 
6
- def get(endpoint_uri)
7
- response = get_request(endpoint_uri)
6
+ def get(endpoint_uri, credentials)
7
+ new_request = Net::HTTP::Get.new(endpoint_uri)
8
+ new_request.basic_auth(credentials[0], credentials[1]) if !credentials.empty?
9
+ response = make_request(endpoint_uri, new_request)
8
10
  parse_response(response)
9
11
  end
10
12
 
11
- def get_request(endpoint)
12
- Net::HTTP.start(endpoint.host, endpoint.port) do |client|
13
+ def post(http_params)
14
+ new_request = build_post_request(http_params)
15
+ response = make_request(http_params[:uri], new_request)
16
+ parse_response(response)
17
+ end
18
+
19
+ def make_request(endpoint, formatted_request)
20
+ check_ssl = (endpoint.scheme == "https")
21
+ Net::HTTP.start(endpoint.host, endpoint.port, :use_ssl => check_ssl) do |client|
13
22
  retries = 5
14
23
  begin
15
- new_request = Net::HTTP::Get.new(endpoint)
16
- client.request(new_request)
24
+ client.request(formatted_request)
17
25
  rescue Exception => ex
18
26
  retries -= 1
19
27
  retries > 0 ? retry : error_response(422, ex)
@@ -22,13 +30,22 @@ class HTTP_Client
22
30
  end
23
31
 
24
32
  def parse_response(response)
25
- if (response.code != "200" || response.code == nil)
26
- { error: response }
27
- else
33
+ if (response.code =~ /^2/ )
28
34
  JSON.parse(response.body, symbolize_names: true)
35
+ else
36
+ { error: response }
29
37
  end
30
38
  end
31
39
 
40
+ def build_post_request(http_params)
41
+ http_params[:header_params]["Content-Type"] = "application/json"
42
+ new_request = Net::HTTP::Post.new(http_params[:uri], http_params[:header_params])
43
+ new_request.basic_auth(http_params[:credentials][0], http_params[:credentials][1])
44
+ new_request.body = http_params[:data].to_json
45
+ new_request
46
+ end
47
+
48
+
32
49
  private
33
50
 
34
51
  def error_response(err_code, exception)
@@ -2,10 +2,11 @@ require 'uri'
2
2
 
3
3
  class URI_Builder
4
4
 
5
- def build_uri(url, queries)
5
+ def self.build_uri(url, queries = {})
6
6
  endpoint = URI(url)
7
- endpoint.query = URI.encode_www_form(queries) if !queries.nil?
7
+ endpoint.query = URI.encode_www_form(queries) if !queries.empty?
8
8
  endpoint
9
9
  end
10
10
 
11
11
  end
12
+
@@ -1,3 +1,3 @@
1
1
  module ExternalApiService
2
- VERSION = "0.1.1"
2
+ VERSION = "0.2.0"
3
3
  end
@@ -1,5 +1,4 @@
1
1
  require 'external_api_service/http_client'
2
- require 'external_api_service/https_client'
3
2
  require 'external_api_service/uri_builder'
4
3
 
5
4
  module ExternalApiService
@@ -7,15 +6,43 @@ module ExternalApiService
7
6
  # Make a GET request to external endpoint
8
7
  # @param url is required
9
8
  # @param query is optional. Query must be formatted as Ruby hash.
9
+ # @param auth is optional. If you choose to include it,
10
+ # You must format it as a hash with string key/value {"username" => "password"}
10
11
  #
11
12
  # @return Will transform the JSON to Ruby Hash with symbolized names
12
13
  #
13
14
  # @example
14
- # ExternalApiService.get_service("sample_endpoint", {sample_hash: "sample_request"})
15
+ # ExternalApiService.get_service("sample_endpoint", {sample_search_query: "sample_request"})
15
16
 
16
- def self.get_service(url, queries = nil)
17
- uri = URI_Builder.new.build_uri(url, queries)
18
- uri.scheme.include?("https") ? HTTPS_Client.new.get(uri) : HTTP_Client.new.get(uri)
17
+ def self.get_service(url, queries = {}, credentials = {})
18
+ credentials_formatted_for_auth = credentials.to_a.flatten
19
+ uri = URI_Builder.build_uri(url, queries)
20
+ HTTP_Client.new.get(uri, credentials_formatted_for_auth)
21
+ end
22
+
23
+ # Make a JSON POST request to external endpoint
24
+ # @param url is required
25
+ # @param data_to_post is required, since you are a making POST request after all.
26
+ # @param auth is required, as it is not an idempotent action.
27
+ # You must format it as a hash of strings (both key and value) e.g. {"username" => "password"}
28
+ # @param header_params are optional. This sets content-type to "application/json", so do not include Content-Type.
29
+ # You get several free headers with the Net/HTTP class, but it is there if you want it. Must be a hash
30
+ #
31
+ # @return Will transform the JSON to Ruby Hash with symbolized names
32
+ #
33
+ # @example
34
+ # auth = {"api_key": "123key"}
35
+ # data_to_post: {sample_data: "sample"}
36
+ # ExternalApiService.post_service("sample_endpoint", data_to_post, auth)
37
+ #
38
+ # Note:
39
+ # You have to have the trailing '/' for the path for Ruby to POST properly e.g. http://sample.com/
40
+
41
+ def self.post_service(url, data_to_post, credentials, http_header_params = {})
42
+ credentials_formatted_for_auth = credentials.to_a.flatten
43
+ uri = URI_Builder.build_uri(url)
44
+ http_params = { uri: uri, data: data_to_post, credentials: credentials_formatted_for_auth, header_params: http_header_params }
45
+ HTTP_Client.new.post(http_params)
19
46
  end
20
47
 
21
48
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: external_api_service
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - nrakochy
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2015-06-04 00:00:00.000000000 Z
11
+ date: 2015-06-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -113,7 +113,6 @@ files:
113
113
  - external_api_service.gemspec
114
114
  - lib/external_api_service.rb
115
115
  - lib/external_api_service/http_client.rb
116
- - lib/external_api_service/https_client.rb
117
116
  - lib/external_api_service/uri_builder.rb
118
117
  - lib/external_api_service/version.rb
119
118
  homepage:
@@ -1,44 +0,0 @@
1
- require 'net/http'
2
- require 'json'
3
- require 'thread'
4
-
5
- class HTTPS_Client
6
-
7
- def get_queue(url_queue)
8
- url_queue.map{|uri| get(uri)}
9
- end
10
-
11
- def get(endpoint_uri)
12
- response = get_request(endpoint_uri)
13
- parse_response(response)
14
- end
15
-
16
- def get_request(endpoint)
17
- Net::HTTP.start(endpoint.host, endpoint.port, :use_ssl => endpoint.scheme == 'https') do |client|
18
- retries = 5
19
- begin
20
- new_request = Net::HTTP::Get.new(endpoint)
21
- client.request(new_request)
22
- rescue Exception => ex
23
- retries -= 1
24
- retries > 0 ? retry : error_response(422, ex)
25
- end
26
- end
27
- end
28
-
29
- def parse_response(response)
30
- if (response.code != "200" || response.code == nil)
31
- { error: response }
32
- else
33
- JSON.parse(response.body, symbolize_names: true)
34
- end
35
- end
36
-
37
- private
38
-
39
- def error_response(err_code, exception)
40
- error = Struct.new(:code, :exception)
41
- error.new(err_code, exception)
42
- end
43
- end
44
-