fiddler 0.0.1alpha → 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/lib/fiddler.rb CHANGED
@@ -1,6 +1,8 @@
1
1
  require 'fiddler/configuration'
2
- require 'fiddler/helper'
3
2
  require 'fiddler/errors'
4
3
  require 'fiddler/parsers'
4
+ require 'fiddler/formatters'
5
5
  require 'fiddler/ticket'
6
- require 'fiddler/connection_manager'
6
+ require 'fiddler/connection_manager'
7
+
8
+ require 'active_support/all'
@@ -16,10 +16,13 @@ module Fiddler
16
16
 
17
17
  def get(url,options)
18
18
  url = "#{base_url}#{url}"
19
- @client.get(url,options_with_login(options)).content
19
+ options = options_with_login(options)
20
+ @client.get(url,options).content
20
21
  end
21
22
 
22
- def post
23
+ def post(url,options)
24
+ url = "#{base_url}#{url}"
25
+ @client.post(url,options_with_login(options)).content
23
26
  end
24
27
 
25
28
  def options_with_login(options)
@@ -32,33 +35,37 @@ module Fiddler
32
35
  class << self
33
36
  attr_accessor :client_connection
34
37
 
35
- def connection
36
- self.client_connection ||= Connection.new
37
- end
38
-
39
38
  def get(url,options={})
40
39
  check_config
41
- connection.get(url,options)
40
+ response = connection.get(url,options)
41
+ debug(response)
42
+ response
42
43
  end
43
44
 
44
- def post(url,data,options={})
45
+ def post(url,options={})
45
46
  check_config
46
- connection.post(url,options)
47
+ response = connection.post(url,options)
48
+ debug(response)
49
+ response
47
50
  end
48
51
 
49
52
  protected
50
53
 
51
54
  def check_config
52
55
  config = Fiddler.configuration
53
- raise InvalidConfigurationError unless Helper.valid?(config.server_url)
54
- unless Helper.valid?(config.username) and Helper.valid?(config.password)
56
+ raise InvalidConfigurationError if config.server_url.blank?
57
+ if config.username.blank? or config.password.blank?
55
58
  raise InvalidConfigurationError unless config.use_cookies
56
59
  end
57
60
  end
58
61
 
59
- def debug
62
+ def connection
63
+ self.client_connection ||= Connection.new
60
64
  end
61
65
 
66
+ def debug(response)
67
+ puts response.inspect if ENV['DEBUG']
68
+ end
62
69
  end # end class method definitions
63
70
  end # end ConnectionManager module definition
64
71
  end # end main module
@@ -3,4 +3,5 @@ module Fiddler
3
3
  class TicketNotFoundError < StandardError; end
4
4
  class InvalidConfigurationError < StandardError; end
5
5
  class TicketAccessDeniedError < StandardError; end
6
+ class RequestError < StandardError; end
6
7
  end
