freshdesk_apiclient 0.1.4 → 0.1.5

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: 6429194a2b6efcbe4c6cad46d080fd2b16679a01
4
- data.tar.gz: 948d6153138e7c8f1b2e887eae23e713de6c15a1
3
+ metadata.gz: 8d85fb8c558f75d5c5d8864830f14d2f09f6dae2
4
+ data.tar.gz: 0210f1247735236bf4c0b3d0abc325f45a0be929
5
5
  SHA512:
6
- metadata.gz: aac732b4c3e702350e3afecbc2b1227212857750b967115a86cda6e72bf00155e4efe4daea82d21ff14407363e55225d7591b3f11643346a9c2226a6106497cc
7
- data.tar.gz: 97e3e57d0922e6afe2c007eadd7040c4534be4f1c2dfdc386bd32f55fccb4dcd0ec858bde172a745b6ce0d8bb952a0b6982ca40734d8986e8e68f11502b233d7
6
+ metadata.gz: a4fe7698d8b7e506a41c07b4055acce08b4dabcb59daf68017b6bb7be5a2968d4963715737095beb61177c9578a3abf71d74ac326ef3372397133d598fc6d453
7
+ data.tar.gz: 252ba08dd746574322b780c596afc7ab0fc85bbfa9f00cf013de238594fc5c09ec5783f89f24e52ee5eea5af0ddcc3f38a9443d181fb51e99a0752e47c0d2786
@@ -5,6 +5,10 @@ module ObjectExtensions
5
5
  full_class_name_as_array.last
6
6
  end
7
7
 
8
+ def namespace_as_array
9
+ full_class_name_as_array[0...-1]
10
+ end
11
+
8
12
  def full_class_name_as_array
9
13
  full_class_name.split('::')
10
14
  end
@@ -5,6 +5,7 @@ require 'freshdesk_apiclient/version'
5
5
  require 'freshdesk_apiclient/utils/loggeable'
6
6
  require 'freshdesk_apiclient/utils/camelizable'
7
7
 
8
+ require 'freshdesk_apiclient/rest/model_factory'
8
9
  require 'freshdesk_apiclient/rest/resources'
9
10
  require 'freshdesk_apiclient/rest/tickets'
10
11
  require 'freshdesk_apiclient/rest/client'
@@ -1,11 +1,12 @@
1
1
  # frozen_string_literal: true
2
2
  require_relative '../../../lib/freshdesk_apiclient'
3
+ require_relative '../../../lib/freshdesk_apiclient/rest/model_factory'
4
+ require_relative '../../../lib/freshdesk_apiclient/utils/camelizable'
3
5
 
4
6
  module FreshdeskApiclient
5
7
  module REST
6
8
  class Client
7
9
  include FreshdeskApiclient::Utils::Loggeable
8
- include FreshdeskApiclient::Utils::Camelizable
9
10
 
10
11
  RESOURCES = %i(tickets).freeze
11
12
 
@@ -17,7 +18,7 @@ module FreshdeskApiclient
17
18
  username_or_api_key: FreshdeskApiclient.username_or_api_key,
18
19
  password: FreshdeskApiclient.password, logger: FreshdeskApiclient.logger)
19
20
  @base_url = "https://#{domain}.freshdesk.com/api/v2/"
20
- @credentials = {username: username_or_api_key, password: password}
21
+ @credentials = {user: username_or_api_key, password: password}
21
22
  @logger = logger
22
23
  end
23
24
 
@@ -33,7 +34,7 @@ module FreshdeskApiclient
33
34
  private
34
35
 
35
36
  def instance_variable(symbol)
36
- class_name = camelize symbol
37
+ class_name = FreshdeskApiclient::Utils::Camelizable.camelize symbol
37
38
  get_set_ivar class_name, as_ivar(class_name)
38
39
  end
39
40
 
@@ -42,23 +43,13 @@ module FreshdeskApiclient
42
43
  end
43
44
 
44
45
  def set(ivar, class_name)
45
- obj = instantiate class_name
46
+ obj = ModelFactory.new.instantiate class_name, @base_url, credentials: @credentials, logger: logger
46
47
  instance_variable_set ivar, obj
47
48
  end
48
49
 
49
50
  def as_ivar(name)
50
51
  "@#{name.downcase}"
51
52
  end
