api_client_base 0.1.0 → 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: a47670c0ba3b9854d0b1fe79fff7b6c507770b02
4
- data.tar.gz: ddbab067dc66987450d9784f95fceba9c819a71b
3
+ metadata.gz: 40aec0e5f2dddf675b4e5a7df5e30518d1e28b7f
4
+ data.tar.gz: 9c225931a08517e543e8818ceef6ce8cc5828e50
5
5
  SHA512:
6
- metadata.gz: 17c4e3977db0ee463d9606c84b43a68c82ad7b32fed463a05348c1d99a7f54f1ef6e38206611b83b238d2e517272c98fe75375f62d8bf52c0ba9cb5f0ace32b1
7
- data.tar.gz: 5ceaf0fcd18fd69a6ff01a0f555ce4f22bf669b35ba55afcd2c2cdcbcaeaa9390739a9f370fc145160cf71327240be29bef83d28525fe8c383056bdbdbd599ab
6
+ metadata.gz: 7755b15e5885babce88d5116a9f64dacafc8231a92a2c67b0fc22390b3f1128e6ef365f487a26568f502486430428ea52bde05aa5cdace1a58951a36d1f7e121
7
+ data.tar.gz: ae7ab45a1807d225c906df4235e017c4fcf0ac0b74ab363fe207e6622887418c44ae09466b115e7f5cfbb1b4a3486432fd67668d47e6ed6849fd955bdb4707c4
data/CHANGELOG.md CHANGED
@@ -4,6 +4,17 @@ All notable changes to this project will be documented in this file.
4
4
  The format is based on [Keep a Changelog](http://keepachangelog.com/)
5
5
  and this project adheres to [Semantic Versioning](http://semver.org/).
6
6
 
7
- ## [Unreleased]
7
+ ## [0.2.0]
8
+ ### Added
9
+ - Convenience module for gem's base module
10
+
11
+ ### Fixed
12
+ - Do not require arguments to be passed into api actions
13
+ - Do not singularize the action_name. Call camelize instead of classify
14
+
15
+ ### Changed
16
+ - Typhoeus is not a dependency; user must add it if they will use `Request#call`
17
+
18
+ ## [0.1.0] - 2016-12-15
8
19
  ### Added
9
20
  - Initial release
data/Gemfile CHANGED
@@ -4,3 +4,4 @@ source 'https://rubygems.org'
4
4
  gemspec
5
5
 
6
6
  gem "pry-byebug"
7
+ gem "typhoeus"
data/README.md CHANGED
@@ -16,6 +16,34 @@ This gem assumes your gem will follow a certain structure.
16
16
  - Request class takes care of appending the endpoint's path to the host, preparing params, and specifying the right http method to call
17
17
  - Response class takes care of parsing the response and making the data easily accessible for consumption.
18
18
 
19
+ ### Configuring the gem's base module
20
+
21
+ Do this:
22
+
23
+ ```ruby
24
+ module MyGem
25
+ include APIClientBase::Base.module
26
+
27
+ with_configuration do
28
+ has :host, classes: String
29
+ has :username, classes: String
30
+ has :password, classes: String
31
+ end
32
+ end
33
+ ```
34
+
35
+ And you can
36
+
37
+ - configure (thanks to [gem_config](https://github.com/krautcomputing/gem_config)) the gem's base module with defaults:
38
+
39
+ ```ruby
40
+ MyGem.configure do |c|
41
+ c.host = "https://test.api.com"
42
+ end
43
+ ```
44
+
45
+ - instantiate `MyGem::Client` by calling `MyGem.new(host: "https://api.com", username: "user", password: "password")`. If you do not specify an option, it will use the gem's default.
46
+
19
47
  ### Configuring the `Client`
20
48
 
21
49
  #### Default Options
@@ -31,12 +59,12 @@ module MyGem
31
59
  private
32
60
 
33
61
  def default_opts
62
+ # At least include `host`. Other things you may need to include: `token`, `secret`
34
63
  { host: HOST, secret: MyGem.configuration.secret }
35
64
  end
36
65
  end
37
66
 
38
67
  class Client
39
- # You may also give a hash directly
40
68
  HOST = "https://production.com"
41
69
  include APIClientBase::Client.module(default_opts: {host: HOST})
42
70
  end
@@ -61,7 +89,6 @@ module MyGem
61
89
  private
62
90
 
63
91
  def all_opts
64
- # At least include `host`. Other things you may need to include: `token`, `secret`
65
92
  { host: "http://prod.com" }
66
93
  end
67
94
  end
@@ -75,9 +102,12 @@ You still need to create `MyGem::GetUserRequest` and `MyGem::GetUserResponse`. S
75
102
 
76
103
  #### Requests
77
104
 
105
+ Requests assume a REST-like structure. This currently does not play well with a SOAP server. You could still use the `Base`, `Client`, and `Response` modules however. For SOAP APIs, write your own Request class that defines `#call`. This method needs to return the `raw_response`.
106
+
78
107
  ```ruby
79
108
  module MyGem
80
109
  class GetUserRequest
110
+ # You must install typhoeus if you use the `APIClientBase::Request`. Add it to your gemfile.
81
111
  include APIClientBase::Request.module(
82
112
  # you may define the action by `action: :post`. Defaults to `:get`.
83
113
  # You may also opt to define `#default_action` (see below)
@@ -86,7 +116,13 @@ module MyGem
86
116
  private
87
117
 
88
118
  def path
89
- "/api/v1/users/#{self.user_id}"
119
+ # all occurrences of `/:\w+/` in the string will have the matches replaced with the
120
+ # request object's value of that method. For example, if `request.user_id` is 33
121
+ # then you will get "/api/v1/users/33" as the path
122
+ "/api/v1/users/:user_id"
123
+
124
+ # Or you can interpolate it yourself if you want
125
+ # "/api/v1/users/#{self.user_id}"
90
126
  end
91
127
 
92
128
  # Following methods are optional. Override them if you need to send something specific
@@ -32,7 +32,7 @@ Gem::Specification.new do |spec|
32
32
 
33
33
  spec.add_dependency "activesupport", ">= 3.0"
34
34
  spec.add_dependency "virtus", ">= 1.0"
35
- spec.add_dependency "typhoeus", ">= 1.0"
35
+ spec.add_dependency "gem_config", ">= 0.3.1"
36
36
 
37
37
  spec.add_development_dependency "bundler", "~> 1.13"
38
38
  spec.add_development_dependency "rake", "~> 10.0"
@@ -0,0 +1,13 @@
1
+ module APIClientBase
2
+ module Base
3
+ module ClassMethods
4
+
5
+ def new(opts={})
6
+ client_class = self.const_get("Client")
7
+ client_opts = self.configuration.current.merge(opts)
8
+ client_class.new(client_opts)
9
+ end
10
+
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,27 @@
1
+ module APIClientBase
2
+ module Base
3
+
4
+ def self.module(opts={})
5
+ mod = Module.new do
6
+ mattr_accessor :api_client_base_base_options
7
+
8
+ def self.included(base)
9
+ base.mattr_accessor :api_client_base_base_options
10
+ base.api_client_base_base_options = self.api_client_base_base_options
11
+ base.send :include, APIClientBase::Base
12
+ end
13
+ end
14
+
15
+ mod.api_client_base_base_options = opts
16
+
17
+ mod
18
+ end
19
+
20
+ extend ActiveSupport::Concern
21
+
22
+ included do
23
+ include GemConfig::Base
24
+ end
25
+
26
+ end
27
+ end
@@ -3,13 +3,13 @@ module APIClientBase
3
3
  module ClassMethods
4
4
 
5
5
  def api_action(action_name, opts={})
6
- define_method action_name do |args|
6
+ define_method action_name do |args={}|
7
7
  namespace = self.class.name.deconstantize.constantize
8
8
 
9
- request_class_name = [action_name.to_s.classify, "Request"].join
9
+ request_class_name = [action_name.to_s.camelize, "Request"].join
10
10
  request_class = namespace.const_get(request_class_name)
11
11
 
12
- response_class_name = [action_name.to_s.classify, "Response"].join
12
+ response_class_name = [action_name.to_s.camelize, "Response"].join
13
13
  response_class = namespace.const_get(response_class_name)
14
14
 
15
15
  if opts[:args].is_a?(Array)
@@ -32,15 +32,20 @@ module APIClientBase
32
32
  end
33
33
 
34
34
  def call
35
- request = Typhoeus::Request.new(
36
- uri,
37
- method: action,
38
- headers: headers,
39
- body: body,
40
- params: params,
41
- )
35
+ require "typhoeus"
36
+ if defined?(Typhoeus)
37
+ request = Typhoeus::Request.new(
38
+ uri,
39
+ method: action,
40
+ headers: headers,
41
+ body: body,
42
+ params: params,
43
+ )
42
44
 
43
- request.run
45
+ request.run
46
+ else
47
+ fail "Either override #call or make sure Typhoeus is available for use."
48
+ end
44
49
  end
45
50
 
46
51
  private
@@ -1,3 +1,3 @@
1
1
  module APIClientBase
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
@@ -2,9 +2,11 @@ require "active_support/concern"
2
2
  require "active_support/core_ext/class/attribute"
3
3
  require "active_support/core_ext/module/attribute_accessors"
4
4
  require "active_support/inflector"
5
- require "typhoeus"
5
+ require "gem_config"
6
6
  require "virtus"
7
7
  require "api_client_base/version"
8
+ require "api_client_base/base"
9
+ require "api_client_base/base/class_methods"
8
10
  require "api_client_base/client"
9
11
  require "api_client_base/client/class_methods"
10
12
  require "api_client_base/request"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: api_client_base
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ramon Tayag
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-12-15 00:00:00.000000000 Z
11
+ date: 2016-12-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -39,19 +39,19 @@ dependencies:
39
39
  - !ruby/object:Gem::Version
40
40
  version: '1.0'
41
41
  - !ruby/object:Gem::Dependency
42
- name: typhoeus
42
+ name: gem_config
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
45
  - - ">="
46
46
  - !ruby/object:Gem::Version
47
- version: '1.0'
47
+ version: 0.3.1
48
48
  type: :runtime
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
52
  - - ">="
53
53
  - !ruby/object:Gem::Version
54
- version: '1.0'
54
+ version: 0.3.1
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: bundler
57
57
  requirement: !ruby/object:Gem::Requirement
@@ -156,6 +156,8 @@ files:
156
156
  - bin/console
157
157
  - bin/setup
158
158
  - lib/api_client_base.rb
159
+ - lib/api_client_base/base.rb
160
+ - lib/api_client_base/base/class_methods.rb
159
161
  - lib/api_client_base/client.rb
160
162
  - lib/api_client_base/client/class_methods.rb
161
163
  - lib/api_client_base/request.rb