@@ -0,0 +1,2 @@
1
+ require 'fiddler/formatters/base_formatter'
2
+ require 'fiddler/formatters/search_request_formatter'
@@ -0,0 +1,6 @@
1
+ module Fiddler
2
+ module Formatters
3
+ class BaseFormatter
4
+ end
5
+ end
6
+ end
@@ -0,0 +1,86 @@
1
+ module Fiddler
2
+ module Formatters
3
+ class SearchRequestFormatter < BaseFormatter
4
+ class << self
5
+ def format(options={})
6
+ result = Hash.new
7
+
8
+ unless options.empty?
9
+ query = Array.new
10
+
11
+ add_array_or_single(options, query, :queue)
12
+ add_array_or_single(options, query, :status)
13
+ add_text_search_fields(options,query)
14
+ add_date_search_fields(options,query)
15
+
16
+ query << options[:conditions].to_s.chomp if options[:conditions]
17
+
18
+ result = { "query" => query.join(" AND "), :format => "l"}
19
+ end
20
+ result
21
+ end
22
+
23
+ protected
24
+
25
+ def add_array_or_single(options, query_array, key)
26
+ unless query_array and options and options.keys.include?(key)
27
+ return false
28
+ end
29
+ value = options[key]
30
+ name = key.to_s.camelize
31
+ if value.is_a?(Array)
32
+ sub_parts = Array.new
33
+ value.each do |part|
34
+ sub_parts << "#{name} = '#{part}'"
35
+ end
36
+ query_array << '( ' + sub_parts.join(' OR ') + ' )'
37
+ elsif value.is_a?(String) || value.is_a?(Symbol)
38
+ query_array << "#{name} = '#{value.to_s}'"
39
+ end
40
+ end
41
+
42
+ def add_text_search_fields(options,query_array)
43
+ text_fields = %w( subject content content_type file_name owner requestors cc admin_cc)
44
+ options.each do |key, value|
45
+ if text_fields.include?(key.to_s)
46
+ key = key.to_s.camelize
47
+ parts = Array.new
48
+ if value.is_a?(Array)
49
+ value.each do |v|
50
+ parts << "#{key} LIKE '#{v}'"
51
+ end
52
+ query_array << '( ' + parts.join(" AND ") + ' )'
53
+ elsif value.is_a?(String)
54
+ # special case for when user is Nobody, like doesnt work there
55
+ if key == "Owner" and value == "Nobody"
56
+ query_array << "#{key} = '#{value}'"
57
+ else
58
+ query_array << "#{key} LIKE '#{value}'"
59
+ end
60
+ end
61
+ end
62
+ end
63
+ end
64
+
65
+ def add_date_search_fields(options,query_array)
66
+ date_field = %w( created started resolved told last_updated starts due updated )
67
+ options.each do |key, value|
68
+ if date_field.include?(key.to_s)
69
+ key = key.to_s.camelize
70
+ parts = Array.new
71
+ if value.is_a?(Range) or value.is_a?(Array)
72
+ parts << "#{key} > '#{value.first.is_a?(Time) ? value.first.strftime("%Y-%m-%d %H:%M:%S") : value.first.to_s}'"
73
+ parts << "#{key} < '#{value.last.is_a?(Time) ? value.last.strftime("%Y-%m-%d %H:%M:%S") : value.last.to_s}'"
74
+ elsif value.is_a?(String)
75
+ parts << "#{key} > '#{value.to_s}'"
76
+ elsif value.is_a?(Time)
77
+ parts << "#{key} > '#{value.strftime("%Y-%m-%d %H:%M:%S")}'"
78
+ end
79
+ query_array << '( ' + parts.join(" AND ") + ' )'
80
+ end
81
+ end
82
+ end
83
+ end
84
+ end
85
+ end
86
+ end
@@ -1,13 +1,22 @@
1
1
  module Fiddler
2
2
  module Parsers
3
3
  class BaseParser
4
- def self.successful?(response)
5
- lines = response.split("\n")
6
- lines.first =~ /200/ ? true : false
7
- end
8
-
9
- def self.message(response)
10
- # strip out the message from the first line and return it
4
+
5
+ SUCCESS_CODES = (200..299).to_a
6
+ ERROR_CODES = (400..499).to_a
7
+
8
+ def self.check_response_code(response)
9
+ lines = response.split("\n").reject { |l| l.nil? or l == "" }
10
+ if lines.count == 0
11
+ raise RequestError, "Empty Response"
12
+ else
13
+ status_line = lines.shift
14
+ version, status_code, status_text = status_line.split(/\s+/,2)
15
+ unless SUCCESS_CODES.include?(status_code.to_i)
16
+ raise RequestError, status_text
17
+ end
18
+ lines
19
+ end
11
20
  end
12
21
  end
13
22
  end
@@ -1,9 +1,57 @@
1
1
  module Fiddler
2
2
  module Parsers
3
3
  class TicketParser < BaseParser
