Class: SharedBook

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

Constant Summary collapse

URL =
"http://api.sharedbook.com/v0.6"
DEFAULT_THEME =
'vanilla'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ SharedBook

Returns a new instance of SharedBook.



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/shared_book.rb', line 42

def initialize(opts = {})
  if opts[:product_api_key]
    @product_api_key = opts[:product_api_key]
  else
    raise MissingProductApiKeyError
  end
  
  if opts[:product_secret_word]
    @product_secret_word = opts[:product_secret_word]
  else
    raise MissingProductSecretWordError
  end
  
  if opts[:auth_token]
    @auth_token = if opts[:auth_token] == 'auto'
      get_new_auth_token
    else
      opts[:auth_token]
    end
  else
    raise MissingAuthTokenError
  end

  if opts[:session_token]
    @session_token = opts[:session_token]
  else
    @session_token = auth_getSessionToken if opts[:get_session_token]
  end

end

Instance Attribute Details

#articlesObject

Returns the value of attribute articles.



40
41
42
# File 'lib/shared_book.rb', line 40

def articles
  @articles
end

#auth_tokenObject (readonly)

Returns the value of attribute auth_token.



39
40
41
# File 'lib/shared_book.rb', line 39

def auth_token
  @auth_token
end

#back_cover_photo_idObject (readonly)

Returns the value of attribute back_cover_photo_id.



39
40
41
# File 'lib/shared_book.rb', line 39

def back_cover_photo_id
  @back_cover_photo_id
end

#bms_idObject (readonly)

Returns the value of attribute bms_id.



39
40
41
# File 'lib/shared_book.rb', line 39

def bms_id
  @bms_id
end

#book_idObject (readonly)

Returns the value of attribute book_id.



39
40
41
# File 'lib/shared_book.rb', line 39

def book_id
  @book_id
end

#book_titleObject

Returns the value of attribute book_title.



40
41
42
# File 'lib/shared_book.rb', line 40

def book_title
  @book_title
end

#book_urlObject (readonly)

Returns the value of attribute book_url.



39
40
41
# File 'lib/shared_book.rb', line 39

def book_url
  @book_url
end

#comment_idsObject (readonly)

Returns the value of attribute comment_ids.



39
40
41
# File 'lib/shared_book.rb', line 39

def comment_ids
  @comment_ids
end

#front_cover_photo_idObject (readonly)

Returns the value of attribute front_cover_photo_id.



39
40
41
# File 'lib/shared_book.rb', line 39

def front_cover_photo_id
  @front_cover_photo_id
end

#photo_idsObject (readonly)

Returns the value of attribute photo_ids.



39
40
41
# File 'lib/shared_book.rb', line 39

def photo_ids
  @photo_ids
end

#product_api_keyObject (readonly)

Returns the value of attribute product_api_key.



39
40
41
# File 'lib/shared_book.rb', line 39

def product_api_key
  @product_api_key
end

#product_secret_wordObject (readonly)

Returns the value of attribute product_secret_word.



39
40
41
# File 'lib/shared_book.rb', line 39

def product_secret_word
  @product_secret_word
end

#session_tokenObject (readonly)

Returns the value of attribute session_token.



39
40
41
# File 'lib/shared_book.rb', line 39

def session_token
  @session_token
end

Class Method Details

.auth_login_urlObject



73
74
75
# File 'lib/shared_book.rb', line 73

def self.
  "#{URL}/auth/login"
end

Instance Method Details

#auth_getSessionTokenObject



77
78
79
80
81
# File 'lib/shared_book.rb', line 77

def auth_getSessionToken
  return session_token if session_token # you can't call the service twice with the same auth_token
  response = get_url("#{URL}/auth/getSessionToken", :apiKey => @product_api_key, :authToken => @auth_token)
  parse_response(response, /\<sessionToken\>(.*)\<\/sessionToken\>/)
end

#bms_addComment(opts = {}) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/shared_book.rb', line 107

