taric 0.1.14 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 9b6116b623d0d9921cb1258da918635fec611525
4
- data.tar.gz: 552d59aed05e74e5d53a9458fd12d79e07c16246
3
+ metadata.gz: 0f2141c73d11c661c20fc2d93327ae3627fda562
4
+ data.tar.gz: a0409ad07a9911debed350002f27db2a5dffa0e7
5
5
  SHA512:
6
- metadata.gz: 422af6ad1eb78d33049edb70475f30b46b849ed7a41b402a7c547b0aff6c192e3a1579cd283b21b4114a606eab4bf0d772d8bd133b839df2fc87e0180bac5360
7
- data.tar.gz: 8b8ac781c7f9028ab92a73cc6ac65e51a0e4df34f89c34f14f4845cdbb32ee6aa3d27fda0d11731df8e2e29f7bd3692158abd2063389e8d5ad8cde880ab1455c
6
+ metadata.gz: eb93f0e684f289bff8a2cff4504f1888c708e77c125002af15938d812b21ef94729532c61a594499285d11ad91fb5f7caea6e7b3a2292c96caa8024f00145300
7
+ data.tar.gz: c93ec5b2740a9dcfdba453e65f4dcb46f4f34030eb1c3ff0ecc2e607a284e2464bf2e7769dcecda2e640d3685854ed93e92fe3bf421a3e59c80261257414ee37
data/README.md CHANGED
@@ -79,6 +79,24 @@ client = Taric.client(region: :na)
79
79
  client = Taric.client(region: 'kr')
80
80
  ```
81
81
 
82
+ Taric also supports parallel HTTP requests via Typhoeus.
83
+
84
+ ```ruby
85
+ # Be sure to include the adapter
86
+ require 'typhoeus/adapters/faraday'
87
+
88
+ # from regular client...
89
+ client = Taric.client(region: :na)
90
+
91
+ # ...to parallel mode. Just chain the operation methods
92
+ parallel_client = client.in_parallel.match(id: 1).match(id: 42)
93
+
94
+ # When you're ready, execute! Note, this clears the operations.
95
+ # By default each response returns a hash with HTTP status
96
+ # and body e.g. {status: 200, body: [1778689691]}
97
+ responses = parallel_client.execute!
98
+ ```
99
+
82
100
  ### Champion
83
101
 
84
102
  ```ruby
data/lib/taric.rb CHANGED
@@ -1,6 +1,6 @@
1
1
  require 'taric/client'
2
2
  require 'taric/configuration'
3
- require 'taric/connection'
3
+
4
4
  require 'taric/version'
5
5
  require 'memoist'
6
6
 
@@ -32,8 +32,7 @@ module Taric
32
32
  def client(region: :na, api_key: nil, config: @configuration ||= Taric::Configuration.new)
33
33
  Taric::Client.new(api_key: api_key || config.api_key,
34
34
  region: region.is_a?(String) ? region.to_sym : region,
35
- requestor: config.requestor.(connection(config)),
36
- response_handler: config.response_handler)
35
+ config: config)
37
36
  end
38
37
 
39
38
  # Sets global configuration. Should only be called once in a process (e.g. Rails initializer)
@@ -55,7 +54,7 @@ module Taric
55
54
  @configuration = Taric::Configuration.new
56
55
  end
57
56
 
58
- include Taric::Connection
57
+
59
58
  end
60
59
 
61
60
  end
data/lib/taric/client.rb CHANGED
@@ -1,8 +1,11 @@
1
1
  require_relative 'operation/api'
2
2
  require 'memoist'
3
+ require_relative 'connection'
3
4
  module Taric
4
5
  class Client
6
+ include Taric::Connection
5
7
  include Taric::Operation::API
8
+ attr_reader :api_key, :region, :conn, :config
6
9
 