4
- def self.parse(response)
5
- success = self.successful?(response)
6
- puts success ? "Successful" : "Failed"
4
+ def self.parse_single(response)
5
+ response = check_response_code(response)
6
+ response = check_for_errors(response)
7
+ ticket_from_response(response)
8
+ end
9
+
10
+ def self.parse_multiple(response)
11
+ response = check_response_code(response)
12
+ response = check_for_errors(response)
13
+ ticket_token_responses = tokenize_response(response)
14
+ tickets = Array.new
15
+ ticket_token_responses.each do |token_response|
16
+ tickets << ticket_from_response(token_response)
17
+ end
18
+ tickets
19
+ end
20
+
21
+ protected
22
+
23
+ def self.check_for_errors(response)
24
+ message = response.first.strip
25
+ if message =~ /^#/
26
+ raise Fiddler::TicketNotFoundError, message
27
+ end
28
+ response
29
+ end
30
+
31
+ def self.ticket_from_response(response)
32
+ result = {}
33
+ response.each do |line|
34
+ matches = /^(.*?):\s(.*)/.match(line)
35
+ if(matches)
36
+ key = matches[1].underscore
37
+ result[key] = matches[2]
38
+ end
39
+ end
40
+ Fiddler::Ticket.new(result)
41
+ end
42
+
43
+ def self.tokenize_response(response)
44
+ tokens = Array.new
45
+ current = Array.new
46
+ response.each do |item|
47
+ if(item == "--")
48
+ tokens << current
49
+ current = Array.new
50
+ next
51
+ end
52
+ current << item
53
+ end
54
+ tokens
7
55
  end
8
56
  end
9
57
  end
@@ -1,16 +1,42 @@
1
1
  module Fiddler
2
2
  class Ticket
3
- attr_accessor :id, :subject, :status, :queue, :owner, :creator, :histories, :content, :last_updated
4
3
 
4
+ DefaultAttributes = %w(queue owner creator subject status priority initial_priority final_priority requestors cc admin_cc created starts started due resolved told last_updated time_estimated time_worked time_left text).inject({}){|memo, k| memo[k] = nil; memo}
5
+ RequiredAttributes = %w(queue subject)
6
+
7
+ attr_reader :histories, :saved
8
+
5
9
  # Initializes a new instance of ticket object
6
10
  #
7
11
  # @params [Hash] of the initial options
8
- def initialize(options={})
9
- options = { :queue => "General", :owner => "Nobody", :status => "new"}.merge options
10
- options.each do |key,value|
11
- send "#{key}=", value
12
+ def initialize(attributes={})
13
+ if attributes
14
+ @attributes = DefaultAttributes.merge(attributes)
15
+ else
16
+ @attributes = DefaultAttributes
17
+ end
18
+ @attributes.update(:id => 'ticket/new')
19
+ @saved = false
20
+ @histories = []
21
+ @new_record = true
22
+ add_methods!
23
+ end
24
+
25
+ def add_methods!
26
+ @attributes.each do |key, value|
27
+ (class << self; self; end).send :define_method, key do
28
+ return @attributes[key]
29
+ end
30
+ (class << self; self; end).send :define_method, "#{key}=" do |new_val|
31
+ @attributes[key] = new_val
32
+ end
33
+ end
34
+ end
35
+
36
+ def description
37
+ @attributes.each do |key,value|
38
+ puts "#{key} = #{value}"
12
39
  end
13
- @id = "ticket/new"
14
40
  end
15
41
 
16
42
  # Class methods
@@ -22,7 +48,17 @@ module Fiddler
22
48
  def get(id)
23
49
  url = "ticket/#{id}"
24
50
  response = Fiddler::ConnectionManager.get(url)
25
- ticket = Fiddler::Parsers::TicketParser.parse(response)
51
+ ticket = Fiddler::Parsers::TicketParser.parse_single(response)
52
+ end
53
+
54
+ # Search the tickets with the given conditions
55
+ #
56
+ # @params [Hash] of conditions
57
+ # @returns [Array<Ticket>] of the tickets matching the criteria
58
+ def all(conditions={})
59
+ url = "search/ticket"
60
+ response = Fiddler::ConnectionManager.get(url,Fiddler::Formatters::SearchRequestFormatter.format(conditions))
61
+ ticket = Fiddler::Parsers::TicketParser.parse_multiple(response)
26
62
  end
