Class: DataMapper::Adapters::KeeperAdapter

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

Constant Summary collapse

OSCRC_CREDENTIALS =
"https://api.opensuse.org"
VERSION =
"0.0.4"

Instance Method Summary collapse

Constructor Details

#initialize(name, options) ⇒ KeeperAdapter

Returns a new instance of KeeperAdapter.



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/dm-keeper-adapter.rb', line 53

def initialize(name, options)
  super
  require 'net/https'
  require 'uri'
  @uri = URI.parse(options[:url])
  @username = options[:username]
  @password = options[:password]
  unless @username && @password
	require 'inifile'
	oscrc = IniFile.new(File.join(ENV['HOME'], '.oscrc'))
    if oscrc.has_section?(OSCRC_CREDENTIALS)
      @username = oscrc[OSCRC_CREDENTIALS]['user']
      @password = oscrc[OSCRC_CREDENTIALS]['pass']
      raise "No .oscrc credentials for keeper.novell.com" unless @username && @password
    end
  end
  @connection = Net::HTTP.new( @uri.host, @uri.port )
  # from http://www.rubyinside.com/nethttp-cheat-sheet-2940.html
  @connection.use_ssl = true if @uri.scheme == "https"
  @connection.verify_mode = OpenSSL::SSL::VERIFY_NONE

end

Instance Method Details

#create(resources) ⇒ Object



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

def create(resources)
  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) ⇒ Object



4
5
6
7
8
9
10
# File 'lib/dm-keeper-adapter/delete.rb', line 4

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(path) ⇒ Object

Raises:

  • (ArgumentError)


76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/dm-keeper-adapter.rb', line 76

def get(path)
#      STDERR.puts "Get #{@uri.path} + #{path}"
  request = Net::HTTP::Get.new @uri.path + path
  request.basic_auth @username, @password
  response = @connection.request request
  raise ArgumentError unless response
  raise( RuntimeError, "Server returned #{response.code}" ) unless response.code.to_i == 200
#      $stderr.puts "#{response.inspect}"
#      response.each { |k,v| $stderr.puts "#{k}: #{v}" }
#      $stderr.puts "Encoding >#{response['content-encoding']}<"
  # check
  # content-type: text/xml;charset=UTF-8
  # content-encoding: gzip
  body = response.body
  if response['content-encoding'] == "gzip"
	require 'zlib'
	require 'stringio'
	# http://stackoverflow.com/questions/1361892/how-to-decompress-gzip-string-in-ruby
	body = Zlib::GzipReader.new(StringIO.new(body)).read
  end
  case response['content-type']
	when /xml/
	  Nokogiri::XML body
	when /html/
	  Nokogiri::HTML body
	else
	  raise TypeError, "Unknown content-type #{response['content-type']}"
  end
end

#update(attributes, collection) ⇒ Object



4
5
6
7
8
9
10
# File 'lib/dm-keeper-adapter/update.rb', line 4

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