json_client 0.2.2 → 1.0.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.
Files changed (57) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +20 -19
  3. data/Rakefile +2 -4
  4. data/json_client.gemspec +18 -17
  5. data/lib/json_client.rb +18 -6
  6. data/lib/json_client/base.rb +48 -47
  7. data/lib/json_client/base_serializer.rb +6 -3
  8. data/lib/json_client/create.rb +9 -0
  9. data/lib/json_client/destroy.rb +11 -0
  10. data/lib/json_client/dsl.rb +2 -2
  11. data/lib/json_client/dsl/on_use_collector.rb +3 -0
  12. data/lib/json_client/dsl/requests_collector.rb +0 -1
  13. data/lib/json_client/dsl/serializers_collector.rb +0 -1
  14. data/lib/json_client/empty_serializer.rb +2 -3
  15. data/lib/json_client/index.rb +9 -0
  16. data/lib/json_client/model_serializer.rb +4 -0
  17. data/lib/json_client/request.rb +48 -0
  18. data/lib/json_client/request_with_body.rb +30 -0
  19. data/lib/json_client/request_without_body.rb +17 -0
  20. data/lib/json_client/requests.rb +58 -16
  21. data/lib/json_client/response.rb +23 -0
  22. data/lib/json_client/show.rb +9 -0
  23. data/lib/json_client/update.rb +15 -0
  24. data/lib/json_client/uri_builder.rb +34 -0
  25. data/lib/json_client/version.rb +1 -1
  26. data/spec/fixtures/vcr_cassettes/all_success.yml +15 -15
  27. data/spec/fixtures/vcr_cassettes/create_success.yml +19 -20
  28. data/spec/fixtures/vcr_cassettes/destroy_success.yml +35 -36
  29. data/spec/fixtures/vcr_cassettes/show_success.yml +17 -16
  30. data/spec/fixtures/vcr_cassettes/update_success.yml +38 -40
  31. data/spec/json_client/base_responses/response_spec.rb +8 -4
  32. data/spec/json_client/base_spec.rb +32 -19
  33. data/spec/json_client/dsl_spec.rb +10 -12
  34. data/spec/json_client/model_serializer_spec.rb +14 -4
  35. data/spec/json_client/{pather_spec.rb → uri_builder_spec.rb} +26 -4
  36. data/spec/json_client_spec.rb +22 -8
  37. data/spec/spec_helper.rb +0 -1
  38. metadata +47 -46
  39. data/lib/json_client/base_requests/create.rb +0 -16
  40. data/lib/json_client/base_requests/destroy.rb +0 -13
  41. data/lib/json_client/base_requests/index.rb +0 -11
  42. data/lib/json_client/base_requests/request.rb +0 -12
  43. data/lib/json_client/base_requests/show.rb +0 -11
  44. data/lib/json_client/base_requests/update.rb +0 -16
  45. data/lib/json_client/base_responses/create.rb +0 -8
  46. data/lib/json_client/base_responses/destroy.rb +0 -8
  47. data/lib/json_client/base_responses/index.rb +0 -8
  48. data/lib/json_client/base_responses/response.rb +0 -25
  49. data/lib/json_client/base_responses/show.rb +0 -8
  50. data/lib/json_client/base_responses/update.rb +0 -8
  51. data/lib/json_client/pather.rb +0 -20
  52. data/lib/json_client/resource_handler.rb +0 -9
  53. data/lib/json_client/responses.rb +0 -29
  54. data/spec/fixtures/vcr_cassettes/authenticate_success.yml +0 -58
  55. data/spec/json_client/base_requests/request_spec.rb +0 -14
  56. data/spec/json_client/requests_spec.rb +0 -43
  57. data/spec/json_client/responses_spec.rb +0 -43
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2cd8fc37e70c92b79134039e9a0f3985b57ed451
4
- data.tar.gz: 7a961eee575d754cc11879d6ad4dc542315a1019
3
+ metadata.gz: c939e92cf7be2e4d0e73ccc904c392b50d85f779
4
+ data.tar.gz: 96ebf0b99e4bf9c39ad8cfc5909945b08191a3a8
5
5
  SHA512:
6
- metadata.gz: 7479e78af37b510c906d366d0540595cfe70d678c513d7808f78b68aaccdb0e009b4852fa2d811e2d8aeb56ef0679706ed2e40ae65ca9f934717907a130e9370
7
- data.tar.gz: 41110ba1b172a29560d7a20e3bf415f23b37a4faa47726ce1f280ccd38aa76810f5a5d0b49d3f362ba2529c9e860d50730cc9480ae26a7cea86a8e0953a2aa28
6
+ metadata.gz: c53e5204fe5d49fd3a0c065d52777f0783dcebc7806ecda9f9650fcb1d1ba0e0cbb230883997c65cd404e86a5ab8e6dd8b2eb2f581dd4154491094c43bd4aed2
7
+ data.tar.gz: ccb9e2796015af82fe7cf32bed3cd06b0abfcf24b8e86bc9e139c72672347af2ca32c8d2d685f43099c7792cfcceb56074d6ea480a57931e9695333d0ed1446a
data/README.md CHANGED
@@ -27,10 +27,6 @@ Or install it yourself as:
27
27
 
28
28
  ## Usage
29
29
 
30
- I've avoided using a DSL in favor of good engineering principals. I am
31
- hoping the DSL will come after.
32
-
33
-
34
30
  ```
35
31
  require 'json_client'
36
32
 
@@ -41,40 +37,45 @@ class Client < JsonClient::Base
41
37
 
42
38
  def pather
43
39
  @pather ||=
44
- JsonClient::Pather.new(
40
+ JsonClient::UriBuilder.new(
45
41
  'https://example.host.com',
46
42
  'api/v1',
47
- 'objects'
43
+ 'objects',
44
+ '80'
48
45
  )
49
46
  end
50
47
  end
51
48
 
52
49
  config = {
53
- api_key: 'api_key',
54
- api_password: 'api_password'
50
+ client_id: '1',
51
+ secret_key: 'dummmy_key',
52
+ host: '127.0.0.1',
53
+ port: '3000'
55
54
  }
56
55
 
57
56
  client = Client.new(config)
58
57
 
59
- # GET https://example.host.com/api/v1/objects?api_key=api_key&api_password=api_password
58
+ # GET http://example.host.com:80/api/v2/objects/2
59
+ # Headers:
60
+ # Content-Type: application/json
61
+ # Accept: application/json
62
+ # Authorization: APIAuth 1:oh3o3h00312h320u530s=
60
63
  client.index.json
61
64
  => { 'server_json' : 'is_parsed_here' }
62
65
 
63
- # GET https://my.host.com/api/v1/objects/2?api_key=api_key&api_password=api_password
66
+ # GET http://example.host.com:80/api/v2/objects
67
+ # Headers:
68
+ # Content-Type: application/json
69
+ # Accept: application/json
70
+ # Authorization: APIAuth 1:oh3o3h00312h320u530s=
64
71
  client.show(2).json
65
72
  => { 'object_json' : 'is_parsed_here' }
66
73
  ```
67
74
 
68
75
  ## Best Guide
69
76
 
