Class: DataMapper::Adapters::BugzillaAdapter

Inherits:
AbstractAdapter
  • Object
show all
Defined in:
lib/dm-bugzilla-adapter.rb,
lib/dm-bugzilla-adapter/misc.rb,
lib/dm-bugzilla-adapter/create.rb,
lib/dm-bugzilla-adapter/delete.rb,
lib/dm-bugzilla-adapter/update.rb,
lib/dm-bugzilla-adapter/version.rb

Constant Summary collapse

VERSION =
"0.0.1"

Instance Method Summary collapse

Constructor Details

#initialize(name, options) ⇒ BugzillaAdapter

Returns a new instance of BugzillaAdapter.



36
37
38
39
40
41
# File 'lib/dm-bugzilla-adapter.rb', line 36

def initialize(name, options)
  super
  require 'uri'
  @uri = URI.parse(options[:url])
  @client = Bicho::Client.new(@uri)
end

Instance Method Details

#create(resources) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/dm-bugzilla-adapter/create.rb', line 4

def create(resources)
  STDERR.puts "BugzillaAdapter::create"
  table_groups = group_resources_by_table(resources)
  table_groups.each do |table, resources|
    # make
    #  class User
    #    property :id, Serial
    #  end
    # work
    resources.each do |resource|
	initialize_serial(resource,
	  worksheet_record_cound(table)+1)
	post_resource_to_worksheet(resource,table)
    end
  end
  resources.size
end

#delete(collection) ⇒ Integer

Constructs and executes DELETE statement for given query

Parameters:

  • collection (Collection)

    collection of records to be deleted

Returns:

  • (Integer)

    the number of records deleted



14
15
16
17
18
19
20
# File 'lib/dm-bugzilla-adapter/delete.rb', line 14

def delete(collection)
  each_resource_with_edit_url(collection) do |resource, edit_url|
    connection.delete(edit_url, 'If-Match' => "*")
  end
  # return count
  collection.size
end

#get_bugs(ids) ⇒ Object

get Bugs by ids



44
45
46
# File 'lib/dm-bugzilla-adapter.rb', line 44

def get_bugs(ids)
  bugs = @client.get_bugs(*ids)
end

#named_query(name) ⇒ Object

run named query, return Array of bug ids



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/dm-bugzilla-adapter.rb', line 49

def named_query(name)
  http = Net::HTTP.new(@uri.host, @uri.port)
  http.use_ssl = true
  http.verify_mode = OpenSSL::SSL::VERIFY_NONE
  request = Net::HTTP::Get.new("/buglist.cgi?cmdtype=runnamed&namedcmd=#{name}&ctype=atom", { "Cookie" => @client.cookie } )
  response = http.request(request)
  case response
  when Net::HTTPSuccess
	bugs = []
	xml = Nokogiri::XML.parse(response.body)
	xml.root.xpath("//xmlns:entry/xmlns:link/@href", xml.root.namespace).each do |attr|
	  uri = URI.parse attr.value
	  bugs << uri.query.split("=")[1]
	end
	get_bugs(bugs)
  when Net::HTTPRedirect
	raise "HTTP redirect not supported in named_query"
  else
	response.error!
  end
end

#update(attributes, collection) ⇒ Integer

Constructs and executes UPDATE statement for given attributes and a query

Parameters:

  • attributes (Hash(Property => Object))

    hash of attribute values to set, keyed by Property

  • collection (Collection)

    collection of records to be updated

Returns:

  • (Integer)

    the number of records updated



16
17
18
19
20
21
22
# File 'lib/dm-bugzilla-adapter/update.rb', line 16

def update(attributes, collection)
  each_resource_with_edit_url(collection) do |resource, edit_url|
    put_updated_resource(edit_url, resource)
  end
  # return count
  collection.size
end