Class: Simplificator::Webthumb::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/rwebthumb/base.rb

Direct Known Subclasses

Job, Webthumb

Constant Summary collapse

VALID_OUTPUT_TYPES =

Valid output_types for thumbnail requests

[:jpg, :png]
VALID_SIZES =

Valid values for size element in fetch requests

[:small, :medium, :medium2, :large, :full, :excerpt, :effect, :custom, :zip]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key, api_endpoint = 'http://webthumb.bluga.net/api.php') ⇒ Base

Constructor

api_key: the Webthumb api key, not nil and not blank

Raises:



17
18
19
20
21
22
# File 'lib/rwebthumb/base.rb', line 17

def initialize(api_key, api_endpoint = 'http://webthumb.bluga.net/api.php')
  raise WebthumbException.new('Need an not nil and not blank api_key') if api_key == nil || api_key == ''
  @api_key = api_key
  @api_endpoint = api_endpoint
  @api_uri = URI.parse(@api_endpoint)
end

Instance Attribute Details

#api_keyObject (readonly)

Returns the value of attribute api_key.



13
14
15
# File 'lib/rwebthumb/base.rb', line 13

def api_key
  @api_key
end

Class Method Details

.parse_webthumb_datetime(s) ⇒ Object

Parse the datetime string and returns a DateTime object in UTC time Webthumb returns the time in MST (Mountain Standard Time) which is some 7 hours behind UTC



27
28
29
# File 'lib/rwebthumb/base.rb', line 27

def self.parse_webthumb_datetime(s)
  Time.parse("#{s} MST").getutc
end

Instance Method Details

#build_root_nodeObject

builds the root node for webthumb requtes



55
56
57
58
59
60
# File 'lib/rwebthumb/base.rb', line 55

def  build_root_node()
  root = REXML::Element.new('webthumb')
  api = root.add_element('apikey')
  api.text = @api_key
  root
end

#do_request(xml) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/rwebthumb/base.rb', line 31

def do_request(xml)
  request = Net::HTTP::Post.new(@api_uri.path)
  request.body = xml.to_s
  response = Net::HTTP.new(@api_uri.host, @api_uri.port).start {|p| p.request(request) }
  case response
  when Net::HTTPOK
    case response.content_type.downcase
    when 'text/xml'
      REXML::Document.new(response.body)
    when 'text/plain'
      raise WebthumbException.new("Unsupported content type #{response.content_type}: Body was: #{response.body}")
    when 'image/jpg', 'image/jpeg', 'image/png', 'archive/zip'
      raise WebthumbException.new("No data returned though content type is #{response.content_type}") if response.body.length == 0
      response.body
    else
      raise WebthumbException.new("Unsupported content type #{response.content_type}")
    end
  else
    raise CommunicationException('Response code was not HTTP OK')
  end
end