Class: Infosimples::Data::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/infosimples/data/client.rb

Overview

Infosimples::Data::Client is a client for the Infosimples Data API.

Constant Summary collapse

BASE_URL =
'https://data.infosimples.com/api/v1/:service.json'
ENCRYPTABLE_ARGS =
['pkcs12_cert', 'pkcs12_pass', 'pass'].map {
  |arg| [arg, true]
}.to_h

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(token, options = {}) ⇒ Infosimples::Data::Client

Create a Infosimples::Data API client.

Parameters:

  • token (String)

    Your access token.

  • options (Hash) (defaults to: {})

    Options hash.

Options Hash (options):

  • :timeout (Integer) — default: 120

    Seconds before giving up of an automation being completed.

  • :max_age (Integer) — default: 86400

    Duration in seconds for a cached automation to be allowed.


22
23
24
25
26
# File 'lib/infosimples/data/client.rb', line 22

def initialize(token, options = {})
  self.token    = token
  self.timeout  = options[:timeout] || 120   # 120 seconds
  self.max_age  = options[:max_age] || 86400 # 24 hours in seconds
end

Instance Attribute Details

#max_ageObject

Returns the value of attribute max_age


9
10
11
# File 'lib/infosimples/data/client.rb', line 9

def max_age
  @max_age
end

#timeoutObject

Returns the value of attribute timeout


9
10
11
# File 'lib/infosimples/data/client.rb', line 9

def timeout
  @timeout
end

#tokenObject

Returns the value of attribute token


9
10
11
# File 'lib/infosimples/data/client.rb', line 9

def token
  @token
end

Instance Method Details

#automate(service, args = {}) ⇒ Hash

Automate a service.

Parameters:

  • service (String)

    Service you want to automate.

  • args (Hash) (defaults to: {})

    Arguments to be used in the service automation.

Returns:


34
35
36
37
38
39
40
41
# File 'lib/infosimples/data/client.rb', line 34

def automate(service, args = {})
  args.keys.each do |key|
    if ENCRYPTABLE_ARGS[key.to_s]
      args[key] = encrypt(args[key])
    end
  end
  request(service, :multipart, args)
end

#billingArray

Get billing statistics from your account.

Returns:

  • (Array)

    Billing statistics per token.


46
47
48
# File 'lib/infosimples/data/client.rb', line 46

def billing
  request('billing', :get)
end

#download_sites_urls(response) ⇒ Array

Download sites_urls from response.

Parameters:

  • response (Hash)

    Response returned by #automate.

Returns:

  • (Array)

    HTML bodies from sites_urls.


62
63
64
65
66
67
68
# File 'lib/infosimples/data/client.rb', line 62

def download_sites_urls(response)
  return [] if !response.is_a?(Hash) ||
               (sites_urls = response.dig('receipt', 'sites_urls')).nil?
  sites_urls.map do |url|
    Infosimples::Data::HTTP.request(url: url, http_timeout: 30)
  end
end

#pricingArray

Get prices for each service.

Returns:

  • (Array)

    Service with price.


53
54
55
# File 'lib/infosimples/data/client.rb', line 53

def pricing
  request('pricing', :get)
end