Class: CrowdFlower::Connection

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

Overview

an object that stores connection details; does actual http talking

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key, domain_base, version, ssl_port = 443, public_port = 80) ⇒ Connection

Returns a new instance of Connection.



51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/crowdflower/base.rb', line 51

def initialize(key, domain_base, version, ssl_port = 443, public_port = 80)
  @domain_base = domain_base
  @version = version
  @domain = "#{@domain_base}/v#{version}"
  @key = key
  @ssl_port = ssl_port
  @public_port = public_port
  begin # pass yaml file
    key = YAML.load_file(key)
    @key = key[:key] || key["key"]
  rescue # pass key
    @key = key
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_id, *args) ⇒ Object

get, post, put and delete methods



67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/crowdflower/base.rb', line 67

def method_missing(method_id, *args)
  if [:get, :post, :put, :delete].include?(method_id)
    path, options = *args
    options ||= {}
    options[:query] = (default_params.merge(options[:query] || {}))
    options[:headers] = (self.class.default_options[:headers].merge(options[:headers] || {}))

    CrowdFlower.request_hook.call(method_id, path, options) do
      self.class.send(method_id, url(path), options)
    end
  else
    super
  end
end

Instance Attribute Details

#domainObject (readonly)

Returns the value of attribute domain.



49
50
51
# File 'lib/crowdflower/base.rb', line 49

def domain
  @domain
end

#domain_baseObject (readonly)

Returns the value of attribute domain_base.



49
50
51
# File 'lib/crowdflower/base.rb', line 49

def domain_base
  @domain_base
end

#keyObject (readonly)

Returns the value of attribute key.



49
50
51
# File 'lib/crowdflower/base.rb', line 49

def key
  @key
end

#public_portObject (readonly)

Returns the value of attribute public_port.



49
50
51
# File 'lib/crowdflower/base.rb', line 49

def public_port
  @public_port
end

#ssl_portObject (readonly)

Returns the value of attribute ssl_port.



49
50
51
# File 'lib/crowdflower/base.rb', line 49

def ssl_port
  @ssl_port
end

#versionObject (readonly)

Returns the value of attribute version.



49
50
51
# File 'lib/crowdflower/base.rb', line 49

def version
  @version
end

Instance Method Details

#crowdflower_baseString

Returns the base crowdflower domain from the api url’s domain.

Examples:

CrowdFlower::Connection.new("asdf", "https://api.crowdflower.com").crowdflower_base #=> "crowdflower.com"

Returns:

  • (String)

    the base crowdflower domain



88
89
90
91
# File 'lib/crowdflower/base.rb', line 88

def crowdflower_base
  uri = URI.parse(domain_base)
  "#{uri.host.gsub("api.", "")}"
end

#public_urlString

Returns the url to reach crowdflower regularly through a browser

Examples:

CrowdFlower::Connection.new("asdf", "https://api.crowdflower.com").public_url #=> "crowdflower.com:80"

Returns:

  • (String)

    the url to reach crowdflower in a browser



99
100
101
# File 'lib/crowdflower/base.rb', line 99

def public_url
  "#{crowdflower_base}:#{public_port}"
end