27
63
 
28
64
  # Creates a new ticket with the given options, it will not save the ticket
@@ -1,3 +1,3 @@
1
1
  module Fiddler
2
- VERSION = "0.0.1alpha"
2
+ VERSION = "0.0.1"
3
3
  end
@@ -3,8 +3,7 @@ require 'spec_helper'
3
3
  describe Fiddler::Configuration do
4
4
  describe 'when no options are given' do
5
5
  before do
6
- Fiddler.configure do |config|
7
- end
6
+ reset_config
8
7
  end
9
8
 
10
9
  it 'defaults to using no cookies' do
@@ -1,22 +1,38 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe Fiddler::ConnectionManager do
4
- before do
5
- reset_config
6
- end
7
-
8
4
  describe "without proper config" do
5
+ before do
6
+ reset_config
7
+ end
8
+
9
9
  it "should raise invalid Configuration error for missing configuration" do
10
- Fiddler.configure do |config|
11
- end
12
10
  expect { Fiddler::ConnectionManager.get("https://www.google.com") }.to raise_error(Fiddler::InvalidConfigurationError)
13
11
  end
14
12
 
15
13
  it "should raise invalid Configuration error for missing credentials if cookies are not enabled" do
16
- Fiddler.configure do |config|
17
- config.server_url = "https://some_server"
18
- end
19
- expect { Fiddler::ConnectionManager.get("https://www.google.com") }.to raise_error(Fiddler::InvalidConfigurationError)
14
+ Fiddler.configuration.server_url = "https://some_server"
15
+ expect { Fiddler::ConnectionManager.get("/") }.to raise_error(Fiddler::InvalidConfigurationError)
16
+ end
17
+
18
+ it "should raise error for invalid server url" do
19
+ Fiddler.configuration.server_url = "https://some_server"
20
+ Fiddler.configuration.use_cookies = true
21
+ expect { Fiddler::ConnectionManager.get("/") }.to raise_error
22
+ end
23
+ end
24
+
25
+ describe "making request" do
26
+ before do
27
+ test_config
28
+ end
29
+
30
+ it "should not return nil response for get request" do
31
+ Fiddler::ConnectionManager.get("/").should_not be_nil
32
+ end
33
+
34
+ it "should not return nil response for post request" do
35
+ Fiddler::ConnectionManager.post("/").should_not be_nil
20
36
  end
21
37
  end
22
38
  end