70
- The best guide is [this
71
- test](https://github.com/johnmcconnell/json_client/blob/master/spec/json_client/base_spec.rb).
72
- Sorry non rspec people
77
+ Use [this test](https://github.com/johnmcconnell/json_client/blob/master/spec/json_client/base_spec.rb)
78
+ as a guide.
73
79
 
74
80
  ## Contributing
75
-
76
- 1. Fork it ( https://github.com/johnmcconnell/json_client/fork )
77
- 2. Create your feature branch (`git checkout -b my-new-feature`)
78
- 3. Commit your changes (`git commit -am 'Add some feature'`)
79
- 4. Push to the branch (`git push origin my-new-feature`)
80
- 5. Create a new Pull Request
81
+ Contributions Welcome
data/Rakefile CHANGED
@@ -1,7 +1,5 @@
1
- require "bundler/gem_tasks"
2
- require "rspec/core/rake_task"
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
3
 
4
4
  task default: :spec
5
5
  RSpec::Core::RakeTask.new(:spec)
6
-
7
-
@@ -4,28 +4,29 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
4
  require 'json_client/version'
5
5
 
6
6
  Gem::Specification.new do |spec|
7
- spec.name = "json_client"
7
+ spec.name = 'json_client'
8
8
  spec.version = JsonClient::VERSION
9
- spec.authors = ["johnmcconnell"]
10
- spec.email = ["johnnyillinois@gmail.com"]
11
- spec.summary = %q{crud json client to make creating services easy'}
12
- spec.description = %q{crud json client to make creating services easy'}
13
- spec.homepage = ""
14
- spec.license = "MIT"
9
+ spec.authors = ['johnmcconnell']
10
+ spec.email = ['johnnyillinois@gmail.com']
11
+ spec.summary = "crud json client to make creating services easy'"
12
+ spec.description = "crud json client to make creating services easy'"
13
+ spec.homepage = ''
14
+ spec.license = 'MIT'
15
15
 
16
16
  spec.files = `git ls-files -z`.split("\x0")
17
17
  spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
18
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
- spec.require_paths = ["lib"]
19
+ spec.require_paths = ['lib']
20
20
 
21
- spec.add_dependency 'rest-client', '~> 1.7.2'
22
- spec.add_dependency 'attr_init', '~> 0.0.4'
21
+ spec.add_dependency 'attr_init'
22
+ spec.add_dependency 'api-auth'
23
23
 
24
- spec.add_development_dependency "simplecov", "~> 0.8.0"
25
- spec.add_development_dependency "coveralls", "~> 0.7.0"
26
- spec.add_development_dependency "rake", "~> 10.0"
27
- spec.add_development_dependency "rspec"
28
- spec.add_development_dependency "rspec-collection_matchers"
29
- spec.add_development_dependency "vcr"
30
- spec.add_development_dependency "webmock"
24
+ spec.add_development_dependency 'simplecov'
25
+ spec.add_development_dependency 'coveralls'
26
+ spec.add_development_dependency 'rake', '~> 10.0'
27
+ spec.add_development_dependency 'rspec'
28
+ spec.add_development_dependency 'rspec-collection_matchers'
29
+ spec.add_development_dependency 'vcr'
30
+ spec.add_development_dependency 'pry'
31
+ spec.add_development_dependency 'webmock'
31
32
  end
@@ -1,10 +1,22 @@
1
- require "json_client/version"
2
- require "json_client/base"
3
- require "json_client/pather"
4
- require "json_client/dsl"
1
+ require 'json_client/version'
2
+ require 'json_client/base'
3
+ require 'json_client/model_serializer'
4
+ require 'json_client/uri_builder'
5
+
6
+ require 'digest'
5
7
 
6
8
  module JsonClient
7
- def self.new(pather, config)
8
- Base.new(pather, config)
9
+ class << self
10
+ def new(uri_builder, config)
11
+ Base.new(uri_builder, config)
12
+ end
13
+
14
+ def md5_base64_digest(string)
15
+ Digest::MD5.base64digest(string)
16
+ end
17
+
18
+ def b64_encode(string)
19
+ Base64.strict_encode64(string)
20
+ end
9
21
  end
10
22
  end
@@ -1,52 +1,43 @@
1
- require 'rest_client'
2
-
3
- require 'json_client/dsl'
4
- require 'json_client/responses'
5
1
  require 'json_client/requests'
6
- require 'json_client/model_serializer'
2
+ require 'json_client/response'
7
3
  require 'json_client/base_serializer'
8
4
  require 'json_client/empty_serializer'
9
- require 'json_client/base_requests/index'
10
- require 'json_client/base_requests/show'
11
- require 'json_client/base_requests/create'
12
- require 'json_client/base_requests/update'
13
- require 'json_client/base_requests/destroy'
14
- require 'json_client/base_responses/index'
15
- require 'json_client/base_responses/show'
16
- require 'json_client/base_responses/create'
17
- require 'json_client/base_responses/update'
18
- require 'json_client/base_responses/destroy'
5
+ require 'json_client/dsl'
6
+ require 'pry'
19
7
 
20
8
  module JsonClient
21
9
  class Base
22
10
  extend DSL
23
- attr_reader :api_key, :api_password, :pather
11
+ attr_reader :secret_key, :client_id, :uri_builder
12
+
13
+ J = ::JsonClient
14
+ RS = J::Requests
24
15
 
25
16
  requests do |r|
26
- r.on :index, use: BaseRequests::Index.new
27
- r.on :show, use: BaseRequests::Show.new
28
- r.on :create, use: BaseRequests::Create.new
29
- r.on :update, use: BaseRequests::Update.new
30
- r.on :destroy, use: BaseRequests::Destroy.new
17
+ r.on :index, use: RS::Index
18
+ r.on :show, use: RS::Show
19
+ r.on :create, use: RS::Create
20
+ r.on :update, use: RS::Update
21
+ r.on :destroy, use: RS::Destroy
31
22
  end
32
23
 
33
24
  responses do |r|
34
- r.on :index, use: BaseResponses::Index
35
- r.on :show, use: BaseResponses::Show
36
- r.on :create, use: BaseResponses::Create
37
- r.on :update, use: BaseResponses::Update
38
- r.on :destroy, use: BaseResponses::Destroy
25
+ r.on :index, use: J::Response
26
+ r.on :show, use: J::Response
27
+ r.on :create, use: J::Response
28
+ r.on :update, use: J::Response
29
+ r.on :destroy, use: J::Response
39
30
  end
40
31
 
41
32
  serializers do |s|
42
- s.on :create, :update, :destroy, use: BaseSerializer.new
43
- s.on :index, :show, use: EmptySerializer.new
33
+ s.on :create, :update, :destroy, use: J::BaseSerializer.new
34
+ s.on :index, :show, use: J::EmptySerializer.new
44
35
  end
45
36
 
46
- def initialize(pather, config)
47
- @api_key = config[:api_key]
48
- @api_password = config[:api_password]
49
- @pather = pather
37
+ def initialize(uri_builder, config)
38
+ @client_id = config[:client_id]
39
+ @secret_key = config[:secret_key]
40
+ @uri_builder = uri_builder
50
41
  validate_variables
51
42
  end
52
43
 
@@ -72,36 +63,46 @@ module JsonClient
72
63
 
73
64
  protected
74
65
 
75
- def result(name, model, id = nil)
76
- response_class = responses.public_send(name)
77
- response_class.new(fetch_response(name, model, id))
66
+ def result(protocol, model, id = nil)
67
+ responder = responders.for(protocol)
68
+
69
+ ## Fetch the response for protocol
70
+ response = fetch_response(protocol, model, id)
71
+
72
+ ## Create new response
73
+ responder.new(response)
78
74
  end
79
75
 
80
- def fetch_response(name, model, id = nil)
81
- path = pather.path(id)
82
- request = requests.public_send(name)
83
- serializer = serializers.public_send(name)
76
+ def build_request(protocol, model, id = nil)
77
+ ## Get URL path
78
+ uri = uri_builder.uri(id)
79
+ requester = requestors.for(protocol)
80
+
81
+ ## Find serializer for model
82
+ serializer = serializers.for(protocol)
84
83
  params = serializer.serialize(model)
85
- fetch(path, request, params)
84
+
85
+ ## Build the request object
86
+ requester.new(uri, auth_params, params)
86
87
  end
87
88
 
88
- def fetch(path, request, params)
89
- request.fetch(path, auth_params, params)
89
+ def fetch_response(protocol, model, id = nil)
90
+ build_request(protocol, model, id).fetch
90
91
  end
91
92
 
92
93
  def auth_params
93
94
  {
94
- api_key: api_key,
95
- api_password: api_password
95
+ client_id: client_id,
96
+ secret_key: secret_key
96
97
  }
97
98
  end
98
99
 
99
100
  private
100
101
 
101
102
  def validate_variables
102
- fail 'api_key must be set' if api_key.nil?
103
- fail 'api_password must be set' if api_password.nil?
104
- fail 'pather must be set' if pather.nil?
103
+ %w(uri_builder client_id secret_key).each do |name|
104
+ fail "#{name} must be set" if (send name).nil?
105
+ end
105
106
  end
106
107
  end
107
108
  end
@@ -1,12 +1,15 @@
1
-
2
1
  module JsonClient
3
2
  class BaseSerializer
4
3
  def serialize(model)
5
4
  if model.nil?
6
- {}
5
+ ''
7
6
  else
8
- model.to_h
7
+ model.to_h.to_json
9
8
  end
10
9
  end
10
+
11
+ def to_h(model)
12
+ model.to_h
13
+ end
11
14
  end
12
15
  end
@@ -0,0 +1,9 @@
1
+ module JsonClient
2
+ module Create
3
+ class Request < ::JsonClient::RequestWithBody
4
+ def fetch
5
+ fetch_method(:post)
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,11 @@
1
+ module JsonClient
2
+ module Destroy
3
+ class Request < ::JsonClient::Request
4
+ def fetch
5
+ client.delete(
6
+ url, params: auth_params
7
+ )
8
+ end
9
+ end
10
+ end
11
+ end
@@ -27,7 +27,7 @@ module JsonClient
27
27
 
28
28
  yield collector
29
29
 
30
- define_method :requests do
30
+ define_method :requestors do
31
31
  collector
32
32
  end
33
33
  end
@@ -41,7 +41,7 @@ module JsonClient
41
41
 
42
42
  yield collector
43
43
 
44
- define_method :responses do
44
+ define_method :responders do
45
45
  collector
46
46
  end
47
47
  end
@@ -13,6 +13,9 @@ module JsonClient
13
13
  end
14
14
  end
15
15
 
16
+ def for(name)
17
+ public_send name
18
+ end
16
19
 
17
20
  protected
18
21
 
@@ -4,7 +4,6 @@ require 'json_client/dsl/collector'
4
4
  module JsonClient
5
5
  module DSL
6
6
  class RequestsCollector < OnUseCollector
7
-
8
7
  protected
9
8
 
10
9
  def collector_name
@@ -4,7 +4,6 @@ require 'json_client/dsl/collector'
4
4
  module JsonClient
5
5
  module DSL
6
6
  class SerializersCollector < OnUseCollector
7
-
8
7
  protected
9
8
 
10
9
  def collector_name
@@ -1,8 +1,7 @@
1
-
2
1
  module JsonClient
3
2
  class EmptySerializer
4
- def serialize(model)
5
- {}
3
+ def serialize(_)
4
+ ''
6
5
  end
7
6
  end
8
7
  end
@@ -0,0 +1,9 @@
1
+ module JsonClient
2
+ module Index
3
+ class Request < ::JsonClient::Request
4
+ def fetch
5
+ client.get url, params: auth_params
6
+ end
7
+ end
8
+ end
9
+ end
@@ -5,6 +5,10 @@ module JsonClient
5
5
  reader_struct :model_name
6
6
 
7
7
  def serialize(model)
8
+ to_h(model).to_json
9
+ end
10
+
11
+ def to_h(model)
8
12
  { model_name => model.to_h }
9
13
  end
10
14
  end
@@ -0,0 +1,48 @@
1
+ require 'api_auth'
2
+
3
+ module JsonClient
4
+ class Request
5
+ attr_reader :uri, :auth_params, :params
6
+
7
+ def initialize(uri, auth_params, params)
8
+ @uri = uri
9
+ @auth_params = auth_params
10
+ @params = params
11
+ end
12
+
13
+ def sign!(request)
14
+ ApiAuth.sign!(
15
+ request,
16
+ auth_params[:client_id],
17
+ auth_params[:secret_key]
18
+ )
19
+ end
20
+
21
+ protected
22
+
23
+ def execute!(request)
24
+ Net::HTTP.start(uri.hostname, uri.port) do |http|
25
+ http.request(
26
+ sign!(request)
27
+ )
28
+ end
29
+ end
30
+
31
+ def client
32
+ Net::HTTP
33
+ end
34
+
35
+ def headers
36
+ {
37
+ 'Content-Type' => 'application/json',
38
+ 'Accept' => 'application/json'
39
+ }
40
+ end
41
+
42
+ def validate_variables
43
+ %w(url auth_params).each do |name|
44
+ fail "#{name} must be set" if (send name).nil?
45
+ end
46
+ end
47
+ end
48
+ end