Class: Whiplash::Batches::Api
- Inherits:
-
Object
- Object
- Whiplash::Batches::Api
- Defined in:
- lib/whiplash/batches/api.rb
Instance Attribute Summary collapse
-
#agent ⇒ Object
readonly
Returns the value of attribute agent.
-
#current_page ⇒ Object
readonly
Returns the value of attribute current_page.
-
#order_current_page ⇒ Object
readonly
Returns the value of attribute order_current_page.
-
#whiplash_base_url ⇒ Object
readonly
Returns the value of attribute whiplash_base_url.
Instance Method Summary collapse
- #batches(page = 1, ignore_ids = []) ⇒ Object
-
#initialize ⇒ Api
constructor
A new instance of Api.
- #login ⇒ Object
- #orders(batch_id = nil) ⇒ Object
Constructor Details
#initialize ⇒ Api
Returns a new instance of Api.
5 6 7 8 9 10 11 12 13 14 |
# File 'lib/whiplash/batches/api.rb', line 5 def initialize if Whiplash.configuration.present? @username = Whiplash.configuration.whiplash_email @password = Whiplash.configuration.whiplash_password @whiplash_base_url = Whiplash.configuration.whiplash_base_url end check_confuration_variables @agent = Mechanize.new login end |
Instance Attribute Details
#agent ⇒ Object (readonly)
Returns the value of attribute agent.
3 4 5 |
# File 'lib/whiplash/batches/api.rb', line 3 def agent @agent end |
#current_page ⇒ Object (readonly)
Returns the value of attribute current_page.
3 4 5 |
# File 'lib/whiplash/batches/api.rb', line 3 def current_page @current_page end |
#order_current_page ⇒ Object (readonly)
Returns the value of attribute order_current_page.
3 4 5 |
# File 'lib/whiplash/batches/api.rb', line 3 def order_current_page @order_current_page end |
#whiplash_base_url ⇒ Object (readonly)
Returns the value of attribute whiplash_base_url.
3 4 5 |
# File 'lib/whiplash/batches/api.rb', line 3 def whiplash_base_url @whiplash_base_url end |
Instance Method Details
#batches(page = 1, ignore_ids = []) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/whiplash/batches/api.rb', line 16 def batches(page = 1, ignore_ids = []) results = [] ignore_ids.map!(&:to_s) # convert to ids of string @current_page = agent.get("#{@whiplash_base_url}/order_batches?page=#{page}") results << build_batch_hash(current_page, ignore_ids, page) page += 1 # page is scrapped already loop do next_page = current_page.links.find{ |l| l.href.to_s.include?("/order_batches?page=#{page}") } if next_page.present? batch_page = next_page.click results << build_batch_hash(batch_page, ignore_ids, page) page += 1 else break end end results.flatten end |
#login ⇒ Object
43 44 45 46 47 48 49 50 51 52 |
# File 'lib/whiplash/batches/api.rb', line 43 def login page = agent.get("#{@whiplash_base_url}/login") @current_page = page.form_with(:id => 'new_user') do |form| form.field_with(name: 'user[email]').value = @username form.field_with(name: 'user[password]').value = @password end.submit if current_page.title == "Log In" raise "Credentials: Login credentials invalid!" end end |
#orders(batch_id = nil) ⇒ Object
36 37 38 39 40 41 |
# File 'lib/whiplash/batches/api.rb', line 36 def orders(batch_id = nil) return [] if batch_id.nil? @order_current_page = agent.get("#{@whiplash_base_url}/order_batches/#{batch_id}") orders = order_current_page.links.select { |l| l.href.to_s.match(/\/orders\/.[0-9]+/) } orders.map{ |l| l.text.split('-').first } end |