52
-
53
- def instantiate(class_name)
54
- klass(class_name, 'FreshdeskApiclient', 'REST').new(@base_url, credentials: @credentials, logger: logger)
55
- end
56
-
57
- def klass(class_name, *module_names)
58
- c = Object
59
- module_names.each {|m| c = c.const_get m }
60
- c.const_get class_name
61
- end
62
53
  end
63
54
  end
64
55
  end
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+ require_relative '../../../lib/core_extensions/object/class_name'
3
+
4
+ module FreshdeskApiclient
5
+ module REST
6
+ class ModelFactory
7
+ using ObjectExtensions
8
+
9
+ def instantiate(class_name, *args)
10
+ klass(class_name).new(*args)
11
+ end
12
+
13
+ def klass(class_name)
14
+ ancestor.const_get class_name
15
+ end
16
+
17
+ def ancestor
18
+ c = Object
19
+ namespace_as_array.each {|m| c = c.const_get m }
20
+ c
21
+ end
22
+ end
23
+ end
24
+ end
@@ -8,47 +8,37 @@ module FreshdeskApiclient
8
8
  class Resources
9
9
  using ObjectExtensions
10
10
 
11
+ ACCEPT_HEADER = {Accept: 'application/json'}.freeze
12
+ ACCEPT_AND_CONTENT_HEADERS = ACCEPT_HEADER.merge('Content-Type': 'application/json')
13
+
11
14
  def initialize(base_url, options={})
12
- @args = default_arguments options[:credentials], base_url, options[:path]
15
+ @base_url = base_url
16
+ @credentials = options[:credentials]
13
17
  RestClient.log = options[:logger]
14
18
  end
15
19
 
16
- def list
17
- execute(method: :get, headers: headers)
20
+ def list(path=nil)
21
+ url = full_url path
22
+ execute(url: url, method: :get, headers: ACCEPT_HEADER)
18
23
  end
19
24
 
20
25
  def create(json_payload)
21
- execute(method: :post, headers: content_headers, payload: json_payload)
26
+ url = full_url
27
+ execute(url: url, method: :post, headers: ACCEPT_AND_CONTENT_HEADERS, payload: json_payload)
22
28
  end
23
29
 
24
30
  private
25
31
 
26
- def resource
27
- class_name.downcase
28
- end
29
-
30
32
  def execute(args)
31
- RestClient::Request.execute @args.merge(args)
33
+ RestClient::Request.execute @credentials.merge(args)
32
34
  end
33
35
 
34
- def default_arguments(credentials, base_url, path=nil)
35
- {
36
- user: credentials[:username],
37
- password: credentials[:password],
38
- url: full_url(base_url, path)
39
- }
36
+ def full_url(path=nil)
37
+ "#{@base_url}/#{path || resource}"
40
38
  end
41
39
 
42
- def full_url(base_url, path)
43
- "#{base_url}/#{path || resource}"
44
- end
45
-
46
- def content_headers
47
- headers.merge('Content-Type': 'application/json')
48
- end
49
-
50
- def headers
51
- {Accept: 'application/json'}
40
+ def resource
41
+ class_name.downcase
52
42
  end
53
43
  end
54
44
  end
@@ -2,12 +2,12 @@
2
2
  module FreshdeskApiclient
3
3
  module Utils
4
4
  module Camelizable
5
- def camelize(term)
5
+ def self.camelize(term)
6
6
  string = term.to_s
7
7
  format string
8
8
  end
9
9
 
10
- def format(string)
10
+ def self.format(string)
11
11
  string = string.sub(/^[a-z\d]*/) { $&.capitalize }
12
12
  string.gsub!(%r{(?:_|(/))([a-z\d]*)}) { $2.capitalize }
13
13
  string
@@ -1,4 +1,4 @@
1
1
  # frozen_string_literal: true
2
2
  module FreshdeskApiclient
3
- VERSION = '0.1.4'
3
+ VERSION = '0.1.5'
4
4
  end
@@ -27,6 +27,14 @@ RSpec.describe ObjectExtensions do
27
27
  describe '#class_name' do
28
28
  it { expect(subject.class_name).to eq('Bar') }
29
29
  end
