Class: BookValue::Client

Inherits:
Object
  • Object
show all
Includes:
Constants
Defined in:
lib/book-value/client.rb

Constant Summary

Constants included from Constants

BookValue::Constants::BASE_PATH

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(port: 443) ⇒ Client

Returns a new instance of Client.



7
8
9
10
# File 'lib/book-value/client.rb', line 7

def initialize(port: 443)
  @base_path = BASE_PATH
  @port = port
end

Instance Attribute Details

#base_pathObject (readonly)

Returns the value of attribute base_path.



5
6
7
# File 'lib/book-value/client.rb', line 5

def base_path
  @base_path
end

#portObject (readonly)

Returns the value of attribute port.



5
6
7
# File 'lib/book-value/client.rb', line 5

def port
  @port
end

Class Method Details

.api_versionObject



12
13
14
# File 'lib/book-value/client.rb', line 12

def self.api_version
  'v1 2023-03-15'
end

Instance Method Details

#car_features(make, model) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/book-value/client.rb', line 41

def car_features(make, model)
  checkbox_options = {}

  process_model(model).each do |model_name|
    raw_page = authorise_and_send(http_method: :get, command: "calculate/#{make.downcase}/#{model_name}")

    doc = Nokogiri::HTML(raw_page['body'])

    doc.css(".list-group li div").each do |checkbox|
      input_of_child = checkbox.children.find { |child| child.name == 'input' }
      label_of_child = checkbox.children.find { |child| child.name == 'label' }

      checkbox_options[input_of_child[:name]] = label_of_child.children.first.to_s
    end
  end

  checkbox_options
end

#car_makesObject

TODO: Handle errors



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/book-value/client.rb', line 17

def car_makes
  raw_page = authorise_and_send(http_method: :get)
  doc = Nokogiri::HTML(raw_page['body'])

  options = {}
  doc.css("select[name='make'] option").each do |option|
    options[option.text.strip] = { name: option.text.strip, value: option.values.first, img: img_path(option.text.strip) }
  end

  options
end

#car_models(make) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/book-value/client.rb', line 29

def car_models(make)
  raw_page = authorise_and_send(http_method: :get, command: "calculate/#{make.downcase}")
  doc = Nokogiri::HTML(raw_page['body'])

  options = {}
  doc.css("select[name='model'] option").each do |option|
    options[option.text.strip] = { name: option.text.strip, value: option.values.first }
  end

  options
end

#get_book_value(make, model, features, mileage, year, condition_score = 10) ⇒ Object

Features are just the list of features condition_score is between 1-10, 10 is perfect, 1 is bad



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/book-value/client.rb', line 62

def get_book_value(make, model, features, mileage, year, condition_score = 10)
  output = ''
  feature_params = ''

  process_model(model).each do |model_name|
    features.each do |feature_id|
      feature_params = "#{feature_params}#{feature_id}=on&"
    end

    feature_params = feature_params[0..-2]

    milage_form_page = authorise_and_send(http_method: :post, payload: feature_params, command: "calculate/#{make.downcase}/#{model_name}")
    next unless milage_form_page['metadata']['ok']

    condition_url = milage_form_page['headers']['location']

    _condition_page = HTTParty.post(condition_url, body: "mileage=#{mileage}&year=#{year}")

    book_value_url = condition_url.gsub('/4', '/5')
    book_value_page = HTTParty.post(book_value_url, body: "condition_score=#{condition_score}")

    doc = Nokogiri::HTML(book_value_page.body)

    output = doc.at('h4').text
  end

  output
end