def bms_addComment(opts = {})
  required = {:bmsId => opts[:bms_id] || @bms_id, :commentTitle => opts[:comment_title], :commentText => opts[:comment_text],
  :chapterNumber => opts[:chapter_number].to_s, :ownerName => opts[:owner_name]}
  optional = {:time => opts[:time], :commentId => opts[:comment_id]}.reject{|k,v| v.nil?}
  
  response = post_url("#{URL}/bms/addComment", required.merge(optional))
  
  parse_response(response, /comment id=\"(.*)\"/) do |match|
    @comment_ids ||= []
    @comment_ids << match[1]
    @comment_ids.last
  end
end

#bms_addPhoto_by_handle(opts = {}) ⇒ Object



133
134
135
136
137
138
139
# File 'lib/shared_book.rb', line 133

def bms_addPhoto_by_handle(opts = {})
  parse_response(post_photo_data("#{URL}/bms/addPhoto", opts), /\<photo id=\"(.*)\"/) do |match|
    @photo_ids ||= []
    @photo_ids << match[1]
    @photo_ids.last
  end
end

#bms_addPhoto_by_url(opts = {}) ⇒ Object



121
122
123
124
125
126
127
128
129
130
131
# File 'lib/shared_book.rb', line 121

def bms_addPhoto_by_url(opts = {})
  required = {:bmsId => opts[:bms_id] || @bms_id, :url => opts[:file_url], :ownerName => opts[:owner_name]}
  optional = {:time => opts[:time], :caption => opts[:caption], :photoId => opts[:photo_id], :photoOrdinal => opts[:photo_ordinal]}.reject{|k,v| v.nil?}
  
  response = post_url("#{URL}/bms/addPhoto", required.merge(optional))
  parse_response(response, /\<photo id=\"(.*)\"/) do |match|
    @photo_ids ||= []
    @photo_ids << match[1]
    @photo_ids.last
  end
end

#bms_publish(bms_id = @bms_id) ⇒ Object



149
150
151
152
# File 'lib/shared_book.rb', line 149

def bms_publish(bms_id = @bms_id) 
  response = post_url("#{URL}/bms/publish", {:bmsId => bms_id})
  parse_response(response, /status=\"ok\"/) { true }
end

#bms_setBackCoverPhoto(opts = {}) ⇒ Object



145
146
147
# File 'lib/shared_book.rb', line 145

def bms_setBackCoverPhoto(opts = {})
  @back_cover_photo_id = set_cover_photo(:back, opts)
end

#bms_setFrontCoverPhoto(opts = {}) ⇒ Object



141
142
143
# File 'lib/shared_book.rb', line 141

def bms_setFrontCoverPhoto(opts = {})
  @front_cover_photo_id = set_cover_photo(:front, opts)
end

#bmscreate_init(book_title = @book_title, articles = @articles, theme = DEFAULT_THEME) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/shared_book.rb', line 87

def bmscreate_init(book_title=@book_title, articles=@articles, theme=DEFAULT_THEME)
  article_hash = if articles.class == Array
    hh = {}
    articles.each_with_index do |article, i|
      hh["chapterTitle#{i+1}".to_sym] = article[:chapterTitle]
      hh["chapterText#{i+1}".to_sym] = article[:chapterText]
    end
    hh
  else
    articles # expected to be hash with :chapterTitle and :chapterText
  end
  response = post_url("#{URL}/bmscreate/init", {:bookTitle => book_title, :theme => theme}.merge(article_hash))
  @bms_id = parse_response(response, /\<bms id=\"(\d+)\"/)
end

#bmscreate_publish(bms_id = @bms_id) ⇒ Object



102
103
104
105
# File 'lib/shared_book.rb', line 102

def bmscreate_publish(bms_id = @bms_id) 
  response = post_url("#{URL}/bmscreate/publish", {:bmsId => bms_id})
  parse_response(response, /status=\"ok\"/) { true }
end

#book_preview(bms_id = @bms_id, book_id = @book_id) ⇒ Object



169
170
171
172
173
174
175
176
177
# File 'lib/shared_book.rb', line 169

def book_preview(bms_id = @bms_id, book_id = @book_id)
  query = {
      :bmsId => bms_id.to_s, :bookId => book_id.to_s, :redirect => 'false', :apiKey => @product_api_key,
      :sessionToken => session_token, :authToken => @auth_token
  }
  query.merge!(generate_signature(query))
  response = get_url("#{URL}/book/preview", query)
  @book_url = parse_response(response, /\<url\>(.*)\<\/url\>/).gsub(/&amp\;/, "&")
end

#bookcreate_init(bms_id = @bms_id) ⇒ Object



154
155
156
157
# File 'lib/shared_book.rb', line 154

def bookcreate_init(bms_id = @bms_id) 
  response = post_url("#{URL}/bookcreate/init", {:bmsId => bms_id})    
  parse_response(response, /status=\"ok\"/) { true }
end

#bookcreate_publish(bms_id = @bms_id) ⇒ Object



164
165
166
167
# File 'lib/shared_book.rb', line 164

def bookcreate_publish(bms_id = @bms_id) 
  response = post_url("#{URL}/bookcreate/publish", {:bmsId => bms_id})
  @book_id = parse_response(response, /\<book id=\"(\d+)\" \/\>/)
end

#bookcreate_setDedication(opts = {}) ⇒ Object



159
160
161
162
# File 'lib/shared_book.rb', line 159

def bookcreate_setDedication(opts = {})
  response = post_url("#{URL}/bookcreate/setDedication", {:bmsId => opts[:bms_id] || @bms_id, :dedicationText => opts[:dedication_text]})
  parse_response(response, /status=\"ok\"/) { true }    
end