@@ -0,0 +1,4 @@
1
+ require 'spec_helper'
2
+
3
+ describe Fiddler::Formatters::BaseFormatter do
4
+ end
@@ -0,0 +1,17 @@
1
+ require 'spec_helper'
2
+
3
+ describe Fiddler::Formatters::SearchRequestFormatter do
4
+ it "should return a hash for the format" do
5
+ Fiddler::Formatters::SearchRequestFormatter.format( { :owner => "jais.cheema" }).should be_a_kind_of(Hash)
6
+ end
7
+
8
+ it "should return a query key in hash with the search options" do
9
+ search_hash = Fiddler::Formatters::SearchRequestFormatter.format( { :owner => "jais.cheema" })
10
+ search_hash.keys.should include("query")
11
+ end
12
+
13
+ it "should return a format key in hash with the long option" do
14
+ search_hash = Fiddler::Formatters::SearchRequestFormatter.format( { :owner => "jais.cheema" })
15
+ search_hash.keys.should include(:format)
16
+ end
17
+ end
@@ -0,0 +1,24 @@
1
+ require 'spec_helper'
2
+
3
+ describe Fiddler::Parsers::BaseParser do
4
+ before do
5
+ test_config
6
+ end
7
+
8
+ it "should not raise error for valid request" do
9
+ response = Fiddler::ConnectionManager.get("/")
10
+ expect { Fiddler::Parsers::BaseParser.check_response_code(response) }.to_not raise_error
11
+ end
12
+
13
+ it "should return an array of lines after checking response" do
14
+ response = Fiddler::ConnectionManager.get("/")
15
+ Fiddler::Parsers::BaseParser.check_response_code(response).should be_a_kind_of(Array)
16
+ end
17
+
18
+ it "should strip the response line from return array" do
19
+ response = Fiddler::ConnectionManager.get("/")
20
+ response_array = response.split("\n").reject { |l| l.nil? or l == "" }
21
+
22
+ Fiddler::Parsers::BaseParser.check_response_code(response).length.should eql(response_array.length-1)
23
+ end
24
+ end
@@ -0,0 +1,30 @@
1
+ require 'spec_helper'
2
+
3
+ describe Fiddler::Parsers::TicketParser do
4
+ before do
5
+ test_config
6
+ end
7
+
8
+ it "should return the response for proper request minus empty line" do
9
+ response = Fiddler::ConnectionManager.get("/ticket/4200")
10
+ response = Fiddler::Parsers::TicketParser.check_response_code(response)
11
+ desired_length = response.length
12
+ Fiddler::Parsers::TicketParser.check_for_errors(response).length.should eql(desired_length)
13
+ end
14
+
15
+ it "should raise TicketFetchError for invalid response" do
16
+ response = Fiddler::ConnectionManager.get("/ticket/asdsadasds")
17
+ response = Fiddler::Parsers::TicketParser.check_response_code(response)
18
+ expect { Fiddler::Parsers::TicketParser.check_for_errors(response) }.to raise_error
19
+ end
20
+
21
+ it "should give ticket object for parse single method" do
22
+ response = Fiddler::ConnectionManager.get("/ticket/4200")
23
+ Fiddler::Parsers::TicketParser.parse_single(response).should be_a_kind_of(Fiddler::Ticket)
24
+ end
25
+
26
+ it "should give array of ticket objects for parse multiple method" do
27
+ response = Fiddler::ConnectionManager.get("/search/ticket?query=Owner='jais.cheema'")
28
+ Fiddler::Parsers::TicketParser.parse_multiple(response).should be_a_kind_of(Array)
29
+ end
30
+ end
data/spec/spec_helper.rb CHANGED
@@ -1,13 +1,21 @@
1
+ require 'rubygems'
2
+ require 'spork'
3
+ require 'rspec'
4
+
1
5
  PROJECT_ROOT = File.expand_path('../..', __FILE__)
2
6
  $LOAD_PATH << File.join(PROJECT_ROOT, 'lib')
3
7
 
4
8
  require 'fiddler'
5
9
 
6
- RSpec.configure do |config|
7
- config.treat_symbols_as_metadata_keys_with_true_values = true
8
- config.run_all_when_everything_filtered = true
9
- config.filter_run :focus
10
- config.order = 'random'
10
+ Spork.prefork do
11
+ RSpec.configure do |config|
12
+ config.treat_symbols_as_metadata_keys_with_true_values = true
13
+ config.run_all_when_everything_filtered = true
14
+ config.filter_run :focus
15
+ end
16
+ end
17
+
18
+ Spork.each_run do
11
19
  end
12
20
 
13
21
  # create a file named config.rb here with the two methods
data/spec/ticket_spec.rb CHANGED
@@ -6,10 +6,16 @@ describe Fiddler::Ticket do
6
6
  end
7
7
 
8
8
  it "should find a ticket with given id" do
9
- Fiddler::Ticket.get(4200).should_not be_nil
9
+ Fiddler::Ticket.get(4200).should be_a_kind_of(Fiddler::Ticket)
10
10
  end
11
11
 
12
12
  it "should raise exception for invalid id" do
13
13
  expect { Fiddler::Ticket.get(50000) }.to raise_error(Fiddler::TicketNotFoundError)
