Class: Datahen::Client::Base

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/datahen/client/base.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Base

Returns a new instance of Base.



36
37
38
39
40
41
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
72
73
74
# File 'lib/datahen/client/base.rb', line 36

def initialize(opts={})
  @ignore_ssl = opts[:ignore_ssl]
  self.class.base_uri(env_api_url)
  self.auth_token = opts[:auth_token] unless opts[:auth_token].nil?
  @options = {
    headers: {
      "Authorization" => "Bearer #{auth_token}",
      "Content-Type" => "application/json",
    },
    verify: !ignore_ssl
  }

  query = {}
  query[:p] = opts[:page] if opts[:page]
  query[:pp] = opts[:per_page] if opts[:per_page]
  query[:fetchfail] = opts[:fetch_fail] if opts[:fetch_fail]
  query[:parsefail] = opts[:parse_fail] if opts[:parse_fail]
  query[:status] = opts[:status] if opts[:status]
  query[:page_type] = opts[:page_type] if opts[:page_type]
  query[:gid] = opts[:gid] if opts[:gid]
  query[:"min-timestamp"] = opts[:"min-timestamp"] if opts[:"min-timestamp"]
  query[:"max-timestamp"] = opts[:"max-timestamp"] if opts[:"max-timestamp"]
  query[:limit] = opts[:limit] if opts[:limit]
  query[:order] = opts[:order] if opts[:order]
  query[:filter] = opts[:filter] if opts[:filter]
  query[:force] = opts[:force] if opts[:force]

  if opts[:query]
    if opts[:query].is_a?(Hash)
      query[:q] = opts[:query].to_json
    elsif opts[:query].is_a?(String)
      query[:q] = JSON.parse(opts[:query]).to_json
    end
  end

  unless query.empty?
    @options.merge!(query: query)
  end
end

Class Method Details

.env_auth_tokenObject



10
11
12
# File 'lib/datahen/client/base.rb', line 10

def self.env_auth_token
  ENV['DATAHEN_TOKEN']
end

.env_ignore_sslObject



14
15
16
# File 'lib/datahen/client/base.rb', line 14

def self.env_ignore_ssl
  ENV['DATAHEN_IGNORE_SSL'].to_s.strip == '1'
end

Instance Method Details

#auth_tokenObject



28
29
30
# File 'lib/datahen/client/base.rb', line 28

def auth_token
  @auth_token ||= self.class.env_auth_token
end

#auth_token=(value) ⇒ Object



32
33
34
# File 'lib/datahen/client/base.rb', line 32

def auth_token= value
  @auth_token = value
end

#env_api_urlObject



18
19
20
# File 'lib/datahen/client/base.rb', line 18

def env_api_url
  ENV['DATAHEN_API_URL'].nil? ? 'https://app.datahen.com/api/v1' : ENV['DATAHEN_API_URL']
end

#ignore_sslObject



22
23
24
25
26
# File 'lib/datahen/client/base.rb', line 22

def ignore_ssl
  return @ignore_ssl unless @ignore_ssl.nil?
  @ignore_ssl = self.class.env_ignore_ssl
  @ignore_ssl
end