7
10
  REGION_ENDPOINT_INFO = {
8
11
  br: {region: 'br'.freeze, platform_id: 'BR1'.freeze, host: 'br.api.pvp.net'},
@@ -22,18 +25,23 @@ module Taric
22
25
 
23
26
  # @param api_key [String] rito api key
24
27
  # @param region [Symbol] region code
25
- # @param requestor [Proc] lambda that will accept a url and return a [Faraday::Response]
26
- # @param response_handler [Proc] lambda that accepts [Faraday::Response] and handles it
27
- #
28
- def initialize(api_key:, region:, requestor:, response_handler:)
28
+ # @param config [Configuration] configuration
29
+ def initialize(api_key:, region:, config:) #requestor:, response_handler:)
29
30
  raise ArgumentError, 'api_key cannot be nil' if api_key.nil?
30
31
  raise ArgumentError, 'region cannot be nil' if region.nil?
31
32
  raise ArgumentError, "#{region} is not a valid region, #{REGION_ENDPOINT_STRING_KEYS}" if REGION_ENDPOINT_INFO[region].nil?
32
-
33
33
  @api_key = api_key
34
34
  @region = region
35
- @requestor = requestor
36
- @response_handler = response_handler
35
+ @config = config
36
+ @conn = connection(config)
37
+ # @requestor = requestor
38
+ # @response_handler = response_handler
39
+ end
40
+
41
+ # Returns a ParallelClient that will execute operations in parallel.
42
+ # @return [ParallelClient]
43
+ def in_parallel
44
+ ParallelClient.new(self)
37
45
  end
38
46
 
39
47
  class << self
@@ -43,8 +51,8 @@ module Taric
43
51
  #
44
52
  # Sets up and returns hash of api key and region values.
45
53
  #
46
- # @params api_key [String] rito api key
47
- # @params region [Symbol] key for region
54
+ # @param api_key [String] rito api key
55
+ # @param region [Symbol] key for region
48
56
  # @return [Hash] of api_key and region info
49
57
  def operation_values(api_key:, region:)
50
58
  {api_key: api_key}.merge!(REGION_ENDPOINT_INFO[region])
@@ -69,8 +77,38 @@ module Taric
69
77
  private
70
78
  def response_for(operation, options = {})
71
79
  -> url {
72
- API_CALL.(url: url, requestor: @requestor, response_handler: @response_handler)
80
+ API_CALL.(url: url, requestor: @config.requestor.(@conn), response_handler: @config.response_handler)
73
81
  }.(self.class.expand_template(api_key: @api_key, region: @region, operation: operation, options: options))
74
82
  end
83
+
84
+ class ParallelClient
85
+ include Taric::Operation::API
86
+ def initialize(parent)
87
+ @parent = parent
88
+ @operations = []
89
+ end
90
+
91
+ # Processes all chained requests, clears operations, and returns responses with respect to the operations.
92
+ #
93
+ # @return [Array] of operation responses, default is hash with response status and body.
94
+ #
95
+ # @example
96
+ # client.in_parallel.match(id: 1).match(id: 42).featured_games.execute!
97
+ #
98
+ def execute!
99
+ -> responses {
100
+ @operations.clear
101
+ responses
102
+ }.(API_CALL.(url: @operations, requestor: @parent.config.parallel_requestor.(@parent.conn), response_handler: @parent.config.parallel_response_handler))
103
+ end
104
+
105
+ private
106
+
107
+ def response_for(operation, options = {})
108
+ @operations << @parent.class.expand_template(api_key: @parent.api_key, region: @parent.region, operation: operation, options: options)
109
+ self
110
+ end
111
+
112
+ end
75
113
  end
76
114
  end
@@ -1,6 +1,6 @@
1
1
  module Taric
2
2
  class Configuration
3
- attr_accessor :api_key, :format, :user_agent, :connection_opts, :adapter, :region, :requestor, :response_handler
3
+ attr_accessor :api_key, :format, :user_agent, :connection_opts, :adapter, :region, :requestor, :response_handler, :parallel_requestor, :parallel_response_handler
4
4
 
5
5
  DEFAULT_REQUESTOR = -> connection, url {
6
6
  connection.get url
@@ -10,6 +10,14 @@ module Taric
10
10
  response.body
11
11
  }
12
12
 
13
+ PARALLEL_REQUESTOR = -> connection, urls {
14
+ urls.map{|url| connection.get url}
15
+ }.curry
16
+
17
+ PARALLEL_RESPONSE_HANDLER = -> responses {
18
+ responses.map{|response| {body: response.body, status: response.status}}
19
+ }
20
+
13
21
  def initialize(options = {})
14
22
  @api_key = options.fetch(:api_key, ENV.fetch('RIOT_API_KEY'.freeze, nil))
15
23
  @format = options.fetch(:format, :json)
@@ -19,6 +27,8 @@ module Taric
19
27
  @connection_opts = options.fetch(:connection_opts, {})
20
28
  @requestor = options.fetch(:requestor, DEFAULT_REQUESTOR)
21
29
  @response_handler = options.fetch(:response_handler, DEFAULT_RESPONSE_HANDLER)
30
+ @parallel_requestor = options.fetch(:parallel_requestor, PARALLEL_REQUESTOR)
31
+ @parallel_response_handler = options.fetch(:parallel_response_handler, PARALLEL_RESPONSE_HANDLER)
22
32
  end
23
33
  end
24
34
  end
@@ -13,6 +13,10 @@ module Taric
13
13
 
14
14
  class HttpException < Faraday::Response::Middleware
15
15
  def call(env)
16
+ default_request(env) if env.parallel_manager.nil? # might need a better way of detecting this
17
+ end
18
+
19
+ def default_request(env)
16
20
  @app.call(env).on_complete do |response|
17
21
  case response[:status]
18
22
  when 400
@@ -33,7 +37,7 @@ module Taric
33
37
  raise Taric::FaradayMiddleware::GatewayTimeout, 'Gateway timeout'
34
38
  end
35
39
  end
40
+ end
36
41
  end
37
42
  end
38
- end
39
43
  end
data/lib/taric/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Taric
2
- VERSION = '0.1.14'
2
+ VERSION = '0.2.0'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: taric
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.14
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Joseph Yi
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2015-04-02 00:00:00.000000000 Z
11
+ date: 2015-04-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -215,7 +215,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
215
215
  version: '0'
216
216
  requirements: []
217
217
  rubyforge_project:
218
- rubygems_version: 2.4.3
218
+ rubygems_version: 2.4.6
219
219
  signing_key:
220
220
  specification_version: 4
221
221
  summary: An outrageous Riot Games LoL API Client