30
+
31
+ describe '#namespace_as_array' do
32
+ it('should return an Array of 1 item') do
33
+ expect(subject.namespace_as_array).to be_a(Array)
34
+ expect(subject.namespace_as_array.size).to eq(1)
35
+ end
36
+ it('first array item should eq Foo') { expect(subject.namespace_as_array[0]).to eq('Foo') }
37
+ end
30
38
  end
31
39
  end
32
40
  end
@@ -23,7 +23,7 @@ describe FreshdeskApiclient::REST::Client do
23
23
  before { FreshdeskApiclient.password = :password }
24
24
  subject { FreshdeskApiclient::REST::Client.new }
25
25
  it 'sets the credentials for the given parameters' do
26
- expect(subject.instance_variable_get(:@credentials)[:username]).to eq(:api_key)
26
+ expect(subject.instance_variable_get(:@credentials)[:user]).to eq(:api_key)
27
27
  expect(subject.instance_variable_get(:@credentials)[:password]).to eq(:password)
28
28
  end
29
29
  end
@@ -31,7 +31,7 @@ describe FreshdeskApiclient::REST::Client do
31
31
  context 'when password is not provided' do
32
32
  before { FreshdeskApiclient.password = nil }
33
33
  it 'sets the credentials for the given api_key and default password' do
34
- expect(subject.instance_variable_get(:@credentials)[:username]).to eq(:api_key)
34
+ expect(subject.instance_variable_get(:@credentials)[:user]).to eq(:api_key)
35
35
  expect(subject.instance_variable_get(:@credentials)[:password]).to eq('X')
36
36
  end
37
37
  end
@@ -52,7 +52,8 @@ describe FreshdeskApiclient::REST::Client do
52
52
 
53
53
  describe "##{method}" do
54
54
  it do
55
- klass = Object.const_get('FreshdeskApiclient').const_get('REST').const_get subject.camelize(method.to_s)
55
+ class_name = FreshdeskApiclient::Utils::Camelizable.camelize method.to_s
56
+ klass = Object.const_get('FreshdeskApiclient').const_get('REST').const_get class_name
56
57
  expect(subject.send(method)).to be_an_instance_of(klass)
57
58
  end
58
59
  end
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ RSpec.describe FreshdeskApiclient::REST::ModelFactory do
4
+ describe '#instantiate' do
5
+ it 'returns an instance of the class' do
6
+ expect(subject.instantiate(:Array)).to be_a(Array)
7
+ end
8
+ it 'passes the arguments to the class constructor' do
9
+ expect(subject.instantiate(:Array, 1, true).size).to eq(1)
10
+ expect(subject.instantiate(:Array, 1, true).first).to eq(true)
11
+ end
12
+ end
13
+
14
+ describe '#klass' do
15
+ it 'returns a class' do
16
+ expect(subject.klass(:Array)).to be_a(Class)
17
+ end
18
+ end
19
+
20
+ describe '#ancestor' do
21
+ it 'returns the parent module (namespace)' do
22
+ expect(subject.ancestor).to eq(FreshdeskApiclient::REST)
23
+ end
24
+ end
25
+ end
@@ -2,7 +2,7 @@
2
2
  require_relative '../../lib/freshdesk_apiclient/rest/resources' unless defined?(FreshdeskApiclient::REST::Resources)
3
3
 
4
4
  RSpec.describe FreshdeskApiclient::REST::Resources do
5
- subject { FreshdeskApiclient::REST::Resources.new(:url, credentials: {username: :u, password: :p}) }
5
+ subject { FreshdeskApiclient::REST::Resources.new(:url, credentials: {user: :u, password: :p}) }
6
6
 
7
7
  RSpec.shared_examples 'a resource' do
8
8
  let(:get_headers) { {Accept: 'application/json'} }
@@ -11,30 +11,17 @@ RSpec.describe FreshdeskApiclient::REST::Resources do
11
11
 
12
12
  describe '#new' do
13
13
  it 'sets the user using given credentials' do
14
- expect(subject.instance_variable_get(:@args)[:user]).to eql(:u)
14
+ expect(subject.instance_variable_get(:@credentials)[:user]).to eql(:u)
15
15
  end
16
16
 
17
17
  it 'sets the password using given credentials' do
