rottendesk 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: eb3b1b0daccdc6e0dc9f3ef7318e5e1e79081e85
4
+ data.tar.gz: 60ded24611b0a97d20ace854bfcdd2995ff9d3ea
5
+ SHA512:
6
+ metadata.gz: 8107158d3c3816dc152ba984311b70eff08fc5079bc6336fc74504e3172214a03783c4081d25c01efc242170c688c43ada15f3696b0faa4a1bd2a31a71973fa8
7
+ data.tar.gz: 5ac20af27b68e20b213a8bbe18724d7eb6dceae2e8c1ca9489c542552e37799b7ee2b161937454175172a4a6ca35160de0253e9c34b8dc62f25e4a28616a8ff3
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in rottendesk.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Abdulsattar Mohammed
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,51 @@
1
+ # Rottendesk
2
+
3
+ Ruby client for [Freshdesk](https://freshdesk.com) that uses the [JSON API](http://freshdesk.com/api) of Freshdesk.
4
+
5
+ [![Code Climate](https://codeclimate.com/github/abdulsattar/rottendesk/badges/gpa.svg)](https://codeclimate.com/github/abdulsattar/rottendesk)
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ gem 'rottendesk'
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install rottendesk
20
+
21
+ ## Usage
22
+
23
+ ``` ruby
24
+ rottendesk = Rottendesk::App.new('domain.freshdesk.com', 'username/key', 'password')
25
+
26
+ rottendesk.User.find(<user_id>)
27
+ => #<Rottendesk::User:0x007f1d9935d438
28
+ @active=true,
29
+ @name = "Eddard Stark"
30
+ @address="1 Winterfell"
31
+ ...
32
+
33
+ rottendesk.User.where(options)
34
+ => [#<Rottendesk::User:0x007f1d9935d438
35
+ @active=true,
36
+ @name = "Eddard Stark"
37
+ @address="1 Winterfell"
38
+ ...,
39
+ #<Rottendesk::User:0x007f1d9935d438
40
+ @active=true,
41
+ @name = "Bran Stark"
42
+ @address="2 Tree"]
43
+ ```
44
+
45
+ ## Contributing
46
+
47
+ 1. Fork it ( https://github.com/abdulsattar/rottendesk/fork )
48
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
49
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
50
+ 4. Push to the branch (`git push origin my-new-feature`)
51
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,32 @@
1
+ require 'logger'
2
+
3
+ module Rottendesk
4
+ class App
5
+ attr_accessor :url, :username, :password, :ssl
6
+
7
+ def initialize(url, username, password = 'X', ssl = false)
8
+ @url = url
9
+ @username = username
10
+ @password = password
11
+ @ssl = ssl
12
+ end
13
+
14
+ def client
15
+ @client ||= Client.new("http#{ssl ? '' : 's'}://#{username}:#{password}@#{url}")
16
+ end
17
+
18
+
19
+ models = [User, Ticket]
20
+
21
+ models.each do |m|
22
+ class_eval %Q<
23
+ def #{m.short_name}
24
+ @#{m.short_name.downcase} ||= begin
25
+ #{m}.app = self
26
+ #{m}
27
+ end
28
+ end
29
+ >
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,46 @@
1
+ module Rottendesk
2
+ class Client
3
+
4
+ def initialize(url)
5
+ @rest_client = RestClient::Resource.new(url)
6
+ end
7
+
8
+ def get(url, options = {})
9
+ request(url, options.merge(method: :get))
10
+ end
11
+
12
+ def post(url, body, options = {})
13
+ request(url, options.merge(method: :post, body: body))
14
+ end
15
+
16
+ def patch(url, body, options = {})
17
+ request(url, options.merge(method: :patch, body: body))
18
+ end
19
+
20
+ def delete(url, options = {})
21
+ request(url, options.merge(method: :delete))
22
+ end
23
+
24
+ def request(url, options = {})
25
+ defaults = {
26
+ method: :get,
27
+ content_type: :json,
28
+ accept: :json
29
+ }
30
+ options = defaults.merge(options)
31
+
32
+ Rottendesk.logger.info "Rottendesk: #{options[:method].upcase} /#{url}.json with #{options.except(:method)}"
33
+
34
+ args = [options[:method], options[:body], options.except(:method, :body)].compact
35
+ response = @rest_client[url + '.json'].send(*args)
36
+
37
+ Rottendesk.logger.info "Rottendesk: #{response.code}"
38
+
39
+ response
40
+
41
+ rescue RestClient::Exception => e
42
+ Rottendesk.logger.warn "Rottendesk: #{e.response.code}"
43
+ raise e
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,5 @@
1
+ class Class
2
+ def short_name
3
+ name.split('::').last
4
+ end
5
+ end
@@ -0,0 +1,16 @@
1
+ class Hash
2
+ def compact
3
+ select do |_, v|
4
+ !v.nil?
5
+ end
6
+ end
7
+
8
+ def except(*keys)
9
+ dup.except!(*keys)
10
+ end
11
+
12
+ def except!(*keys)
13
+ keys.each { |key| delete(key) }
14
+ self
15
+ end
16
+ end
@@ -0,0 +1,127 @@
1
+ require 'rest_client'
2
+ require 'json'
3
+
4
+ module Rottendesk
5
+ class Model
6
+
7
+ def initialize(attrs = {})
8
+ attrs.each do |k,v|
9
+ self.send("#{k}=", v) if respond_to? "#{k}="
10
+ end
11
+ end
12
+
13
+ def persisted?
14
+ !self.id.nil?
15
+ end
16
+
17
+ def save
18
+ persisted? ? update : create_new
19
+ end
20
+
21
+ def update
22
+ end
23
+
24
+ def to_h
25
+ fields.reduce({}){|h, f| h[f] = self.send(f); h}.compact
26
+ end
27
+
28
+ def errors
29
+ @errors
30
+ end
31
+
32
+ def valid?
33
+ errors.nil? || errors.empty?
34
+ end
35
+
36
+ def from_json(json)
37
+ json = JSON.parse(json) if json.is_a? String
38
+ json = json[json_key] if json[json_key]
39
+ fields.each do |f|
40
+ self.instance_variable_set("@#{f}", json[f.to_s])
41
+ end
42
+ self
43
+ end
44
+
45
+ def fields
46
+ self.class.fields
47
+ end
48
+
49
+ protected
50
+ def create_new
51
+ json = {json_key => to_h}.to_json
52
+ response = client.post(endpoint, json)
53
+ self.from_json response
54
+ rescue RestClient::UnprocessableEntity => ex
55
+ @errors = Hash[JSON.parse(ex.response)]
56
+ self
57
+ end
58
+
59
+ def endpoint
60
+ self.class.get_endpoint
61
+ end
62
+
63
+ def json_key
64
+ self.class.get_json_key
65
+ end
66
+
67
+ def client
68
+ self.class.app.client
69
+ end
70
+
71
+ class << self
72
+
73
+ attr_accessor :app
74
+
75
+ def create(attrs = {})
76
+ model = self.new(attrs)
77
+ model.save
78
+ end
79
+
80
+ def find(id)
81
+ parse(app.client.get("#{@endpoint}/#{id}"))
82
+ end
83
+
84
+ def where(filters = {})
85
+ parse(app.client.get(@endpoint, params: filters))
86
+ end
87
+
88
+ def parse(json)
89
+ remote_fields = JSON.parse(json)
90
+
91
+ if remote_fields.is_a? Array
92
+ remote_fields.map{|f| self.new.from_json(f)}
93
+ else
94
+ self.new.from_json(remote_fields)
95
+ end
96
+ end
97
+
98
+ def field(field, options = {})
99
+ @_fields ||= []
100
+ @_fields << field
101
+
102
+ attr_reader field
103
+ attr_writer field unless options[:readonly]
104
+ end
105
+
106
+ def fields
107
+ @_fields
108
+ end
109
+
110
+ def endpoint(endpoint)
111
+ @endpoint ||= endpoint
112
+ end
113
+
114
+ def json_key(key)
115
+ @json_key = key
116
+ end
117
+
118
+ def get_endpoint
119
+ @endpoint
120
+ end
121
+
122
+ def get_json_key
123
+ @json_key
124
+ end
125
+ end
126
+ end
127
+ end
@@ -0,0 +1,29 @@
1
+ module Rottendesk
2
+ class Ticket < Model
3
+
4
+ endpoint "helpdesk/tickets"
5
+ json_key "helpdesk_ticket"
6
+
7
+ field :id, readonly: true
8
+
9
+ field :display_id, readonly: true
10
+ field :email
11
+ field :requester_id
12
+ field :subject
13
+ field :description
14
+ field :description_html
15
+ field :status
16
+ field :priority
17
+ field :source
18
+ field :deleted
19
+ field :spam
20
+ field :responder_id
21
+ field :group_id
22
+ field :ticket_type
23
+ field :cc_email
24
+ field :email_config_id
25
+ field :isescalated
26
+ field :due_by
27
+ field :attachments, readonly: true
28
+ end
29
+ end
@@ -0,0 +1,35 @@
1
+ module Rottendesk
2
+ class User < Model
3
+
4
+ endpoint "contacts"
5
+ json_key "user"
6
+
7
+ field :id, readonly: true
8
+
9
+ field :name
10
+ field :email
11
+ field :address
12
+ field :description
13
+ field :job_title
14
+ field :twitter_id
15
+ field :fb_profile_if
16
+ field :phone
17
+ field :mobile
18
+ field :language
19
+ field :time_zone
20
+ field :customer_id
21
+ field :deleted
22
+ field :helpdesk_agent, readonly: true
23
+ field :active, readonly: true
24
+
25
+ class << self
26
+ def where(filters = {})
27
+ filters[:query] = "phone is #{filters.delete(:phone)}" if filters[:phone]
28
+ filters[:query] = "mobile is #{filters.delete(:mobile)}" if filters[:mobile]
29
+ filters[:query] = "email is #{filters.delete(:email)}" if filters[:email]
30
+ super(filters)
31
+ end
32
+ end
33
+
34
+ end
35
+ end
@@ -0,0 +1,3 @@
1
+ module Rottendesk
2
+ VERSION = "0.0.1"
3
+ end
data/lib/rottendesk.rb ADDED
@@ -0,0 +1,19 @@
1
+ require "rottendesk/version"
2
+
3
+ require 'rottendesk/helpers/class'
4
+ require 'rottendesk/helpers/hash'
5
+
6
+ require 'rottendesk/models/model'
7
+ require 'rottendesk/models/ticket'
8
+ require 'rottendesk/models/user'
9
+
10
+ require 'rottendesk/client'
11
+ require 'rottendesk/app'
12
+
13
+ module Rottendesk
14
+ class << self
15
+ attr_accessor :logger
16
+ end
17
+ end
18
+
19
+ Rottendesk.logger = Logger.new $stdout
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'rottendesk/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "rottendesk"
8
+ spec.version = Rottendesk::VERSION
9
+ spec.authors = ["Abdulsattar Mohammed"]
10
+ spec.email = ["asattar.md@gmail.com"]
11
+ spec.summary = %q{Ruby client for Freshdesk}
12
+ spec.description = %q{Ruby client for Freshdesk that uses the JSON API of Freshdesk}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_runtime_dependency 'rest_client'
22
+
23
+ spec.add_development_dependency "bundler"
24
+ spec.add_development_dependency "pry"
25
+ spec.add_development_dependency "rake"
26
+ end
metadata ADDED
@@ -0,0 +1,116 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rottendesk
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Abdulsattar Mohammed
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-09-17 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rest_client
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: pry
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: Ruby client for Freshdesk that uses the JSON API of Freshdesk
70
+ email:
71
+ - asattar.md@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - Gemfile
78
+ - LICENSE.txt
79
+ - README.md
80
+ - Rakefile
81
+ - lib/rottendesk.rb
82
+ - lib/rottendesk/app.rb
83
+ - lib/rottendesk/client.rb
84
+ - lib/rottendesk/helpers/class.rb
85
+ - lib/rottendesk/helpers/hash.rb
86
+ - lib/rottendesk/models/model.rb
87
+ - lib/rottendesk/models/ticket.rb
88
+ - lib/rottendesk/models/user.rb
89
+ - lib/rottendesk/version.rb
90
+ - rottendesk.gemspec
91
+ homepage: ''
92
+ licenses:
93
+ - MIT
94
+ metadata: {}
95
+ post_install_message:
96
+ rdoc_options: []
97
+ require_paths:
98
+ - lib
99
+ required_ruby_version: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ required_rubygems_version: !ruby/object:Gem::Requirement
105
+ requirements:
106
+ - - ">="
107
+ - !ruby/object:Gem::Version
108
+ version: '0'
109
+ requirements: []
110
+ rubyforge_project:
111
+ rubygems_version: 2.2.2
112
+ signing_key:
113
+ specification_version: 4
114
+ summary: Ruby client for Freshdesk
115
+ test_files: []
116
+ has_rdoc: