Class: RemoteBook::Amazon

Inherits:
Base
  • Object
show all
Defined in:
lib/remote_book/amazon.rb

Constant Summary collapse

DIGEST_SUPPORT =

FIXME: failed digest support should raise exception. mac os 10.5 does not ship with SHA256 support built into ruby, 10.6 does.

::OpenSSL::Digest.constants.include?('SHA256') || ::OpenSSL::Digest.constants.include?(:SHA256)
DIGEST =
::OpenSSL::Digest::Digest.new('sha256')

Instance Attribute Summary collapse

Attributes inherited from Base

#isbn, #link

Class Method Summary collapse

Methods inherited from Base

associate_keys, associate_keys=, #author, find_by_isbn, setup

Instance Attribute Details

#authorsObject

Returns the value of attribute authors.



11
12
13
# File 'lib/remote_book/amazon.rb', line 11

def authors
  @authors
end

#large_imageObject

Returns the value of attribute large_image.



11
12
13
# File 'lib/remote_book/amazon.rb', line 11

def large_image
  @large_image
end

#medium_imageObject

Returns the value of attribute medium_image.



11
12
13
# File 'lib/remote_book/amazon.rb', line 11

def medium_image
  @medium_image
end

#raw_responseObject

Returns the value of attribute raw_response.



11
12
13
# File 'lib/remote_book/amazon.rb', line 11

def raw_response
  @raw_response
end

#small_imageObject

Returns the value of attribute small_image.



11
12
13
# File 'lib/remote_book/amazon.rb', line 11

def small_image
  @small_image
end

#titleObject

Returns the value of attribute title.



11
12
13
# File 'lib/remote_book/amazon.rb', line 11

def title
  @title
end

Class Method Details

.find(options) ⇒ Object

Raises:



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/remote_book/amazon.rb', line 13

def self.find(options)
  raise AmazonError.new("RemoteBook::Amazon.associate_keys requires :key_id") unless associate_keys.has_key?(:key_id)
  raise AmazonError.new("RemoteBook::Amazon.associate_keys requires :associates_id") unless associate_keys.has_key?(:associates_id)
  raise AmazonError.new("RemoteBook::Amazon.associate_keys requires :secret_key") unless associate_keys.has_key?(:secret_key)
  a = new
  # unless DIGEST_SUPPORT raise "no digest sup"
  if options[:isbn]
    req = build_isbn_lookup_query(options[:isbn])
    a.raw_response = RemoteBook.get_url(req)

    if a.raw_response.respond_to?(:code) && "200" == a.raw_response.code
      xml_doc = Nokogiri.XML(a.raw_response.body)
    else
      return false 
    end

    if 1 == xml_doc.xpath("//xmlns:Items").size
      if xml_doc.xpath("//xmlns:Items/xmlns:Item/xmlns:LargeImage/xmlns:URL")
        a.large_image  = xml_doc.xpath("//xmlns:Items/xmlns:Item/xmlns:LargeImage/xmlns:URL").inner_text
      end
      if xml_doc.xpath("//xmlns:Items/xmlns:Item/xmlns:MediumImage/xmlns:URL")
        a.medium_image = xml_doc.xpath("//xmlns:Items/xmlns:Item/xmlns:MediumImage/xmlns:URL").inner_text
      end
      if xml_doc.xpath("//xmlns:Items/xmlns:Item/xmlns:SmallImage/xmlns:URL")
        a.small_image  = xml_doc.xpath("//xmlns:Items/xmlns:Item/xmlns:SmallImage/xmlns:URL").inner_text
      end
      a.title        = xml_doc.xpath("//xmlns:Items/xmlns:Item/xmlns:ItemAttributes/xmlns:Title").inner_text
      a.authors = []
      xml_doc.xpath("//xmlns:Items/xmlns:Item/xmlns:ItemAttributes/xmlns:Author").each do |author|
        a.authors << author.inner_text
      end
      # ewww
      if xml_doc.xpath("//xmlns:Items/xmlns:Item/xmlns:ItemLinks/xmlns:ItemLink/xmlns:Description='Technical Details'")
        xml_doc.xpath("//xmlns:Items/xmlns:Item/xmlns:ItemLinks/xmlns:ItemLink").each do |item_link|
          if "Technical Details" == item_link.xpath("xmlns:Description").inner_text
            a.link = item_link.xpath("xmlns:URL").inner_text
          end
        end
      end
    end
  end  
  return a
end