18
- expect(subject.instance_variable_get(:@args)[:password]).to eql(:p)
19
- end
20
-
21
- context 'when path option is provided' do
22
- subject { FreshdeskApiclient::REST::Resources.new(:url, credentials: {username: :u, password: :p}, path: :foo) }
23
- it 'sets the url using given path' do
24
- expect(subject.instance_variable_get(:@args)[:url]).to eql("#{:url}/#{:foo}")
25
- end
26
- end
27
-
28
- context 'when path option is not provided' do
29
- it 'sets the url for the given resource' do
30
- expect(subject.instance_variable_get(:@args)[:url]).to eql("#{:url}/#{resource}")
31
- end
18
+ expect(subject.instance_variable_get(:@credentials)[:password]).to eql(:p)
32
19
  end
33
20
 
34
21
  it('sets the logger on RestClient') do
35
22
  rest_client = object_double('RestClient', :log= => nil).as_stubbed_const
36
23
  logger = Logger.new(STDOUT)
37
- FreshdeskApiclient::REST::Resources.new(:url, credentials: {username: :u, password: :p}, logger: logger)
24
+ FreshdeskApiclient::REST::Resources.new(:url, credentials: {user: :u, password: :p}, logger: logger)
38
25
  expect(rest_client).to have_received(:log=).with(logger)
39
26
  end
40
27
  end
@@ -52,6 +39,22 @@ RSpec.describe FreshdeskApiclient::REST::Resources do
52
39
  expect(get_headers[:Accept]).to eq('application/json')
53
40
  expect(request).to have_received(:execute).with(hash_including(headers: get_headers))
54
41
  end
42
+
43
+ context 'when path option is provided' do
44
+ it 'sets the url using given path' do
45
+ request = object_double('RestClient::Request', execute: nil).as_stubbed_const
46
+ subject.list :path
47
+ expect(request).to have_received(:execute).with(hash_including(url: "url/#{:path}"))
48
+ end
49
+ end
50
+
51
+ context 'when path option is not provided' do
52
+ it 'sets the url for the given resource' do
53
+ request = object_double('RestClient::Request', execute: nil).as_stubbed_const
54
+ subject.list
55
+ expect(request).to have_received(:execute).with(hash_including(url: "url/#{resource}"))
56
+ end
57
+ end
55
58
  end
56
59
 
57
60
  describe '#create' do
@@ -4,7 +4,7 @@ require_relative '../../lib/freshdesk_apiclient/rest/resources' unless defined?(
4
4
  require_relative '../../lib/freshdesk_apiclient/rest/tickets' unless defined?(FreshdeskApiclient::REST::Tickets)
5
5
 
6
6
  describe FreshdeskApiclient::REST::Tickets do
7
- subject { FreshdeskApiclient::REST::Tickets.new(:url, credentials: {username: :u, password: :p}) }
7
+ subject { FreshdeskApiclient::REST::Tickets.new(:url, credentials: {user: :u, password: :p}) }
8
8
 
9
9
  it_behaves_like 'a resource'
10
10
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: freshdesk_apiclient
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Erich Quintero
@@ -197,6 +197,7 @@ files:
197
197
  - lib/core_extensions/object/class_name.rb
198
198
  - lib/freshdesk_apiclient.rb
199
199
  - lib/freshdesk_apiclient/rest/client.rb
200
+ - lib/freshdesk_apiclient/rest/model_factory.rb
200
201
  - lib/freshdesk_apiclient/rest/resources.rb
201
202
  - lib/freshdesk_apiclient/rest/tickets.rb
202
203
  - lib/freshdesk_apiclient/utils/camelizable.rb
@@ -206,6 +207,7 @@ files:
206
207
  - spec/core_extensions/object/class_name_spec.rb
207
208
  - spec/freshdesk_apiclient_spec.rb
208
209
  - spec/rest/client_spec.rb
210
+ - spec/rest/model_factory_spec.rb
209
211
  - spec/rest/resources_spec.rb
210
212
  - spec/rest/tickets_spec.rb
211
213
  - spec/spec_helper.rb
@@ -234,6 +236,7 @@ signing_key:
234
236
  specification_version: 4
235
237
  summary: A ruby API client for freshdesk.com.
236
238
  test_files:
239
+ - spec/rest/model_factory_spec.rb
237
240
  - spec/rest/client_spec.rb
238
241
  - spec/rest/tickets_spec.rb
239
242
  - spec/rest/resources_spec.rb