14
14
  end
15
+
16
+ describe "executing all method" do
17
+ it "should return all the user assigned tickets for empty conditions" do
18
+ Fiddler::Ticket.all.should be_a_kind_of(Array)
19
+ end
20
+ end
15
21
  end
metadata CHANGED
@@ -1,8 +1,8 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fiddler
3
3
  version: !ruby/object:Gem::Version
4
- prerelease: 5
5
- version: 0.0.1alpha
4
+ prerelease:
5
+ version: 0.0.1
6
6
  platform: ruby
7
7
  authors:
8
8
  - Jais Cheema
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2012-10-03 00:00:00 Z
13
+ date: 2012-10-04 00:00:00 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: httpclient
@@ -45,6 +45,28 @@ dependencies:
45
45
  version: "0"
46
46
  type: :development
47
47
  version_requirements: *id003
48
+ - !ruby/object:Gem::Dependency
49
+ name: spork
50
+ prerelease: false
51
+ requirement: &id004 !ruby/object:Gem::Requirement
52
+ none: false
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: "0"
57
+ type: :development
58
+ version_requirements: *id004
59
+ - !ruby/object:Gem::Dependency
60
+ name: watchr
61
+ prerelease: false
62
+ requirement: &id005 !ruby/object:Gem::Requirement
63
+ none: false
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: "0"
68
+ type: :development
69
+ version_requirements: *id005
48
70
  description: This is the interface to request tracker based on Roart Gem
49
71
  email:
50
72
  - jaischeema@gmail.com
@@ -59,18 +81,24 @@ files:
59
81
  - lib/tasks/fiddler_tasks.rake
60
82
  - lib/fiddler/version.rb
61
83
  - lib/fiddler/errors.rb
84
+ - lib/fiddler/formatters.rb
62
85
  - lib/fiddler/connection_manager.rb
63
86
  - lib/fiddler/parsers.rb
64
87
  - lib/fiddler/configuration.rb
65
- - lib/fiddler/helper.rb
66
88
  - lib/fiddler/ticket.rb
67
89
  - lib/fiddler/parsers/ticket_parser.rb
68
90
  - lib/fiddler/parsers/base_parser.rb
91
+ - lib/fiddler/formatters/search_request_formatter.rb
92
+ - lib/fiddler/formatters/base_formatter.rb
69
93
  - spec/spec_helper.rb
70
94
  - spec/ticket_spec.rb
71
95
  - spec/configuration_spec.rb
72
96
  - spec/config.rb
73
97
  - spec/connection_manager_spec.rb
98
+ - spec/formatters/search_request_formatter_spec.rb
99
+ - spec/formatters/base_formatter_spec.rb
100
+ - spec/parsers/base_parser_spec.rb
101
+ - spec/parsers/ticket_parser_spec.rb
74
102
  - Rakefile
75
103
  - README.md
76
104
  homepage: https://github.com/jaischeema/fiddler
@@ -90,9 +118,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
90
118
  required_rubygems_version: !ruby/object:Gem::Requirement
91
119
  none: false
92
120
  requirements:
93
- - - ">"
121
+ - - ">="
94
122
  - !ruby/object:Gem::Version
95
- version: 1.3.1
123
+ version: "0"
96
124
  requirements: []
97
125
 
98
126
  rubyforge_project:
@@ -106,3 +134,7 @@ test_files:
106
134
  - spec/configuration_spec.rb
107
135
  - spec/config.rb
108
136
  - spec/connection_manager_spec.rb
137
+ - spec/formatters/search_request_formatter_spec.rb
138
+ - spec/formatters/base_formatter_spec.rb
139
+ - spec/parsers/base_parser_spec.rb
140
+ - spec/parsers/ticket_parser_spec.rb
@@ -1,9 +0,0 @@
1
- module Fiddler
2
- module Helper
3
- class << self
4
- def valid?(value)
5
- ( value.nil? or value == "" ) ? false : true
6
- end
7
- end
8
- end
9
- end